Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=2
- //strategy(title="Stochastic RSI", shorttitle="Stoch RSI Backtest")
- TopBand = input(80, step=0.01)
- LowBand = input(20, step=0.01)
- reverse = input(false, title="Trade reverse")
- hline(TopBand, color=red, linestyle=line)
- hline(LowBand, color=green, linestyle=line)
- Source = close
- lengthRSI = input(14, minval=1), lengthStoch = input(14, minval=1)
- smoothK = input(3, minval=1), smoothD = input(3, minval=1)
- rsi1 = rsi(Source, lengthRSI)
- k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
- d = sma(k, smoothD)
- d_cross_80 = cross(d,TopBand)
- dc80 = d_cross_80 ? red : green
- pos = iff(k > TopBand, 1,
- iff(k < LowBand, -1, nz(pos[1], 0)))
- possig = iff(reverse and pos == 1, -1,
- iff(reverse and pos == -1, 1, pos))
- //End of Stoch RSI
- //EMA crossover strategy
- strategy("EMA Fib Cross, 11 / 21", shorttitle="EMA Fib Cross, 11 / 21", overlay=true)
- //Input
- movingaverage_fast = ema(close, input(11))
- movingaverage_slow = ema(close, input(21))
- // Calculation
- bullish_cross = crossover(movingaverage_fast, movingaverage_slow)
- bearish_cross = crossunder(movingaverage_fast, movingaverage_slow)
- // Strategy
- if possig == -1 and movingaverage_fast > movingaverage_slow
- strategy.entry("long", strategy.long)
- strategy.close("long", when = possig == 1 )
- LongClosed = strategy.long and possig == 1
- if possig == 1 and movingaverage_fast < movingaverage_slow
- strategy.entry("short", strategy.short)
- strategy.close("short", when = possig == -1 )
- ShortClosed = strategy.short and possig == -1
- // Output
- plot(movingaverage_fast,color = blue,linewidth=2 )
- plot(movingaverage_slow,color = red,linewidth=2)
- //
- alertcondition(bullish_cross, title='EMA Cross (bullish)', message='Bullish')
- alertcondition(strategy.long, title='Buy Condition Met', message='Time to Buy')
- alertcondition(bearish_cross, title='EMA Cross (bearish)', message='Bearish')
- alertcondition(strategy.short, title='Sell Condition Met', message='Time to Sell')
- alertcondition(LongClosed, title='Long Position Needs To Close', message='Close Long Position')
- alertcondition(ShortClosed, title='Short Position Needs To Close', message='Close Short Position')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement