Implementing a Dynamic Trailing Stop Using ATR in ProBuilder

21 May 2017
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop based on the Average True Range (ATR) for managing positions in trading strategies. The trailing stop adjusts according to the market’s volatility, measured by ATR, to protect profits while giving enough room for the trade to breathe.


// ================trailing atr stop VIRTUAL==================
if enabletsvir then
    // once stepsvir=0
    // once minatrdistvir=0
    // once atrtrailingperiodvir = 2 // atr parameter
    // once minstopvir = 10 // minimum distance
    if barindex=tradeindex then
        trailingstoplongvir = 5 // trailing stop atr distance
        trailingstopshortvir = 5 // trailing stop atr distance
    else
        if longonmarket then
            if newslvir>0 then
                if trailingstoplongvir>minatrdistvir then
                    if newslvir>newslvir[1] then
                        trailingstoplongvir=trailingstoplongvir
                    else
                        trailingstoplongvir=trailingstoplongvir-stepsvir
                    endif
                else
                    trailingstoplongvir=minatrdistvir
                endif
            endif
        endif
        if shortonmarket then
            if newslvir>0 then
                if trailingstopshortvir>minatrdistvir then
                    if newslvir=tglvir*pointsize then
            if maxpricevir-tradeprice(1)>=minstopvir then
                newslvir=maxpricevir-tglvir*pointsize
            else
                newslvir=maxpricevir-minstopvir*pointsize
            endif
        endif
    endif
    // if shortonmarket then
        minpricevir=min(minpricevir,close)
        if tradeprice(1)-minpricevir>=tgsvir*pointsize then
            if tradeprice(1)-minpricevir>=minstopvir then
                newslvir=minpricevir+tgsvir*pointsize
            else
                newslvir=minpricevir+minstopvir*pointsize
            endif
        endif
    endif
    // if longonmarket and close 0 then
        sell at market
    endif
    if shortonmarket and close>newslvir and newslvir>0 then
        exitshort at market
    endif

Explanation of the Code:

  • The code starts by checking if the trailing stop feature is enabled.
  • Initial settings are defined using once statements for parameters like steps, minimum ATR distance, ATR period, and minimum stop distance.
  • For each trade, it checks if the market is long or short and adjusts the trailing stop based on the ATR and the price movement.
  • The ATR is calculated and used to determine the trailing stop distance dynamically, which adjusts as the market’s volatility changes.
  • Conditions are set to exit the market if the price crosses the dynamically calculated trailing stop level.

This snippet is a practical example of how to manage risk dynamically in a trading strategy using market volatility indicators like ATR in the ProBuilder language.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/strategy-barhunter-dax-v1p/page/4/#post-117543

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.
fifi743 Master
patience ,patience comme en ski de rando
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...