Implementing a Trailing Stop Strategy Based on Higher Lows

21 Sep 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a trailing stop strategy based on higher lows in trading using the ProBuilder language. A trailing stop adjusts the stop-loss level as the price of an asset moves in a favorable direction, which can potentially lock in profits.


IF Not OnMarket THEN MySL = 0 ENDIF
IF MyLongConditions THEN
    BUY 1 CONTRACT AT Market
    MySL = low
ENDIF
IF MySL > 0 THEN
    MySL = max(MySL,low)
    SELL AT MySL STOP
ENDIF

Explanation of the Code:

  • Line 1: Checks if the trading system is currently not in the market (i.e., no open positions). If true, it sets MySL (My Stop Loss) to 0. This is a reset mechanism for the stop loss.
  • Line 2-4: Evaluates whether the conditions to enter a long position (MyLongConditions) are met. If these conditions are true, the system will execute a buy order for 1 contract at the market price and set MySL to the current low price of the asset.
  • Line 5-7: Checks if MySL is greater than 0, which indicates that a position is open and a stop loss level has been set. It then updates MySL to the maximum of the current MySL or the current low price, ensuring that the stop loss level trails the lowest price. Finally, it places a stop sell order at the MySL level to exit the position if the price drops to this level.

This snippet is a basic implementation of a trailing stop strategy and can be adapted for different trading conditions or asset types. It helps in managing risk by potentially locking in profits as the asset price moves favorably, and limiting losses when the trend reverses.

Related Post

Check out this related content for more information:

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

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