Implementing Various Trailing Stop Strategies in ProBuilder

13 Feb 2016
0 comment
0 attachment

This code snippet demonstrates how to implement three different types of trailing stop strategies in ProBuilder language. Trailing stops are dynamic stop orders that adjust as the market price moves, helping to protect profits while allowing a trade to remain open as long as the price is moving favorably.

//%trailing stop function
trailingPercent = tst
stepPercent = st
if onmarket then
    trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
    trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN newSL=0 ENDIF
//manage long positions
IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN newSL = tradeprice(1)+trailingstep ENDIF
    //next moves
    IF newSL>0 AND close-newSL>trailingstep THEN newSL = newSL+trailingstep ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN newSL = tradeprice(1)-trailingstep ENDIF
    //next moves
    IF newSL>0 AND newSL-close>trailingstep THEN newSL = newSL-trailingstep ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
ENDIF

Explanation of the Code:

  • The code starts by defining two percentage values: trailingPercent and stepPercent, which determine the initial point to start the trailing stop and the step size to move the stop loss, respectively.
  • It checks if the market is entered (onmarket). If true, it calculates the trailingstart and trailingstep based on the trade price and the defined percentages.
  • If the market is not entered, it resets the stop loss value (newSL) to zero.
  • For long positions, the code first checks if the stop loss is not set (newSL=0) and the profit has reached the trailingstart. If true, it sets the stop loss to the trade price plus the trailing step. It then adjusts the stop loss further if the price continues to rise.
  • For short positions, similar logic is applied but in the opposite direction, adjusting the stop loss down as the price falls.
  • Finally, if a new stop loss (newSL) is set, the code places a stop order to exit the position at the new stop loss level for both long and short positions.

This snippet is a practical example of how to manage risk dynamically in trading strategies using ProBuilder language, focusing on trailing stops to lock in profits while allowing room for further gains.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/moving-stop-loss/#post-136960

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.
nonetheless Master
As an architect of digital worlds, my own description remains a mystery. Think of me as an undeclared variable, existing somewhere in the code.
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...