Implementing Breakeven and Conditional Exit Strategies in Trading Algorithms

01 Jun 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a trading strategy in ProBuilder that adjusts the stop loss to breakeven after a certain profit is achieved and exits positions based on specific candlestick patterns when the breakeven is set. This strategy is useful for managing risk and securing profits in volatile markets.


defparam cumulateorders = false
startBreakeven = 20 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 1 //how much pips/points to keep in profit above or below our entry price when the breakeven is activated (beware of spread)

//exit long order on red candle (if breakeven is set)
if longonmarket and close0 then
    sell at market
endif

//exit short order on green candle (if breakeven is set)
if shortonmarket and close>open and breakevenLevel>0 then
    exitshort at market
endif

//dummy strategy
c1 = RSI[14] crosses over 50
if c1 then
    BUY 1 LOT AT MARKET
    SET STOP PLOSS 50
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" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
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" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
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 is structured into several key sections:

  • Parameter Initialization: Sets up initial parameters like the point at which breakeven is triggered and the profit to keep above the entry price.
  • Conditional Exit: Checks if the market position is long or short and exits based on the color of the candle (red for long positions, green for short positions) if the breakeven level has been reached.
  • Dummy Strategy: A simple trading condition using the RSI indicator to demonstrate how to enter the market.
  • Breakeven Calculation: Calculates the new breakeven level once the price has moved favorably by a specified number of points.
  • Stop Order Adjustment: Adjusts the stop orders to the new breakeven level once it is calculated.
  • Resetting Breakeven Level: Resets the breakeven level to zero when there are no open market positions, ensuring clean state for the next trade.

This example provides a practical approach to managing trades using breakeven adjustments and conditional exits based on market conditions, which can be particularly useful in strategies aiming to minimize risk and protect gains.

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