Implementing a Dynamic Stop Loss Strategy in ProBuilder

11 Dec 2019
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to dynamically adjust a stop loss to breakeven plus a specified number of pips once a trade reaches a certain profit threshold. The example is specifically for setting a stop loss at breakeven plus 10 pips after the profit has reached 50 pips.


If not OnMarket then
    StopLoss = 0
Endif

If StopLoss = 0 and OnMarket then
    Profit = PositionPerf * PositionPrice
    If Profit > 50 * pipsize then
        If LongOnMarket then
            StopLoss = PositionPrice + 10 * pipsize
        Else
            StopLoss = PositionPrice - 10 * pipsize
        Endif
    Endif
Endif

If StopLoss > 0 then
    Sell at StopLoss STOP
    Exitshort at StopLoss STOP
Endif

Explanation of the Code:

  • Initial Condition: The code starts by checking if there is no open market position (not OnMarket). If true, it sets the StopLoss variable to 0, indicating that no stop loss has been set yet.
  • Calculating Profit: If there is an open position (OnMarket) and no stop loss has been set (StopLoss = 0), the code calculates the current profit of the position. This is done by multiplying the performance of the position (PositionPerf) by the position price (PositionPrice).
  • Setting Stop Loss: If the profit exceeds 50 pips (50 * pipsize), the stop loss is adjusted based on the type of position. For a long position (LongOnMarket), the stop loss is set to the entry price plus 10 pips. For a short position, it is set to the entry price minus 10 pips.
  • Executing Stop Orders: If a stop loss has been set (StopLoss > 0), the code places a stop order to sell for long positions or to exit short positions at the specified stop loss level. This ensures that the position is closed if the price reaches the stop loss level.

This snippet is useful for traders looking to protect their profits by adjusting the stop loss to a breakeven point as the market moves in their favor.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/set-stop-loss-in-profit-region-when-in-profit/#post-162498

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
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...