0bps

Strategy vault / Mean reversion

Moving Average and Stochastic Trading Strategy

This strategy combines moving averages and the Stochastic oscillator to implement an automated stock trading system. It uses two moving averages of different lengths and the Stochastic indicator to capture trend and…

Author
ChaoZhang
Complexity
Starter · 67 lines
Parameters
6
Backtest bar
5m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy combines moving averages and the Stochastic oscillator to implement an automated stock trading system. It uses two moving averages of different lengths and the Stochastic indicator to capture trend and overbought/oversold signals, and makes buy and sell decisions based on the trend direction and indicator signals in overbought/oversold regions.

Strategy Logic

1. Moving Averages

A fast line (5-day) and slow line (20-day) moving average are used. The fast line crossing above the slow line is a buy signal, while crossing below is a sell signal. The moving averages determine the price trend and direction.

2. Stochastic Oscillator

The Stochastic parameters are set to: K line period of 14, K line smooth period of 3, D line smooth period of 3. Below 20 on the K line is the oversold region, while above 80 is the overbought region. The Stochastic oscillator determines whether it is in overbought/oversold regions.

3. Entry Rules

Buy condition: Fast MA crossover above slow MA and K line <20 (oversold region) Sell condition: Fast MA crossover below slow MA and K line >80 (overbought region)

Go long when the buy condition is met; go short when the sell condition is met.

4. Stop Loss Settings

Set 1% profit target after buying; set 1% stop loss after selling.

Advantage Analysis

This strategy combines trend and indicators to effectively capture medium-to-long term price trends, while using the Stochastic oscillator to control the timing of trades and avoid random entries without a clear directional bias. The strategy parameters are adjustable to different market environments. Overall this strategy works very well on large/mid-cap stocks in uptrends.

Risks and Solutions

  • Price spikes from significant news events may incur large losses. Stop losses can control the risk.

  • Sustained range-bound markets may lead to consecutive small losses. Adjust the MA periods to reduce losses.

  • Avoid key market periods when prices tend to reverse.

Optimization

  • Test different parameter combinations to find optimal parameters, such as different MA lengths.

  • Incorporate other analysis tools like volume, volatility for filter conditions to improve profit rate.

  • Research stock selection mechanisms, like picking strong stocks or cap-weighted indices, to reduce single-stock risks.

Conclusion

The overall strategy operates smoothly. With stop losses and profit targets, the overall profit/loss profile is solid. Further improvements can be expected from parameter tuning and stock pool filtering. In general this is an easy-to-implement and robust quantitative trading strategy.

Parameters

NameDefaultWhat it controls
v_input_15Short MA Length
v_input_220Long MA Length
v_input_314Stochastic Length
v_input_43Stochastic %K
v_input_53Stochastic %D
v_input_6trueProfit Target (%)

Source Pine Script

/*backtest
start: 2024-01-25 00:00:00
end: 2024-02-01 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Moving Average and Stochastic Strategy 80% ", overlay=true)

// Moving Average Settings
maShortLength = input(5, title="Short MA Length")
maLongLength = input(20, title="Long MA Length")

// Stochastic Settings
stochLength = input(14, title="Stochastic Length")
smoothK = input(3, title="Stochastic %K")
smoothD = input(3, title="Stochastic %D")
stochOverbought = 80
stochOversold = 20

// Profit Target Settings
profitTarget = input(1, title="Profit Target (%)") // 1% profit target

// Calculate Moving Averages
maShort = sma(close, maShortLength)
maLong = sma(close, maLongLength)

// Calculate Stochastic
k = sma(stoch(close, high, low, stochLength), smoothK)
d = sma(k, smoothD)

// Entry Conditions
longConditionMA = crossover(maShort, maLong) and k < stochOversold
shortConditionMA = crossunder(maShort, maLong) and k > stochOverbought

// Opposite Conditions
oppositeLongConditionMA = crossunder(maShort, maLong) and k < stochOversold
oppositeShortConditionMA = crossover(maShort, maLong) and k > stochOverbought

// Strategy Logic
if (longConditionMA)
    strategy.entry("Buy", strategy.long)
    strategy.exit("Exit Buy", "Buy", profit=close * (50 + profitTarget / 100))

if (shortConditionMA)
    strategy.entry("Sell", strategy.short)
    strategy.exit("Exit Sell", "Sell", profit=close * (20 - profitTarget / 100))

// Opposite Strategy Logic
if (oppositeLongConditionMA)
    strategy.entry("Sell", strategy.short)
    strategy.exit("Exit Sell", "Sell", profit=close * (50 - profitTarget / 100))

if (oppositeShortConditionMA)
    strategy.entry("Buy", strategy.long)
    strategy.exit("Exit Buy", "Buy", profit=close * (20 + profitTarget / 100))

// Plot Moving Averages
plot(maShort, color=color.blue, title="Short MA")
plot(maLong, color=color.red, title="Long MA")

// Plot Stochastic
hline(stochOverbought, "Overbought", color=color.red)
hline(stochOversold, "Oversold", color=color.green)
plot(k, color=color.black, title="Stochastic %K")

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-02. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Moving-Average-and-Stochastic-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open