This code snippet demonstrates how to implement an ATR (Average True Range) trailing stop mechanism along with dynamic take profit levels based on predefined price points (big lines) in a trading algorithm using the ProBuilder language. The code is structured to adjust stop levels and take profit points dynamically based on market conditions and the trader’s settings.
using atr trailingstop, but now includes a option to take profit near, at or above biglines biglines can be defined with factor i.e. 50 every 50 points, 100 = every 100 points. distance to take profit can be set. So if taken profit at every 100 lines (if trailingstop is active), it's possible to take it say 15 points earlier profit. Not really happy a.t.m. how things are rounded up/down and affect startlevel. Setup for 5s timeframe. once trailingstoptype = 1 once biglineclose = 1 // depended on trailing stop // trailing atr stop once tsatrperiod = 14 // ts atr parameter once tsminstop = 10 // ts minimum stop distance if tradetype=1 then once tsincrements = 0.0025 // set to 0 to ignore tsincrements //0.0025 once tsminatrdist = 1 once tssensitivity = 1 // [0]close;[1]high/low elsif (tradetype=2 or tradetype=3) then once tsincrements = 0 // set to 0 to ignore tsincrements once tsminatrdist = 0 once tssensitivity = 1 // [0]close;[1]high/low endif if trailingstoptype then if barindex=tradeindex then if tradetype=1 then trailingstoplong = 2 // ts atr distance trailingstopshort = 2 // ts atr distance elsif (tradetype=2 or tradetype=3) then trailingstoplong = 4 // ts atr distance trailingstopshort = 4 // ts atr distance endif else if longonmarket then if tsnewsl>0 then if trailingstoplong>tsminatrdist then if tsnewsl>tsnewsl[1] then trailingstoplong=trailingstoplong else trailingstoplong=trailingstoplong-tsincrements endif else trailingstoplong=tsminatrdist endif endif endif if shortonmarket then if tsnewsl>0 then if trailingstopshort>tsminatrdist then if tsnewsl=tgl*pointsize then if tsmaxprice-tradeprice(1)>=tsminstop then tsnewsl=tsmaxprice-tgl*pointsize else tsnewsl=tsmaxprice-tsminstop*pointsize endif endif endif if shortonmarket then tsminprice=min(tsminprice,tssensitivityshort) if tradeprice(1)-tsminprice>=tgs*pointsize then if tradeprice(1)-tsminprice>=tsminstop then tsnewsl=tsminprice+tgs*pointsize else tsnewsl=tsminprice+tsminstop*pointsize endif endif endif if longonmarket then if tsnewsl>0 then sell at tsnewsl stop endif if tsnewsl>0 then if low crosses under tsnewsl then sell at market endif endif endif if shortonmarket then if tsnewsl>0 then exitshort at tsnewsl stop endif if tsnewsl>0 then if high crosses over tsnewsl then exitshort at market endif endif endif if biglineclose then currentprice = round(tradeprice(1)) mpmod = 100 factor = 100 // defines lines abovelevel0 = currentprice mod mpmod startlevel = currentprice - abovelevel0 distance=0 //positive = more profit or negative = less profit if distance<0 then distancel=distance distances=-distance elsif distance>0 then distancel=distance distances=-distance else distancel=distance distances=distance endif level10up = (startlevel + (1*factor))+distancel level20up = (startlevel + (2*factor))+distancel level30up = (startlevel + (3*factor))+distancel level10down = (startlevel - (1*factor))+distances level20down = (startlevel - (2*factor))+distances level30down = (startlevel - (3*factor))+distances if longonmarket then if tsnewsl>0 then sell at startlevel limit sell at level10up limit sell at level20up limit sell at level30up limit endif endif if shortonmarket then if tsnewsl>0 then exitshort at startlevel limit exitshort at level10down limit exitshort at level20down limit exitshort at level30down limit endif endif endif endif graphonprice startlevel graphonprice level10down coloured(255,0,0,255) graphonprice level20down coloured(255,0,0,255) graphonprice level10up coloured(0,0,255,255) graphonprice level20up coloured(0,0,255,255)
Explanation of the Code:
This snippet is a practical example of how to integrate complex trading strategies into automated trading systems using ProBuilder language.
Check out this related content for more information:
https://www.prorealcode.com/topic/discussion-re-pure-renko-strategy/page/23/#post-137198
Visit Link