Dual MA Stoch Indicators Combo Trading Strategy

Author: ChaoZhang, Date: 2023-09-12 14:44:56
Tags:

This strategy combines moving averages and Stoch indicators into a quantitative trading system with both trend-following and overbought/oversold detection capabilities. It integrates the strengths of multiple indicators for systematic trend identification and opportunity capture.

Strategy Logic:

  1. Calculate long-term MA and short-term EMA to determine trend direction.

  2. Calculate Stoch K and D values to identify overbought/oversold levels.

  3. When CLOSE breaks above MA, and Stoch K&D are above overbought line, go long.

  4. When CLOSE breaks below EMA, and Stoch K&D are below oversold line, go short.

  5. Use COLOR to mark determined trade direction.

Advantages:

  1. Dual MAs improve trend accuracy and avoid false signals.

  2. Stoch identifies high-probability buy/sell zones.

  3. Combining indicators improves reliability through verification.

Risks:

  1. Poor parameter tuning causes excessive signals or inconsistencies.

  2. Both MAs and Stoch can lag, causing premature or delayed entries.

  3. More indicators mean more complexity despite improved reliability.

In summary, this strategy quantitatively trades using MAs for trend and Stoch for overbought/oversold levels. With robust parameter optimization, it can improve system stability and reliability. But prudent risk management is still required, and investors should apply discretion.


/*backtest
start: 2023-08-12 00:00:00
end: 2023-09-11 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

// strategy("PMB2", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 20, initial_capital=1000, currency=currency.USD)
//study(title="PMB2", overlay=true)

l_ma = input(50, title="MA (green)", type=input.integer)
l_ema = input(25, title="EMA (red)", type=input.integer)

MA = sma(close,l_ma)
EMA = ema(close,l_ema)

plot(MA, color=color.green)
plot(EMA, color=color.red)

//STOCH(14,3,3)
length = input(20, minval=1, title="STOCH - K")
smoothK = input(2, minval=1, title="STOCH - D")
smoothD = input(2 , minval=1, title="STOCH - Smooth")

StkLong= input(50 , minval=1, maxval=100, title="Long when Close > MA and Stoch > ")
StkShort= input(80 , minval=1, maxval=100, title="Short when Close < EMA and Stoch < ")

k = sma(stoch(close, high, low, length), smoothK)
d = sma(k, smoothD)
//plot(k, color=color.blue, title="STOCH - K")
//plot(d, color=color.orange, title="STOCH - D")
//band180 = hline(80, title="STOCH - Banda superior")
//band120 = hline(20, title="STOCH - Banda superior")
//band100 = hline(50,  color=color.gray, editable=false, linestyle=hline.style_solid)
//fill(band180, band120, color=color.gray, transp=75, title="STOCH - Fundo")

BTStartY = input(title="Strategy Test Start Year", type=input.integer, defval=2019, minval=2010, maxval=2100)
BTStartM = input(title="Strategy Test Start Month", type=input.integer, defval=1, minval=1, maxval=12)
BTStartD = input(title="Strategy Test Start Day", type=input.integer, defval=1, minval=1, maxval=31)
BTStopY = input(title="Strategy Test Stop Year", type=input.integer, defval=2019, minval=2010, maxval=2100)
BTStopM = input(title="Strategy Test Stop Month", type=input.integer, defval=12, minval=1, maxval=12)
BTStopD = input(title="Strategy Test Stop Day", type=input.integer, defval=31, minval=1, maxval=31)

// set up min and max date for strategy test
TMin = timestamp(BTStartY, BTStartM, BTStartD, 00, 00)
TMax = timestamp(BTStopY, BTStopM, BTStopD, 00, 00)
InTime = true

bool long = false, short = false, trade = false

trade := trade[1]
long := long[1]
short := short[1]

if (crossover(close, MA) and k > StkLong and d > StkLong) // "LONG!"
//if (close > MA and k > StkLong and d > StkLong) // "LONG!"
    short := false
    long := true
    trade := true // LONG

if (crossunder(close, EMA)  and k < StkShort and d < StkShort) // "SHORT!""
//if (close < EMA and k < StkShort and d < StkShort) // "SHORT!""
    long := false
    short := true
    trade := false // SHORT


//bgcolor(FL > SH ? color.green : FH < SL ? color.red : na, transp=80)
bgcolor(trade ? color.green : color.red, transp=90)

//alertcondition((crossover(close, MA) and k > 50 and d > 50) , title='Buy', message='Buy')
//alertcondition((crossunder(close, EMA) and k > 80 and d > 80) , title='Sell', message='Sell')


if ((crossover(close, MA) and k > StkLong and d > StkLong) and InTime)
//if ((close > MA and k > StkLong and d > StkLong) and InTime)
    strategy.entry("Long", strategy.long)

if ((crossunder(close, EMA) and k < StkShort and d < StkShort)  and InTime)
//if ((close < EMA and k < StkShort and d < StkShort)  and InTime)
    strategy.entry("Short", strategy.short)
    





More