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

Comments

Search Snippets

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

Snippets Categories

global
33
indicator
132
strategy
170

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Implementing Single Trade Per Timeframe Logic in ProBuilder
strategy
This code snippet demonstrates how to ensure that only one trade is executed per timeframe in a trading strategy using [...]
Logo Logo
Loading...