ATR Based Trailing Stop

ATR Based Trailing Stop With In Built Graph Function

// trailing stop atr
once enablets=1
once displayts=1
if enablets then
//
once steps=0.05
once minatrdist=3
once atrtrailingperiod    = 14   // atr parameter value
once minstop              = 10   // minimum trailing stop distance
if barindex=tradeindex then
trailingstoplong     = 5   // trailing stop atr relative distance
trailingstopshort    = 5   // trailing stop atr relative distance
elsif prezzouscita>0 then
if longonmarket then
if trailingstoplong>minatrdist then
if prezzouscita>prezzouscita[1] then
trailingstoplong=trailingstoplong
else
trailingstoplong=trailingstoplongsteps
endif
else
trailingstoplong=minatrdist
endif
endif
if shortonmarket then
if trailingstopshort>minatrdist then
if prezzouscita<prezzouscita[1] then
trailingstopshort=trailingstopshort
else
trailingstopshort=trailingstopshortsteps
endif
else
trailingstopshort=minatrdist
endif
endif
endif
//
atrtrail=averagetruerange[atrtrailingperiod]((close/10)*pipsize)/1000
trailingstartl=round(atrtrail*trailingstoplong)
trailingstarts=round(atrtrail*trailingstopshort)
tgl=trailingstartl
tgs=trailingstarts
//
if not onmarket  then
maxprice=0
minprice=close
prezzouscita=0
endif
//
if longonmarket then
maxprice=max(maxprice,close)
if maxpricetradeprice(1)>=tgl*pointsize then
if maxpricetradeprice(1)>=minstop then
prezzouscita=maxpricetgl*pointsize
else
prezzouscita=maxpriceminstop*pointsize
endif
endif
endif
//
if shortonmarket then
minprice=min(minprice,close)
if tradeprice(1)minprice>=tgs*pointsize then
if tradeprice(1)minprice>=minstop then
prezzouscita=minprice+tgs*pointsize
else
prezzouscita=minprice+minstop*pointsize
endif
endif
endif
//
if onmarket and prezzouscita>0 then
exitshort at prezzouscita stop
sell at prezzouscita stop
endif
if displayts then
graphonprice prezzouscita coloured(0,0,255,255) as “trailingstop atr”
endif
endif

Leave a Comment