Implementing a Trailing Stop and Instant Reorder Strategy in ProBuilder

20 Jun 2024
0 comment
0 attachment

This ProBuilder script is designed to manage a trading position by implementing a trailing stop loss and instantly reopening a trade when certain conditions are met. The code snippet below demonstrates how to set up these trading controls.


Once TriggerPips = 175 * pipsize
Once CloseTrigger = 100 * pipsize
Once TPflag = 0 //will be set to 1 when TP reaches 175 pips

If not OnMarket then
    TPflag = 0
    MyProfit = 0
Endif

If LongOnMarket then
    MyProfit = Close - PositionPrice
Elsif ShortOnMarket then
    MyProfit = PositionPrice - close
Endif

If MyProfit >= TriggerPips Then
    TPflag = 1
Endif

If TPflag And MyProfit <= CloseTrigger Then
    TPflag = 0
    MyProfit = 0
    NewSL = 0
    If LongOnMarket Then
        Sell at Market
        Buy 1 Contract at Market
    Elsif ShortOnMarket Then
        Exitshort at Market
        Sellshort 1 contract at Market
    Endif
Endif

The code operates as follows:

  • Initialization: Variables TriggerPips and CloseTrigger are set to define the profit thresholds for the trailing stop and the reopening condition. TPflag is used as a flag to indicate whether the initial profit target has been reached.
  • Market Entry Check: If the strategy is not currently in the market, it resets TPflag and MyProfit to ensure clean slate calculations for new entries.
  • Profit Calculation: Depending on whether the position is long or short, MyProfit calculates the unrealized profit from the current position.
  • Trailing Stop Activation: If the profit exceeds TriggerPips, TPflag is set to 1, activating the trailing stop mechanism.
  • Position Management: If TPflag is active and profit drops below CloseTrigger, the position is closed at market price, and a new position is immediately opened in the same direction, effectively resetting the strategy.

This script is useful for traders looking to capitalize on favorable market movements while protecting gains with a trailing stop, and immediately re-entering the market upon retracement.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/trailing-stop-function-to-take-profit-during-retracement/#post-145492

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
134
strategy
171

Recent Snippets

How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
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 [...]
Logo Logo
Loading...