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 Legend
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
135
strategy
171

Recent Snippets

A multi indicator Dashboard for ProRealTime
indicator
This snippet creates a compact on-chart dashboard that aggregates multiple indicator states into a single, readable [...]
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 [...]
Logo Logo
Loading...