Implementing a Dynamic Trailing Stop Using ATR in ProBuilder

14 Apr 2017
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop strategy for both long and short positions using the Average True Range (ATR) in ProBuilder. The trailing stop adjusts based on the ATR, which measures market volatility.


// trailing stop atr [adjusted reset] 
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=trailingstoplong-steps 
                endif 
            else 
                trailingstoplong=minatrdist 
            endif 
        endif 
        if shortonmarket then 
            if trailingstopshort>minatrdist then 
                if prezzouscita=tgl*pointsize then 
        if maxprice-tradeprice(1)>=minstop then 
            prezzouscita=maxprice-tgl*pointsize 
        else 
            prezzouscita=maxprice-minstop*pointsize 
        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 
    // 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

Explanation of the Code:

  • The code starts by setting initial conditions such as enabling the trailing stop and display settings.
  • It defines parameters like the minimum ATR distance, ATR period, and minimum stop distance.
  • For both long and short market positions, it adjusts the trailing stop based on the previous exit price and the current market conditions.
  • The ATR is used to calculate the trailing stop distance dynamically, which helps in adjusting the stop loss according to market volatility.
  • Conditions to exit the market are set based on the calculated trailing stop values.
  • If enabled, the trailing stop value is displayed on the price graph.

This implementation helps in managing risk by dynamically adjusting the stop loss levels based on market conditions, providing a more flexible and potentially more effective risk management tool.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/vectorial-dax-m5/page/39/#post-114921

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
Operating in the shadows, I hack problems one by one. My bio is currently encrypted by a complex algorithm. Decryption underway...
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...