Implementing a Dynamic Trailing Stop Strategy with Sensitivity Settings in Trading

17 Aug 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop strategy in a trading algorithm using the ProBuilder language. The trailing stop adjusts based on a percentage of the position price and incorporates sensitivity settings that determine the trigger based on different price points like close, high, low, or a typical price.


once trailingstoptype = 1
if trailingstoptype then
    trailingpercentlong = tst
    trailingpercentshort = tss
    once acceleratorlong = a1
    once acceleratorshort= a2
    ts2sensitivity = 2

    once steppercentlong = (trailingpercentlong/10)*acceleratorlong
    once steppercentshort = (trailingpercentshort/10)*acceleratorshort

    if onmarket then
        trailingstartlong = positionprice*(trailingpercentlong/100)
        trailingstartshort = positionprice*(trailingpercentshort/100)
        trailingsteplong = positionprice*(steppercentlong/100)
        trailingstepshort = positionprice*(steppercentshort/100)
    endif

    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
        newsl = 0
        mypositionprice = 0
    endif

    positioncount = abs(countofposition)

    if newsl > 0 then
        if positioncount > positioncount[1] then
            if longonmarket then
                newsl = max(newsl,positionprice * newsl / mypositionprice)
            else
                newsl = min(newsl,positionprice * newsl / mypositionprice)
            endif
        endif
    endif

    if ts2sensitivity=1 then
        ts2sensitivitylong=close
        ts2sensitivityshort=close
    elsif ts2sensitivity=2 then
        ts2sensitivitylong=high
        ts2sensitivityshort=low
    elsif ts2sensitivity=3 then
        ts2sensitivitylong=low
        ts2sensitivityshort=high
    elsif ts2sensitivity=4 then
        ts2sensitivitylong=(typicalprice)
        ts2sensitivityshort=(typicalprice)
    endif

    if longonmarket then
        if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong then
            newsl = positionprice+trailingsteplong
        endif
        if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong then
            newsl = newsl+trailingsteplong
        endif
    endif

    if shortonmarket then
        if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort then
            newsl = positionprice-trailingstepshort
        endif
        if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort then
            newsl = newsl-trailingstepshort
        endif
    endif

    if barindex-tradeindex>1 then
        if longonmarket then
            if newsl>0 then
                sell at newsl stop
            endif
            if newsl>0 then
                if low crosses under newsl then
                    sell at market
                endif
            endif
        endif

        if shortonmarket then
            if newsl>0 then
                exitshort at newsl stop
            endif
            if newsl>0 then
                if high crosses over newsl then
                    exitshort at market
                endif
            endif
        endif
    endif

    mypositionprice = positionprice
endif

The code snippet above is structured to manage a trailing stop loss in a trading system. Here’s a step-by-step explanation:

  • Initialization: Sets up initial parameters like trailing stop percentages and acceleration factors.
  • Market Entry: Calculates the trailing start and step values when a position is entered.
  • Adjustment Logic: Adjusts the stop loss level dynamically based on market conditions and the defined sensitivity (close, high, low, or typical price).
  • Exit Conditions: Defines conditions under which the position should be closed, either at the stop loss level or through a market order if the price crosses the trailing stop level.

This example is useful for understanding how to implement complex trading strategies programmatically, focusing on dynamic and responsive stop loss mechanisms.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/change-stop-loss-in-strategy/#post-179902

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