Implementing Dynamic Stop Loss and Break Even Using Market Structure in ProBuilder

27 Jan 2023
0 comment
0 attachment

This code snippet demonstrates how to dynamically adjust stop loss levels based on market structure, specifically identifying swing highs and lows over a specified lookback period. It is designed for use in trading strategies to help manage risk and protect profits.


ONCE SigHI1 = 0
ONCE SigHI2 = 0
ONCE SigLO1 = 0
ONCE SigLO2 = 0
ONCE LookBack = 10
IF Not OnMarket Then
    MySL = 0
ENDIF
MaxHI = max(open,close)
MinLO = min(open,close)
Bullish = close > open
Bearish = close < open
SwingHI = highest[LookBack](MaxHI)
SwingLO = lowest[LookBack](MinLO)
SigHI = SwingHI AND Bearish AND Bullish[1]
SigLO = SwingLO AND Bullish AND Bearish[1]
IF LongOnMarket AND SigHI > 0 AND SigHI > SigHI1 THEN
    SigHI2 = SigHI1
    SigHI1 = SigHI
    IF SigHI1 > SigLO1 THEN
        MySL = max(TradePrice,max(MySL,SigLO1))
    ENDIF
ELSIF ShortOnMarket AND SigLO > 0 AND SigLO < SigLO1 THEN
    SigLO2 = SigLO1
    SigLO1 = SigLO
    IF SigLO1 < SigHI1 THEN
        MySL = min(TradePrice,min(MySL,SigLO1))
    ENDIF
ENDIF
IF OnMarket AND MySL <> 0 THEN
    SELL AT MySL Stop
    EXITSHORT AT MySL Stop
ENDIF

Explanation of the Code:

  • Initialization: Variables SigHI1, SigHI2, SigLO1, SigLO2 are initialized to zero, and LookBack is set to 10. These are used to store previous high and low signals and define the lookback period for calculating swings.
  • Market Entry Check: If not in the market, the stop loss (MySL) is reset to zero.
  • Swing High/Low Calculation: MaxHI and MinLO are calculated as the maximum and minimum of the open and close prices, respectively. SwingHI and SwingLO are then determined as the highest and lowest values within the lookback period.
  • Signal Generation: Signals for swing highs (SigHI) and lows (SigLO) are generated based on the current and previous bar’s bullish or bearish nature.
  • Stop Loss Adjustment: If in a long position and a new high signal is detected, the stop loss is adjusted to the maximum of the trade entry price, current stop loss, or the last significant low. For short positions, it adjusts to the minimum of these values based on low signals.
  • Market Exit: If in the market and a stop loss level is set, the position is exited at the stop loss level to either sell or cover a short.

This snippet is a foundational example of how to use market highs and lows to manage trade exits dynamically. Adjust the LookBack value to fit different trading styles or market conditions.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/move-to-be-on-confirmation-of-market-structure/#post-137288

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