Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. //@version=2
  2.  
  3. //strategy(title="Stochastic RSI", shorttitle="Stoch RSI Backtest")
  4. TopBand = input(80, step=0.01)
  5. LowBand = input(20, step=0.01)
  6. reverse = input(false, title="Trade reverse")
  7. hline(TopBand, color=red, linestyle=line)
  8. hline(LowBand, color=green, linestyle=line)
  9. Source = close
  10. lengthRSI = input(14, minval=1), lengthStoch = input(14, minval=1)
  11. smoothK = input(3, minval=1), smoothD = input(3, minval=1)
  12. rsi1 = rsi(Source, lengthRSI)
  13. k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
  14. d = sma(k, smoothD)
  15. d_cross_80 = cross(d,TopBand)
  16. dc80 = d_cross_80 ? red : green
  17. pos = iff(k > TopBand, 1,
  18. iff(k < LowBand, -1, nz(pos[1], 0)))
  19. possig = iff(reverse and pos == 1, -1,
  20. iff(reverse and pos == -1, 1, pos))
  21.  
  22. //End of Stoch RSI
  23.  
  24. //EMA crossover strategy
  25. strategy("EMA Fib Cross, 11 / 21", shorttitle="EMA Fib Cross, 11 / 21", overlay=true)
  26.  
  27. //Input
  28. movingaverage_fast = ema(close, input(11))
  29. movingaverage_slow = ema(close, input(21))
  30.  
  31. // Calculation
  32. bullish_cross = crossover(movingaverage_fast, movingaverage_slow)
  33. bearish_cross = crossunder(movingaverage_fast, movingaverage_slow)
  34.  
  35. // Strategy
  36. if possig == -1 and movingaverage_fast > movingaverage_slow
  37. strategy.entry("long", strategy.long)
  38.  
  39. strategy.close("long", when = possig == 1 )
  40. LongClosed = strategy.long and possig == 1
  41.  
  42. if possig == 1 and movingaverage_fast < movingaverage_slow
  43. strategy.entry("short", strategy.short)
  44.  
  45. strategy.close("short", when = possig == -1 )
  46. ShortClosed = strategy.short and possig == -1
  47.  
  48. // Output
  49. plot(movingaverage_fast,color = blue,linewidth=2 )
  50. plot(movingaverage_slow,color = red,linewidth=2)
  51.  
  52. //
  53. alertcondition(bullish_cross, title='EMA Cross (bullish)', message='Bullish')
  54. alertcondition(strategy.long, title='Buy Condition Met', message='Time to Buy')
  55. alertcondition(bearish_cross, title='EMA Cross (bearish)', message='Bearish')
  56. alertcondition(strategy.short, title='Sell Condition Met', message='Time to Sell')
  57. alertcondition(LongClosed, title='Long Position Needs To Close', message='Close Long Position')
  58. alertcondition(ShortClosed, title='Short Position Needs To Close', message='Close Short Position')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement