0bps

Strategy vault / Mean reversion

Moving Average and Stochastic RSI Combination Trading Strategy

This strategy combines the use of moving averages and Stochastic Relative Strength Index (Stochastic RSI) to find trading opportunities. Specifically, it looks at the medium-term moving average in an upward trend and…

Author
ChaoZhang
Complexity
Starter · 53 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy combines the use of moving averages and Stochastic Relative Strength Index (Stochastic RSI) to find trading opportunities. Specifically, it looks at the medium-term moving average in an upward trend and the overbought/oversold Stochastic RSI indicator to make trading decisions when both signals emerge. This combined use can filter out some false signals and improve the stability of the strategy.

Strategy Principle

The main components of this strategy are:

  1. Calculate two moving averages, MA1 and MA2, with different periods.

  2. Calculate the Stochastic Relative Strength Index (Stochastic RSI). This indicator incorporates RSI and stochastic principles to show whether the RSI is overbought or oversold.

  3. A buy signal is generated when stochastic RSI crosses above oversold threshold, while a sell signal is generated when it crosses below overbought threshold.

  4. Enter long when stochastic RSI signals aligned with the faster moving average above the slower one. This filters most false signals.

  5. Calculate the risk amount and position size. A fixed risk amount helps effectively control single loss.

  6. Set stop loss and take profit price. Trail stop profit to maximize profit.

Advantage Analysis

The strategy of combining moving average and stochastic RSI has the following advantages:

  1. It can yield good returns in trending markets. The combination of medium and long-term moving averages can determine the overall market trend direction.

  2. Stochastic RSI is useful in identifying overbought and oversold situations to catch reversal opportunities.

  3. Combination use filters out false signals and improves stability.

  4. The fixed risk percentage method manages risk by capping single loss below tolerance level.

  5. Stop loss and take profit lock in profits and limit downside risk.

Risk Analysis

There are also some risks to this strategy:

  1. In ranging markets, the combined moving averages may give false signals. Stop loss should be used to control risk.

  2. Stochastic RSI is sensitive to volatile price action and may also provide false signals occasionally. Combining with moving averages alleviates this.

  3. Fixed risk allocation cannot completely avoid large losses. Position sizing should be set appropriately.

  4. In extreme volatile scenarios, reasonable stop loss/profit prices are unavailable. Manual intervention is required then.

Optimization Directions

The strategy can be further optimized in the following aspects:

  1. Test more parameter combinations to find optimal periods. The current ones may not be the best.

  2. Try combining moving averages with other indicators like KDJ, MACD etc. Identify the best match.

  3. Test and optimize across different trading instruments. Currently optimized for FX trading.

  4. Employ machine learning models to dynamically optimize parameters over time against changing markets.

Conclusion

The moving average and stochastic RSI combination strategy identifies trend with moving averages and reversal levels with stochastic RSI to form trade signals, along with stop loss/profit and risk control to form a robust strategy logic. This simple and practical combination framework can be further tested and optimized across more instruments and parameter sets.

Parameters

NameDefaultWhat it controls
v_input_int_120MA1 Length
v_input_int_250MA2 Length
v_input_int_314Stochastic RSI Length
v_input_int_480Overbought Level
v_input_int_520Oversold Level
v_input_float_12Risk Percentage

Source Pine Script

/*backtest
start: 2023-01-09 00:00:00
end: 2024-01-15 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Moving Average and Stochastic RSI Strategy", shorttitle="MA+Stoch RSI", overlay=true)

// Input variables
ma1_length = input.int(20, title="MA1 Length")
ma2_length = input.int(50, title="MA2 Length")
stoch_length = input.int(14, title="Stochastic RSI Length")
overbought = input.int(80, title="Overbought Level")
oversold = input.int(20, title="Oversold Level")
risk_percentage = input.float(2.0, title="Risk Percentage")

// Calculate moving averages
ma1 = ta.sma(close, ma1_length)
ma2 = ta.sma(close, ma2_length)

// Calculate Stochastic RSI
rsi1 = ta.rsi(close, stoch_length)
rsiH = ta.highest(rsi1, stoch_length)
rsiL = ta.lowest(rsi1, stoch_length)
stoch = (rsi1 - rsiL) / (rsiH - rsiL) * 100

// Determine buy and sell signals based on Stochastic RSI
buySignal = ta.crossover(stoch, oversold)
sellSignal = ta.crossunder(stoch, overbought)

// Plot signals on the chart
plotshape(buySignal, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(sellSignal, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// Calculate position size based on equity and risk percentage
equity = strategy.equity
riskAmount = equity * risk_percentage / 100
positionSize = riskAmount / ta.atr(14)

// Entry and exit conditions
var float stopLoss = na
var float takeProfit = na

if buySignal
    stopLoss := low
    takeProfit := high
    strategy.entry("Buy", strategy.long)
else if sellSignal
    strategy.exit("Sell", from_entry="Buy", stop=stopLoss, limit=takeProfit)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-16. 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-RSI-Combination-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