Implementing a Trailing Stop Loss Strategy in ProBuilder

08 Dec 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a trailing stop loss strategy in ProBuilder, which is useful for managing risk in trading by adjusting the stop loss level as the market price moves favorably.


StopLoss = 23 //StopLoss size
TrailingStopStart = 0 //Trailing Stop start at X points in profit
buystoploss = close-StopLoss*pointsize
sellstoploss = close+StopLoss*pointsize

if not onmarket then
    ibuystoploss=buystoploss
    isellstoploss=sellstoploss
endif

if longonmarket then
    //checking and adjusting stoploss
    if close-tradeprice>=trailingstopstart*pointsize then
        ibuystoploss = max(ibuystoploss,high-(stoploss+trailingstopstart)*pointsize)
    endif
    //set the stoploss level
    sell at ibuystoploss stop
endif

if shortonmarket then
    //checking and adjusting stoploss
    if tradeprice-close>=trailingstopstart*pointsize then
        isellstoploss = min(isellstoploss,low+(stoploss+trailingstopstart)*pointsize)
    endif
    //set the stoploss level
    exitshort at isellstoploss stop
endif

Explanation of the Code:

  • Initialization: The variables StopLoss and TrailingStopStart are set, defining the size of the stop loss and the point at which the trailing stop starts.
  • Initial Stop Loss Calculation: buystoploss and sellstoploss are calculated based on the closing price adjusted by the stop loss size.
  • Market Entry: If not currently in the market, the initial stop loss levels are stored in ibuystoploss and isellstoploss.
  • Long Position Adjustment: If in a long position and the profit reaches the trailing start point, ibuystoploss is adjusted to the higher of the current ibuystoploss or the high price minus the adjusted stop loss.
  • Setting the Stop Loss for Long Position: The stop loss for a long position is set at ibuystoploss.
  • Short Position Adjustment: If in a short position and the profit reaches the trailing start point, isellstoploss is adjusted to the lower of the current isellstoploss or the low price plus the adjusted stop loss.
  • Setting the Stop Loss for Short Position: The stop loss for a short position is set at isellstoploss.

This code snippet is a practical example of how to dynamically manage stop losses in trading strategies to protect gains or limit losses as price movements occur.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/code-trailing-stop/#post-215762

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...