Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. //@version=3
  2. //@John Zebley
  3. ////////////////////////////////////////////////////////////
  4. // Originally based on Average True Range Trailing Stops Strategy by HPotter with okadoke & Sylvain Vervoort's tweaks
  5. // Uses RSI oversold and overbought ranges for entries and exits along with ATR stoplosses and some other tricks
  6. // Trust the strat, become one with the strat
  7. ////////////////////////////////////////////////////////////
  8. strategy(title="ATR Trailing Stops Strategy", shorttitle="ATRTSS", overlay = true,
  9. initial_capital=4000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type="percent", commission_value=0.0)
  10.  
  11. //user settings
  12. nATRPeriod = input(9, "ATR Period")
  13. nATRMultip = input(3.5, "ATR Multiplier")
  14. useShorts = input(false, "Test w/Shorts?")
  15.  
  16.  
  17. emaentry =input(true, "Enter trades on ema crosses?")
  18. RSIentry =input(true, "RSI Knife Catch?")
  19. RSIatrmode =input(true, "ATR stoploss on RSI entries?")
  20. RSItrailing =input(true, "Trail RSI stop to cutoff?")
  21. RSIexit =input(true, "Exit trades on RSI multitouch?")
  22. takeProfit =input(false, "Take profit at fixed percent?")
  23. stopEMAPercent =input(0.5,"EMA entry Stop Loss Percent")
  24. stopRSIPercent =input(20.0,"RSI stop loss ATR shrink/fixed stop loss percent")
  25. trailingcuttoff =input(4.0, "Trailing Cuttoff Percent")
  26. profitPercent =input(57.0,"Fixed Take-Profit Percent")
  27. RSItouchesOS =input(2, "# of RSI touches for entries")
  28. RSItouchesOB =input(4, "# of RSI touches for exits")
  29. OBstoch =input(75,"Stochastic Danger zone")
  30. StochX =input(12, "Higher Time Frame Stoch Multiplier", minval = 1)
  31. RSIlevelOS =input(32, "OS RSI level", minval=0, maxval=100)
  32. RSIlevelOB =input(84, "OB RSI level", minval=0, maxval=100)
  33. RSIdelay =input(1, "RSI candle delay")
  34.  
  35.  
  36. emafast = ema(close, input(21,"EMA 1"))
  37. emaslow = ema(close, input(50,"EMA 2"))
  38.  
  39. k = sma(stoch(close, high, low, 14), 6)
  40. kdaily = sma(stoch(close, high, low, (StochX * 14)), 6)
  41. rsi14 = rsi(close, 14)
  42.  
  43.  
  44. //obtain values from last ticker
  45. entryPrice=0.0
  46. entryPrice:= nz(entryPrice[1])
  47.  
  48. profitPriceLevel=0.0
  49. profitPriceLevel:=nz(profitPriceLevel[1])
  50.  
  51. stopLossLevel=0.0
  52. stopLossLevel:=nz(stopLossLevel[1])
  53.  
  54. isProfitCatch=na
  55. isStopLoss=na
  56.  
  57. bullcross =na
  58. RSItrigger=na
  59.  
  60. calcStop=true
  61. calcStop:=nz(calcStop[1])
  62.  
  63. RSIcounterOS=na
  64. RSIcounterOS:=nz(RSIcounterOS[1])
  65.  
  66. RSIcounterOB=na
  67. RSIcounterOB:=nz(RSIcounterOB[1])
  68.  
  69.  
  70.  
  71. ///ATR code
  72. xATR = atr(nATRPeriod)
  73. nLoss = nATRMultip * xATR
  74. xATRTrailingStop = na
  75. xATRTrailingStop :=
  76. iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
  77. iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
  78. iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
  79.  
  80. pos = na
  81. pos :=
  82. iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
  83. iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
  84.  
  85. color = pos == -1 ? red: pos == 1 ? green : blue
  86. plot(xATRTrailingStop, color=color, linewidth=2, title="ATR Trailing Stop")
  87.  
  88.  
  89. //RSI code & ema triggers
  90. if crossunder(rsi14, RSIlevelOS)
  91. RSIcounterOS := RSIcounterOS + 1
  92.  
  93. if crossunder(rsi14, RSIlevelOB)
  94. RSIcounterOB := RSIcounterOB + 1
  95.  
  96. //BUY triggers
  97. bullcross:= (crossover(emafast, emaslow) and k < OBstoch and kdaily < OBstoch)
  98. RSItrigger:= (crossunder(rsi14[RSIdelay], RSIlevelOS) and kdaily < OBstoch and RSIcounterOS == RSItouchesOS)
  99. buy = (crossover(close, xATRTrailingStop) or (emaentry and bullcross) or (RSIentry and RSItrigger))
  100.  
  101.  
  102. //calculations for fixed take profit & fixed stop loss
  103. if (buy and takeProfit)
  104. entryPrice:=ohlc4
  105. profitPriceLevel := (1 + 0.01 * profitPercent) * entryPrice
  106.  
  107. if (buy and bullcross and calcStop)
  108. entryPrice:=ohlc4
  109. stopLossLevel := (1 - 0.01 * stopEMAPercent) * entryPrice
  110. calcStop:=false
  111.  
  112. if (buy and RSItrigger and calcStop)
  113. entryPrice:=ohlc4
  114. if RSIatrmode
  115. stopLossLevel := close - ((1 - 0.01 * stopRSIPercent) * nLoss)
  116. else
  117. stopLossLevel := (1 - 0.01 * stopRSIPercent) * entryPrice
  118. calcStop:=false
  119.  
  120.  
  121. //RSI trailing stop calculations
  122. if (RSItrailing and RSIatrmode and (close < xATRTrailingStop) and (stopLossLevel <= (entryPrice * (1 - 0.01 * trailingcuttoff))) and calcStop==false)
  123. if (close > nz(stopLossLevel[1], 0) and close[1] > nz(stopLossLevel[1], 0))
  124. stopLossLevel := max(nz(stopLossLevel[1]), (close - ((1 - 0.01 * stopRSIPercent) * nLoss)))
  125.  
  126. if close > xATRTrailingStop
  127. stopLossLevel :=na
  128.  
  129. if crossunder(close, xATRTrailingStop)
  130. stopLossLevel :=na
  131.  
  132. plot(stopLossLevel, title='stop loss', color=green, linewidth=2, style=area, transp=85)
  133.  
  134.  
  135. //SELL triggers
  136. isProfitCatch:= crossover(close, profitPriceLevel)
  137. isStopLoss:=(crossunder(low, stopLossLevel) and close < xATRTrailingStop)
  138. sell= (crossunder(close, xATRTrailingStop) or (takeProfit and isProfitCatch) or isStopLoss or (RSIexit and (crossover(rsi14[RSIdelay], RSIlevelOB) and RSIcounterOB == RSItouchesOB)))
  139.  
  140. if sell
  141. entryPrice:=na
  142. profitPriceLevel:=na
  143. stopLossLevel:=na
  144. isProfitCatch:= false
  145. isStopLoss:= false
  146. calcStop:=true
  147. RSIcounterOB:=0
  148. RSIcounterOS:=0
  149.  
  150.  
  151.  
  152. // === BACKTEST RANGE ===
  153. FromMonth = input(defval = 02, title = "From Month", minval = 1)
  154. FromDay = input(defval = 05, title = "From Day", minval = 1)
  155. FromYear = input(defval = 2019, title = "From Year", minval = 2019)
  156. ToMonth = input(defval = 1, title = "To Month", minval = 1)
  157. ToDay = input(defval = 1, title = "To Day", minval = 1)
  158. ToYear = input(defval = 9999, title = "To Year", minval = 2019)
  159.  
  160. testPeriod() =>
  161. (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
  162.  
  163.  
  164. strategy.entry("LONG", long=true, when=buy and testPeriod())
  165. strategy.close("LONG", when=sell and not useShorts and testPeriod())
  166. strategy.entry("SHORT", long=false, when=useShorts and sell and testPeriod())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement