Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 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", type=float, minval=0.5, maxval=1000, step=0.1)
  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. useshortstop =input(true, "use short stoploss after taking profit?")
  24. buydips =input(true, "buy dips after take profit?")
  25. stopEMAPercent =input(0.5,"EMA entry Stop Loss Percent")
  26. stopRSIPercent =input(20.0,"RSI stop loss ATR shrink/fixed stop loss percent")
  27. trailingcuttoff =input(4.0, "Trailing Cuttoff Percent")
  28. profitPercent =input(57.0,"Fixed Take-Profit Percent")
  29. RSItouchesOS =input(2, "# of RSI touches for entries")
  30. RSItouchesOB =input(4, "# of RSI touches for exits")
  31. OBstoch =input(75,"Stochastic Danger zone")
  32. StochX =input(12, "Higher Time Frame Stoch Multiplier", minval = 1)
  33. RSIlevelOS =input(32, "OS RSI level", minval=0, maxval=100)
  34. RSIlevelOB =input(84, "OB RSI level", minval=0, maxval=100)
  35. RSIdelay =input(1, "RSI candle delay")
  36.  
  37.  
  38. emafast = ema(close, input(21,"EMA 1"))
  39. emaslow = ema(close, input(50,"EMA 2"))
  40.  
  41. k = sma(stoch(close, high, low, 14), 6)
  42. kdaily = sma(stoch(close, high, low, (StochX * 14)), 6)
  43. rsi14 = rsi(close, 14)
  44.  
  45.  
  46. //obtain values from last ticker
  47. entryPrice=0.0
  48. entryPrice:= nz(entryPrice[1])
  49.  
  50. profitPriceLevel=0.0
  51. profitPriceLevel:=nz(profitPriceLevel[1])
  52.  
  53. stopLossLevel=0.0
  54. stopLossLevel:=nz(stopLossLevel[1])
  55.  
  56. isProfitCatch=na
  57. isStopLoss=na
  58.  
  59. bullcross =na
  60. RSItrigger=na
  61.  
  62. calcStop=true
  63. calcStop:=nz(calcStop[1])
  64.  
  65. RSIcounterOS=na
  66. RSIcounterOS:=nz(RSIcounterOS[1])
  67.  
  68. RSIcounterOB=na
  69. RSIcounterOB:=nz(RSIcounterOB[1])
  70.  
  71. RSItakeprofit=false
  72. RSItakeprofit:=nz(RSItakeprofit[1])
  73.  
  74. profitTaken=false
  75. profitTaken:=nz(profitTaken[1])
  76.  
  77. shortstop=0.0
  78. shortstop:=nz(shortstop[1])
  79.  
  80.  
  81. rebuy=na
  82.  
  83. ///ATR code
  84. xATR = atr(nATRPeriod)
  85. nLoss = nATRMultip * xATR
  86. xATRTrailingStop = na
  87. xATRTrailingStop :=
  88. iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
  89. iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
  90. iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
  91.  
  92. pos = na
  93. pos :=
  94. iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
  95. iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
  96.  
  97. color = pos == -1 ? red: pos == 1 ? green : blue
  98. plot(xATRTrailingStop, color=color, linewidth=2, title="ATR Trailing Stop")
  99.  
  100.  
  101. //RSI code & ema triggers
  102. if (crossunder(rsi14, RSIlevelOS) and close < xATRTrailingStop)
  103. RSIcounterOS := RSIcounterOS + 1
  104.  
  105. if (crossunder(rsi14, RSIlevelOB) and close > xATRTrailingStop)
  106. RSIcounterOB := RSIcounterOB + 1
  107.  
  108.  
  109.  
  110. //shortstop check
  111. if (crossover(close, shortstop) and close > xATRTrailingStop)
  112. shortstop:=na
  113. rebuy:=true
  114. profitTaken:=false
  115.  
  116. if (profitTaken and crossunder(close, xATRTrailingStop))
  117. shortstop:=na
  118. profitTaken:=false
  119.  
  120. if (buydips and profitTaken and close > xATRTrailingStop)
  121. if (low[3] <= xATRTrailingStop and low[2] > xATRTrailingStop and low[1] > xATRTrailingStop and low > xATRTrailingStop)
  122. rebuy:=true
  123. profitTaken:=false
  124.  
  125.  
  126.  
  127. //regular BUY triggers
  128. bullcross:= (crossover(emafast, emaslow) and k < OBstoch and kdaily < OBstoch and emaentry)
  129. RSItrigger:= (crossunder(rsi14[RSIdelay], RSIlevelOS) and kdaily < OBstoch and RSIcounterOS == RSItouchesOS)
  130. buy = (crossover(close, xATRTrailingStop) or (emaentry and bullcross) or (RSIentry and RSItrigger) or (buydips and rebuy) or (useshortstop and rebuy))
  131.  
  132.  
  133. //calculations for fixed take profit & fixed stop loss
  134. if (buy and takeProfit)
  135. entryPrice:=ohlc4
  136. profitPriceLevel := (1 + 0.01 * profitPercent) * entryPrice
  137.  
  138. if (buy and bullcross and calcStop)
  139. entryPrice:=ohlc4
  140. stopLossLevel := (1 - 0.01 * stopEMAPercent) * entryPrice
  141. calcStop:=false
  142.  
  143. if (buy and RSItrigger and calcStop)
  144. entryPrice:=ohlc4
  145. if RSIatrmode
  146. stopLossLevel := close - ((1 - 0.01 * stopRSIPercent) * nLoss)
  147. else
  148. stopLossLevel := (1 - 0.01 * stopRSIPercent) * entryPrice
  149. calcStop:=false
  150.  
  151.  
  152. //RSI trailing stop calculations
  153. if (RSItrailing and RSIatrmode and (close < xATRTrailingStop) and (stopLossLevel <= (entryPrice * (1 - 0.01 * trailingcuttoff))) and calcStop==false)
  154. if (close > nz(stopLossLevel[1], 0) and close[1] > nz(stopLossLevel[1], 0))
  155. stopLossLevel := max(nz(stopLossLevel[1]), (close - ((1 - 0.01 * stopRSIPercent) * nLoss)))
  156.  
  157. if close > xATRTrailingStop
  158. stopLossLevel :=na
  159.  
  160. if crossunder(close, xATRTrailingStop)
  161. stopLossLevel :=na
  162.  
  163. plot(stopLossLevel, title='stop loss', color=green, linewidth=2, style=area, transp=85)
  164.  
  165. if (RSIexit and (crossover(rsi14[RSIdelay], RSIlevelOB) and RSIcounterOB == RSItouchesOB))
  166. RSItakeprofit:=true
  167.  
  168. //SELL triggers
  169. isProfitCatch:=(crossover(close, profitPriceLevel) and close > xATRTrailingStop)
  170. isStopLoss:=(crossunder(low, stopLossLevel) and close < xATRTrailingStop)
  171. sell= (crossunder(close, xATRTrailingStop) or (takeProfit and isProfitCatch) or isStopLoss or RSItakeprofit)
  172.  
  173. if (RSItakeprofit or isProfitCatch)
  174. profitTaken:=true
  175.  
  176. if sell
  177. entryPrice:=na
  178. profitPriceLevel:=na
  179. stopLossLevel:=na
  180. isStopLoss:= false
  181. isProfitCatch:=false
  182. RSItakeprofit:=false
  183. calcStop:=true
  184. RSIcounterOB:=0
  185. RSIcounterOS:=0
  186. rebuy:=na
  187.  
  188.  
  189. //short stop loss after profit is taken)
  190. if (useshortstop and profitTaken and close > xATRTrailingStop)
  191. shortstop := close + nLoss
  192. if (close < nz(shortstop[1], 0) and close[1] < nz(shortstop[1], 0))
  193. shortstop := min(nz(shortstop[1]), (close + nLoss))
  194.  
  195. //(close + ((1 - 0.01 * stopRSIPercent) * nLoss))
  196.  
  197. plot(shortstop, "short stop", color=purple, linewidth=2, style=line, transp=20)
  198.  
  199.  
  200.  
  201.  
  202. // === BACKTEST RANGE ===
  203. FromMonth = input(defval = 02, title = "From Month", minval = 1)
  204. FromDay = input(defval = 05, title = "From Day", minval = 1)
  205. FromYear = input(defval = 2019, title = "From Year")
  206. ToMonth = input(defval = 1, title = "To Month", minval = 1)
  207. ToDay = input(defval = 1, title = "To Day", minval = 1)
  208. ToYear = input(defval = 9999, title = "To Year")
  209.  
  210. testPeriod() =>
  211. (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
  212.  
  213.  
  214. strategy.entry("LONG", long=true, when=(buy and testPeriod()))
  215. strategy.close("LONG", when=sell and not useShorts and testPeriod())
  216. strategy.entry("SHORT", long=false, when=useShorts and sell and testPeriod())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement