Implementing ATR Trailing Stop and Dynamic Take Profit Levels in Trading Algorithms

18 Jul 2020
0 comment
0 attachment

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:

  • The code starts by setting up parameters for the ATR trailing stop and the big line take profit levels.
  • It checks the type of trade (long or short) and adjusts the trailing stop based on the ATR value and sensitivity settings.
  • Dynamic take profit levels are calculated based on the ‘big lines’ which are defined by a factor (e.g., every 100 points).
  • The code includes conditions to execute trades (sell or exit short) when the price crosses these dynamic levels or the trailing stop level.
  • Graphical representations of the start level and dynamic levels are plotted on the price chart for visual reference.

This snippet is a practical example of how to integrate complex trading strategies into automated trading systems using ProBuilder language.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/discussion-re-pure-renko-strategy/page/23/#post-137198

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
paul New
This author is like an anonymous function, present but not directly identifiable. More details on this code architect as soon as they exit 'incognito' mode.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...