Implementing a Dynamic Trailing Stop Strategy Using Higher Lows and Highs

05 Apr 2016
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to implement a dynamic trailing stop strategy based on higher lows for long positions and lower highs for short positions. The trailing stop adjusts as the market price moves, providing a method to potentially protect gains or minimize losses in trading scenarios.


IF Not OnMarket THEN 
    MySLl = 0 
    MySLs = 999999 
ENDIF 

IF LONGONMARKET AND (close > low[1]) THEN 
    MySLl = max(low[1],MySLl) 
ENDIF 

IF MySLl > 0 THEN 
    SELL AT MySLl STOP 
ENDIF 

IF SHORTONMARKET AND (close < high[1]) THEN 
    MySLs = min(MySLs,high[1]) 
ENDIF 

IF MySLs > 0 THEN 
    BUY AT MySLs STOP 
ENDIF

Explanation of the Code:

  • Initialization: When not in the market (Not OnMarket), the long stop loss (MySLl) is set to 0 and the short stop loss (MySLs) is set to a very high value (999999). This setup ensures that the stop values are reset when a new trade is initiated.
  • Adjusting Long Stop Loss: If the strategy is in a long position (LONGONMARKET) and the current closing price is greater than the previous low (close > low[1]), the long stop loss (MySLl) is updated to the maximum of the previous low or the existing stop loss value. This logic helps in raising the stop loss as the price makes new highs, securing profits on a move upwards.
  • Executing Long Exit: If the long stop loss (MySLl) is greater than 0, a sell order is placed at the stop loss level, allowing the position to exit when the price drops to this level.
  • Adjusting Short Stop Loss: For short positions (SHORTONMARKET), if the closing price is below the previous high (close < high[1]), the short stop loss (MySLs) is updated to the minimum of the previous high or the existing stop loss value. This adjustment lowers the stop loss as the price makes new lows, protecting gains in a downward moving market.
  • Executing Short Exit: If the short stop loss (MySLs) is greater than 0, a buy order is placed at this stop loss level to cover the short position when the price rises to this level.

This strategy is useful for traders looking to automate their stop loss adjustments based on market movements, potentially enhancing their trading strategy's effectiveness by locking in profits and reducing losses.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/trailing-stop-based-on-higher-lows/#post-197711

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