Implementing a Breakeven Strategy with Percentage Thresholds in Trading

19 Mar 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a breakeven trading strategy using percentage thresholds in the ProBuilder language. The strategy adjusts the stop-loss order to a breakeven point after a specified percentage gain, aiming to secure a portion of the profits.


defparam cumulateorders = false
startBreakeven = 0.1 //how much percentage in price gain to activate the breakeven function?
PercentToKeep = 0.05 //how much percentage in price to keep in profit above or below our entry price when the breakeven is activated (beware of spread)
c1 = RSI[14] crosses over 50
if c1 then
    BUY 1 LOT AT MARKET
    SET STOP %LOSS 1
endif
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
    breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" percent already
IF LONGONMARKET AND positionperf>=startBreakeven THEN
    //calculate the breakevenLevel
    PointsToKeep = (tradeprice(1)/100)*PercentToKeep
    breakevenLevel = tradeprice(1)+PointsToKeep
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// --- SELL SIDE ---
//test if the price have moved favourably of "startBreakeven" percent already
IF SHORTONMARKET AND positionperf>=startBreakeven THEN
    //calculate the breakevenLevel
    PointsToKeep = (tradeprice(1)/100)*PercentToKeep
    breakevenLevel = tradeprice(1)-PointsToKeep
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
ENDIF
// --- end of SELL SIDE ---

The code snippet above is structured to manage trading orders based on the performance of the current position relative to a set percentage gain:

  • Initialization: Sets defparam cumulateorders to false to ensure each order is treated individually.
  • Trigger Condition: A buy order is placed if the RSI crosses over 50, which is a common momentum indicator used to signal potential entry points.
  • Breakeven Calculation: Once the position gains a specified percentage (startBreakeven), the code calculates a new stop-loss level (breakevenLevel) that aims to secure a portion of the profits defined by PercentToKeep.
  • Order Adjustment: If the breakeven level is greater than zero, the stop-loss order for a long position is adjusted to this new breakeven point. Similarly, for short positions, the stop order is adjusted to secure profits.
  • Market Exit: The script also handles the exit for short positions by setting a stop order at the calculated breakeven level.

This strategy helps in minimizing losses and protecting gains by dynamically adjusting stop-loss orders based on market performance relative to the entry point.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/stop-nachziehen-aber-wie/#post-38428

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