This code snippet demonstrates how to implement ATR (Average True Range) based trailing and breakeven stop mechanisms in trading strategies using the ProBuilder language. These stops are crucial for managing risk and protecting profits in trading systems.
atr trailing stop & atr breakeven stop. Both can be used.
// breakeven stop atr once breakevenstoptype = 1
// breakeven stop - 0 off, 1 on
once breakevenstoplong = 1
// breakeven stop atr relative distance
once breakevenstopshort = 1
// breakeven stop atr relative distance
once pointstokeep = 5
// positive or negative
once atrperiodbreakeven = 14
// atr parameter value
once minstopbreakeven = 10
// minimum breakeven stop distance
//----------------------------------------------
atrbreakeven = averagetruerange[atrperiodbreakeven]((close/10)*pipsize)/1000
//atrbreakeven=averagetruerange[atrperiodbreakeven]((close/1)*pipsize) // (forex)
bestopl = round(atrbreakeven*breakevenstoplong)
bestops = round(atrbreakeven*breakevenstopshort)
if breakevenstoptype = 1 then
// if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxpricebe = 0
minpricebe = close
newslbe = 0
endif
// if longonmarket then
maxpricebe = max(maxpricebe,close)
if maxpricebe-tradeprice(1)>=bestopl*pointsize then
if maxpricebe-tradeprice(1)>=minstopbreakeven then
newslbe=tradeprice(1)+pointstokeep*pipsize
else
newslbe=tradeprice(1)- minstopbreakeven*pointsize
endif
endif
endif
// if shortonmarket then
minpricebe = min(minpricebe,close)
if tradeprice(1)-minpricebe>=bestops*pointsize then
if tradeprice(1)-minpricebe>=minstopbreakeven then
newslbe = tradeprice(1)-pointstokeep*pipsize
else
newslbe = tradeprice(1) + minstopbreakeven*pointsize
endif
endif
endif
// if longonmarket then
if newslbe>0 then
sell at newslbe stop
endif
if newslbe>0 then
if low < newslbe then
sell at market
endif
endif
endif
// if shortonmarket then
if newslbe>0 then
exitshort at newslbe stop
endif
if newslbe>0 then
if high > newslbe then
exitshort at market
endif
endif
endif
//graphonprice newslbe coloured(255,165,0) as "breakevenstop atr"
endif
// trailing stop atr
once trailingstoptype = 1
// trailing stop - 0 off, 1 on
once trailingstoplong = 3
// trailing stop atr relative distance
once trailingstopshort = 3
// trailing stop atr relative distance
once atrtrailingperiod = 14
// atr parameter value
once minstop = 10
// minimum trailing stop distance
//----------------------------------------------
atrtrail = averagetruerange[atrtrailingperiod]((close/10)*pipsize)/1000
//atrtrail=averagetruerange[atrtrailingperiod]((close/1)*pipsize) (forex)
tgl = round(atrtrail*trailingstoplong)
tgs = round(atrtrail*trailingstopshort)
if trailingstoptype = 1 then
// if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice = 0
minprice = close
newsl = 0
endif
// if longonmarket then
maxprice = max(maxprice,close)
if maxprice-tradeprice(1)>=tgl*pointsize then
if maxprice-tradeprice(1)>=minstop then
newsl = maxprice-tgl*pointsize
else
newsl = maxprice - minstop*pointsize
endif
endif
endif
// if shortonmarket then
minprice = min(minprice,close)
if tradeprice(1)-minprice>=tgs*pointsize then
if tradeprice(1)-minprice>=minstop then
newsl = minprice+tgs*pointsize
else
newsl = minprice + minstop*pointsize
endif
endif
endif
// if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low < newsl then
sell at market
endif
endif
endif
// if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high > newsl then
exitshort at market
endif
endif
endif
//graphonprice newsl coloured(0,0,255,255) as "trailingstop atr"
endif
Explanation of the Code: