Implementing a Trailing Stop Loss Based on Historical Price Lows and Highs

10 Sep 2022
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to implement a trailing stop loss strategy that adjusts based on the high or low prices from three bars ago. The strategy is designed for both long and short positions in trading.


Not tested:
IF MyLongConditions AND Not OnMarket THEN
    BUY 1 CONTRACT AT MARKET
    NewSL = low[3]-1*pipsize
    SELL AT NewSL STOP
ENDIF

IF MyShortConditions AND Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT MARKET
    NewSL = high[3]+1*pipsize
    EXITSHORT AT NewSL STOP
ENDIF

IF OnMarket THEN
    IF PositionPerf > 0 THEN
        IF LongOnMarket THEN
            NewSL = max(newSL,low[3]-1*pipsize)
        ELSE
            NewSL = min(newSL,high[3]+1*pipsize)
        ENDIF
    ENDIF
    IF NewSL > 0 THEN
        SELL AT NewSL STOP
        EXITSHORT AT NewSL STOP
    ENDIF
ENDIF

Explanation of the Code:

  • The code starts by checking if the conditions for entering a long position (MyLongConditions) are met and if there is currently no open market position (Not OnMarket). If both conditions are true, it executes a buy order for one contract at the market price. It then sets a new stop loss (NewSL) at the low price of the bar three bars ago minus one pip.
  • Similarly, for a short position, the code checks if the conditions for entering a short position (MyShortConditions) are met and if there is no open market position. If true, it executes a sell short order for one contract at the market price and sets the stop loss at the high of the bar three bars ago plus one pip.
  • If there is an open market position (OnMarket), the code then checks if the current position is profitable (PositionPerf > 0). If profitable and it’s a long position, it updates the stop loss to be the maximum of the current stop loss or the low of the bar three bars ago minus one pip. For a short position, it updates the stop loss to be the minimum of the current stop loss or the high of the bar three bars ago plus one pip.
  • If the new stop loss (NewSL) is greater than zero, the code places a stop order to sell (for long positions) or to exit short (for short positions) at the new stop loss level.

This code snippet is useful for traders looking to protect their profits by adjusting their stop loss levels based on recent market movements, specifically targeting the highs and lows from three bars prior.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/stop-loss-3-candles/#post-150870

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
33
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...