Implementing Dynamic Trailing Stop and Take Profit in Trading Strategies

28 Nov 2019
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop loss and take profit mechanism in a trading strategy using ProBuilder language. The trailing stop adjusts as the market price moves, ensuring that the stop loss and take profit levels adapt to changing market conditions.


sl = 20 //Stop loss distance
slmove = 3 //Price move needed to move stop loss
slminstop = 5 //Minimum stop loss distance allowed
tp = 20 //Take profit distance
tpmove = 3 //Price move needed to move take profit
tpminstop = 5 //Minimum take profit distance allowed

sl = max(sl, slminstop)
tp = max(tp, tpminstop)

if longonmarket and adj then
    slprice = positionprice - sl
    tpprice = positionprice + tp
    adj = 0
endif

if shortonmarket and adj then
    slprice = positionprice + sl
    tpprice = positionprice - tp
    adj = 0
endif

if not onmarket and (your long entry conditions) then
    buy 1 contract at market
    slprice = close - sl
    tpprice = close + tp
    adj = 1
endif

if not onmarket and (your short entry conditions) then
    sellshort 1 contract at market
    slprice = close + sl
    tpprice = close - tp
    adj = 1
endif

if longonmarket and high - sl > slprice + slmove then
    slprice = min(high - sl, close - slminstop)
endif

if longonmarket and low + tp < tpprice - tpmove then
    tpprice = max(low + tp, close + tpminstop)
endif

if shortonmarket and low + sl < slprice - slmove then
    slprice = max(low + sl, close + slminstop)
endif

if shortonmarket and high - tp > tpprice + tpmove then
    tpprice = min(high - tp, close - tpminstop)
endif

sell at slprice stop
exitshort at slprice stop
sell at tpprice limit
exitshort at tpprice limit

Explanation of the Code:

  • The variables sl, slmove, and slminstop define the stop loss distance, the price movement needed to trigger a stop loss adjustment, and the minimum stop loss distance, respectively.
  • Similarly, tp, tpmove, and tpminstop define the take profit distance, the price movement needed to trigger a take profit adjustment, and the minimum take profit distance.
  • The max function ensures that the stop loss and take profit do not fall below their respective minimum values.
  • Conditional statements check whether the market is long or short and adjust the stop loss (slprice) and take profit (tpprice) prices accordingly.
  • New positions are opened based on specific entry conditions (not detailed here), setting initial values for slprice and tpprice.
  • Further conditions dynamically adjust the slprice and tpprice as the market price moves, ensuring the trailing functionality.
  • The sell and exitshort commands are used to exit positions at the calculated stop loss and take profit levels.

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
134
strategy
171

Recent Snippets

How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
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 [...]
Logo Logo
Loading...