Implementing Breakeven and Trailing Stop Using Percentage in ProBuilder

20 Oct 2021
0 comment
0 attachment

This code snippet demonstrates how to implement a breakeven and trailing stop strategy using percentage values in ProBuilder language. The strategy adjusts the stop loss level once a trade reaches a specified profit percentage, helping to protect gains or minimize losses.


BreakEvenStop = 1 // BreakEvenStop and BreakEvenStop Minimum Gain
bs = 1.00 // % BreakEvenStop
bsm= 0.10 // % BreakEvenStop Minimum Gain

if tcShort and tcxShort then
    if (s0 or s1 or s2 or s3 or s4 or s5 or s6 or s7 or s8 or s9) then
        sellshort positionsize contract at market
        shorttradecounter=shorttradecounter + 1
        If BreakEvenStop then
            SET STOP %LOSS bsm
        endif
    endif
endif

// BreakEvenStop
If BreakEvenStop then
    if not onmarket then
        newSL=0
    endif
    If longonmarket and close-tradeprice(1)>=((tradeprice/100)*bs)*pipsize then
        newSL = tradeprice(1)+((tradeprice/100)*bsm)*pipsize
    endif
    If shortonmarket and tradeprice(1)-close>=((tradeprice/100)*bs)*pipsize then
        newSL = tradeprice(1)-((tradeprice/100)*bsm)*pipsize
    endif
    If newSL>0 then
        sell at newSL Stop
        exitshort at newSL Stop
    endif
endif

The code snippet above is structured to manage trading positions by setting breakeven and trailing stops based on percentage gains:

  • Initialization: Variables BreakEvenStop, bs (BreakEvenStop percentage), and bsm (BreakEvenStop Minimum Gain percentage) are set.
  • Entry Condition: A short position is initiated if certain conditions (tcShort and tcxShort) are met and any of the signals from s0 to s9 are true. The trade counter is incremented, and an initial stop loss is set as a percentage of the entry price.
  • Stop Adjustment: The stop loss level (newSL) is recalculated based on the trade’s profit reaching the defined percentage (bs). If the condition is met, newSL is adjusted to either lock in a profit or reduce the loss, depending on the market direction (long or short).
  • Execution: If a new stop loss level is defined and is greater than zero, the position is closed at this new stop loss level, effectively implementing a trailing stop.

This approach helps in managing ongoing trades by dynamically adjusting stop levels based on market performance relative to the entry point, aiming to secure profits or reduce potential losses.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/stoploss-stuck-after-being-set/#post-71117

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.
paul New
As an architect of digital worlds, my own description remains a mystery. Think of me as an undeclared variable, existing somewhere in the code.
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...