Implementing a Dynamic Trailing Stop Strategy in ProBuilder

04 Sep 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop strategy in the ProBuilder language, used within the ProRealTime trading platform. The strategy is designed to adjust the stop loss level dynamically based on the maximum price achieved since entry (Maximum Favorable Excursion or MFE). It is particularly useful for managing open positions and locking in profits while allowing room for price movement.

DEFPARAM CumulateOrders = False
trailingstop1 = 40
bottomTrailingStopValue1 = 30
TopTrailingStopValue1 = 70

if not onmarket then
    MAXPRICE1 = 0
    priceexit1 = 0
endif

a = close-tradeprice(1) > bottomTrailingStopValue1*pointsize
b = close-tradeprice(1) < TopTrailingStopValue1*pointsize

if longonmarket then
    MAXPRICE1 = MAX(MAXPRICE1,close) //saving the MFE of the current trade
    if a and b then
        if MAXPRICE1-tradeprice(1)>=trailingstop1*pointsize then //if the MFE is higher than the trailingstop
            priceexit1 = MAXPRICE1-trailingstop1*pointsize //set the exit price at the MFE - trailing stop price level
        endif
    endif
endif

if onmarket and priceexit1>0 then
    EXITSHORT AT priceexit1 STOP
    SELL AT priceexit1 STOP
endif

IF close CROSSES OVER average[20,0](close) THEN
    BUY AT MARKET
    SET TARGET pPROFIT 250
    SET STOP pLOSS 200
ENDIF

graph a
graph b
graph MAXPRICE1
graph priceexit1
graph MAXPRICE1-tradeprice

Explanation of the Code:

  • Initialization: Sets up parameters for the trailing stop and initializes variables when the strategy is not in the market.
  • Condition Checks: Defines two conditions a and b to check if the current close price is within a specified range from the trade entry price, adjusted for point size.
  • Dynamic Trailing Stop Adjustment: If the strategy holds a long position, it updates the maximum price (MFE) and adjusts the trailing stop level dynamically based on this maximum price.
  • Market Exit: Executes an exit for short positions or sells at the dynamically calculated stop price if certain conditions are met.
  • Entry Signal: A buy signal is generated when the close price crosses over a 20-period moving average, setting predefined profit and loss targets.
  • Graphical Output: Visualizes the conditions and values for analysis, including the maximum price since entry and the calculated exit price.

This snippet is a practical example of how to manage trades dynamically to potentially increase profitability while controlling downside risk.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/need-help-with-fx-trail/#post-73718

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...