Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- //@John Zebley
- ////////////////////////////////////////////////////////////
- // Originally based on Average True Range Trailing Stops Strategy by HPotter with okadoke & Sylvain Vervoort's tweaks
- // Uses RSI oversold and overbought ranges for entries and exits along with ATR stoplosses and some other tricks
- // Trust the strat, become one with the strat
- ////////////////////////////////////////////////////////////
- strategy(title="ATR Trailing Stops Strategy", shorttitle="ATRTSS", overlay = true,
- initial_capital=4000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type="percent", commission_value=0.0)
- //user settings
- nATRPeriod = input(9, "ATR Period")
- nATRMultip = input(3.5, "ATR Multiplier", type=float, minval=0.5, maxval=1000, step=0.1)
- useShorts = input(false, "Test w/Shorts?")
- emaentry =input(true, "Enter trades on ema crosses?")
- RSIentry =input(true, "RSI Knife Catch?")
- RSIatrmode =input(true, "ATR stoploss on RSI entries?")
- RSItrailing =input(true, "Trail RSI stop to cutoff?")
- RSIexit =input(true, "Exit trades on RSI multitouch?")
- takeProfit =input(false, "Take profit at fixed percent?")
- useshortstop =input(true, "use short stoploss after taking profit?")
- buydips =input(true, "buy dips after take profit?")
- stopEMAPercent =input(0.5,"EMA entry Stop Loss Percent")
- stopRSIPercent =input(20.0,"RSI stop loss ATR shrink/fixed stop loss percent")
- trailingcuttoff =input(4.0, "Trailing Cuttoff Percent")
- profitPercent =input(57.0,"Fixed Take-Profit Percent")
- RSItouchesOS =input(2, "# of RSI touches for entries")
- RSItouchesOB =input(4, "# of RSI touches for exits")
- OBstoch =input(75,"Stochastic Danger zone")
- StochX =input(12, "Higher Time Frame Stoch Multiplier", minval = 1)
- RSIlevelOS =input(32, "OS RSI level", minval=0, maxval=100)
- RSIlevelOB =input(84, "OB RSI level", minval=0, maxval=100)
- RSIdelay =input(1, "RSI candle delay")
- emafast = ema(close, input(21,"EMA 1"))
- emaslow = ema(close, input(50,"EMA 2"))
- k = sma(stoch(close, high, low, 14), 6)
- kdaily = sma(stoch(close, high, low, (StochX * 14)), 6)
- rsi14 = rsi(close, 14)
- //obtain values from last ticker
- entryPrice=0.0
- entryPrice:= nz(entryPrice[1])
- profitPriceLevel=0.0
- profitPriceLevel:=nz(profitPriceLevel[1])
- stopLossLevel=0.0
- stopLossLevel:=nz(stopLossLevel[1])
- isProfitCatch=na
- isStopLoss=na
- bullcross =na
- RSItrigger=na
- calcStop=true
- calcStop:=nz(calcStop[1])
- RSIcounterOS=na
- RSIcounterOS:=nz(RSIcounterOS[1])
- RSIcounterOB=na
- RSIcounterOB:=nz(RSIcounterOB[1])
- RSItakeprofit=false
- RSItakeprofit:=nz(RSItakeprofit[1])
- profitTaken=false
- profitTaken:=nz(profitTaken[1])
- shortstop=0.0
- shortstop:=nz(shortstop[1])
- rebuy=na
- ///ATR code
- xATR = atr(nATRPeriod)
- nLoss = nATRMultip * xATR
- xATRTrailingStop = na
- xATRTrailingStop :=
- iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
- iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
- iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
- pos = na
- pos :=
- iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
- iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
- color = pos == -1 ? red: pos == 1 ? green : blue
- plot(xATRTrailingStop, color=color, linewidth=2, title="ATR Trailing Stop")
- //RSI code & ema triggers
- if (crossunder(rsi14, RSIlevelOS) and close < xATRTrailingStop)
- RSIcounterOS := RSIcounterOS + 1
- if (crossunder(rsi14, RSIlevelOB) and close > xATRTrailingStop)
- RSIcounterOB := RSIcounterOB + 1
- //shortstop check
- if (crossover(close, shortstop) and close > xATRTrailingStop)
- shortstop:=na
- rebuy:=true
- profitTaken:=false
- if (profitTaken and crossunder(close, xATRTrailingStop))
- shortstop:=na
- profitTaken:=false
- if (buydips and profitTaken and close > xATRTrailingStop)
- if (low[3] <= xATRTrailingStop and low[2] > xATRTrailingStop and low[1] > xATRTrailingStop and low > xATRTrailingStop)
- rebuy:=true
- profitTaken:=false
- //regular BUY triggers
- bullcross:= (crossover(emafast, emaslow) and k < OBstoch and kdaily < OBstoch and emaentry)
- RSItrigger:= (crossunder(rsi14[RSIdelay], RSIlevelOS) and kdaily < OBstoch and RSIcounterOS == RSItouchesOS)
- buy = (crossover(close, xATRTrailingStop) or (emaentry and bullcross) or (RSIentry and RSItrigger) or (buydips and rebuy) or (useshortstop and rebuy))
- //calculations for fixed take profit & fixed stop loss
- if (buy and takeProfit)
- entryPrice:=ohlc4
- profitPriceLevel := (1 + 0.01 * profitPercent) * entryPrice
- if (buy and bullcross and calcStop)
- entryPrice:=ohlc4
- stopLossLevel := (1 - 0.01 * stopEMAPercent) * entryPrice
- calcStop:=false
- if (buy and RSItrigger and calcStop)
- entryPrice:=ohlc4
- if RSIatrmode
- stopLossLevel := close - ((1 - 0.01 * stopRSIPercent) * nLoss)
- else
- stopLossLevel := (1 - 0.01 * stopRSIPercent) * entryPrice
- calcStop:=false
- //RSI trailing stop calculations
- if (RSItrailing and RSIatrmode and (close < xATRTrailingStop) and (stopLossLevel <= (entryPrice * (1 - 0.01 * trailingcuttoff))) and calcStop==false)
- if (close > nz(stopLossLevel[1], 0) and close[1] > nz(stopLossLevel[1], 0))
- stopLossLevel := max(nz(stopLossLevel[1]), (close - ((1 - 0.01 * stopRSIPercent) * nLoss)))
- if close > xATRTrailingStop
- stopLossLevel :=na
- if crossunder(close, xATRTrailingStop)
- stopLossLevel :=na
- plot(stopLossLevel, title='stop loss', color=green, linewidth=2, style=area, transp=85)
- if (RSIexit and (crossover(rsi14[RSIdelay], RSIlevelOB) and RSIcounterOB == RSItouchesOB))
- RSItakeprofit:=true
- //SELL triggers
- isProfitCatch:=(crossover(close, profitPriceLevel) and close > xATRTrailingStop)
- isStopLoss:=(crossunder(low, stopLossLevel) and close < xATRTrailingStop)
- sell= (crossunder(close, xATRTrailingStop) or (takeProfit and isProfitCatch) or isStopLoss or RSItakeprofit)
- if (RSItakeprofit or isProfitCatch)
- profitTaken:=true
- if sell
- entryPrice:=na
- profitPriceLevel:=na
- stopLossLevel:=na
- isStopLoss:= false
- isProfitCatch:=false
- RSItakeprofit:=false
- calcStop:=true
- RSIcounterOB:=0
- RSIcounterOS:=0
- rebuy:=na
- //short stop loss after profit is taken)
- if (useshortstop and profitTaken and close > xATRTrailingStop)
- shortstop := close + nLoss
- if (close < nz(shortstop[1], 0) and close[1] < nz(shortstop[1], 0))
- shortstop := min(nz(shortstop[1]), (close + nLoss))
- //(close + ((1 - 0.01 * stopRSIPercent) * nLoss))
- plot(shortstop, "short stop", color=purple, linewidth=2, style=line, transp=20)
- // === BACKTEST RANGE ===
- FromMonth = input(defval = 02, title = "From Month", minval = 1)
- FromDay = input(defval = 05, title = "From Day", minval = 1)
- FromYear = input(defval = 2019, title = "From Year")
- ToMonth = input(defval = 1, title = "To Month", minval = 1)
- ToDay = input(defval = 1, title = "To Day", minval = 1)
- ToYear = input(defval = 9999, title = "To Year")
- testPeriod() =>
- (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
- strategy.entry("LONG", long=true, when=(buy and testPeriod()))
- strategy.close("LONG", when=sell and not useShorts and testPeriod())
- strategy.entry("SHORT", long=false, when=useShorts and sell and testPeriod())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement