TradingView
Trendoscope
3 dec 2021 13:18

Supertrend - Ladder ATR 

USD/TRYOANDA

Beskrivning

This is a supertrend with slight twisted concept which can be very benefecial in strong trending markets to reduce stop loss distance and exit slightly quicker.

⬜ Concept
▶ When the instrument is trending up, regular ATR shows high values if there are big green candles. This affect the stoploss distance in regular supertrend which leads to wide stops or delayed lagging. When you are in long trade, what matters for stoploss is how much a negative candle can move within bar. Hence, using ATR derived only based on red candles is more beneficial for trailing stops on long signals. Same applies to short trades where using ATR derived from only green candles is more efficient than overall ATR.
▶ ATR will be minimal when the volatility is less and ATR will increase with volatility. That means, once you are in trade, the trailing of stoploss also will vary based on ATR (or volatility). With regular ATR and supertrend, chances of stop loss distance widening is high with increased volatility even though stoploss levels will not move down. This again poses the risk of higher drawdown during trade closure and also keeps in the trade during ranging market. To avoid this, the second trick we are using here is only to reduce the atr stoploss difference when in trade. That is, when in long trade and negative candles ATR is increasing, we will not consider that. We will consider the new ATR only if it is lesser than previous bar ATR.

Effect of these changes on the trending market is quite visual. Lets take example of USDTRY



Settings are quite simple and does not vary much from regular supertrend settings.

Versionsinformation

Updated libraries.
Kommentarer
PineCoders
Vibranium_Capital
Lovely idea dear
Trendoscope
@Vibranium_Capital, thanks for your support :)
ragar25439
@HeWhoMustNotBeNamed, sir one idea from my side , too many stop loss hit in ranging market so if stop loss is wide while ranging market , we save our money too . thankyou sir
Arina_chm
Hello, it's great job, but is it possible to add timeframe option, for example to choose 1H timeframe when I see 15M timeframe please?
Trendoscope
@Arina_chm, at present don't have Multi timeframe option. Will consider it in future updates.
X-Algo
Try it with the rma in 26 😉
sahilmorey06
@TheDragonTraderOfWallStreet, Timeframe?
montealegre60
Congratulations, great idea. Does the modified supertrend work on smaller timeframes (ie, 5m, 15m or 1hr) or just on 1D?
TradingRookieDog
I tried to modify this indicator to a strategy, but it didn't show any long/short signal on the graph, and there's no error showed in the console, could you help me find out the problem?

strategy("Supertrend - Ladder ATR", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
import HeWhoMustNotBeNamed/arrayutils/3 as pa

matype = input.string("sma", title="Type", group="ATR", options=["sma", "ema", "rma", "wma", "hma"])
malength = input.int(20, title="Length", group="ATR")

multiplier = input.int(4, "Multiplier", step=1, group="Supertrend")
waitForClose = input.bool(false, "Wait For Close", group="Supertrend")
delayed = input.bool(false, "Delayed/Sticky", group="Supertrend")

supertrend_atr(float positiveAtr, float negativeAtr, simple float multiplier, simple bool waitForClose = false, simple bool delayed = false) =>
var dir = 1
lowSource = low
highSource = high
source = close
buyStopDiff = negativeAtr*multiplier
sellStopDiff = positiveAtr*multiplier
buyStopDiff := (dir == 1? math.min(buyStopDiff, nz(buyStopDiff[1], buyStopDiff)) : buyStopDiff)
sellStopDiff := (dir == -1? math.min(sellStopDiff, nz(sellStopDiff[1], sellStopDiff)) : sellStopDiff)
var buyStop = lowSource - buyStopDiff
var sellStop = highSource + sellStopDiff

buyStopCurrent = lowSource - buyStopDiff
sellStopCurrent = highSource + sellStopDiff

buyStopInverse = lowSource - buyStopDiff/2
sellStopInverse = highSource + sellStopDiff/2

highConfirmation = waitForClose ? source : highSource
lowConfirmation = waitForClose? source : lowSource
dir := dir == 1 and lowConfirmation[1] < buyStop[1]? -1 : dir == -1 and highConfirmation[1] > sellStop[1]? 1 : dir
targetReached = (dir == 1 and nz(highConfirmation[1]) >= nz(sellStop[1])) or (dir == -1 and nz(lowConfirmation[1]) <= nz(buyStop[1])) or not delayed
buyStop := dir == 1? (targetReached? math.max(nz(buyStop, buyStopCurrent), buyStopCurrent): buyStop): targetReached ? buyStopCurrent : math.max(nz(buyStop, buyStopInverse), buyStopInverse)
sellStop := dir == -1? (targetReached? math.min(nz(sellStop, sellStopCurrent), sellStopCurrent): sellStop): targetReached? sellStopCurrent : math.min(nz(sellStop, sellStopInverse), sellStopInverse)
[dir, dir > 0? buyStop : sellStop]

var positiveTrArray = array.new_float()
var negativeTrArray = array.new_float()

if(open < close)
pa.push_to_float_array(positiveTrArray, ta.tr, malength)
else
pa.push_to_float_array(negativeTrArray, ta.tr, malength)

positiveAtr = pa.ma(positiveTrArray, matype, malength)
negativeAtr = pa.ma(negativeTrArray, matype, malength)

[dir, supertrend] = supertrend_atr(positiveAtr, negativeAtr, multiplier, waitForClose, delayed)
plot(supertrend, color=dir>0? color.green : color.red, title="Supertrend Stop")
plot(dir)
strategy.entry("Buy", strategy.long, when = dir > 0.0)

strategy.close("Sell", when = dir < 0.0)
Mer