Implementing a Dynamic Trailing Stop Strategy in Trading

17 Dec 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a dynamic trailing stop strategy for trading positions using the ProBuilder language. A trailing stop adjusts the stop loss level as the price of an asset moves, providing a balance between locking in profits and allowing room for price movement.

//Vonasi Trailing Stop v3 //20190509
sl = 144 //Stop loss distance
slmove = 5 //Price move needed to move stop
minstop = 10 //Minimum stop distance allowed
sl = max(sl, minstop)
if longonmarket and sladj then
  slprice = positionprice - sl
  sladj = 0
endif
if shortonmarket and sladj then
  slprice = positionprice + sl
  sladj = 0
endif
if not onmarket and (your long entry conditions) then
  buy 1 contract at market
  slprice = close - sl
  sell at slprice stop
  sladj = 1
endif
if not onmarket and (your short entry conditions) then
  sellshort 1 contract at market
  slprice = close + sl
  exitshort at slprice stop
  sladj = 1
endif
if longonmarket and high - sl > slprice + slmove then
  slprice = min(high - sl, close - minstop)
endif
if shortonmarket and low + sl < slprice - slmove then
  slprice = max(low + sl, close + minstop)
endif
sell at slprice stop
exitshort at slprice stop

This code snippet is structured to adjust the stop loss levels dynamically based on market conditions and the position of the asset. Here's a step-by-step explanation:

  • Initialization: Set initial values for the stop loss distance (sl), the price movement required to adjust the stop loss (slmove), and the minimum stop distance (minstop). The stop loss distance is adjusted to be no less than the minimum stop distance.
  • Adjusting Stop Loss: If in a long position and adjustments are flagged (sladj), the stop loss price (slprice) is recalculated and adjustment flag is reset. The same logic applies for short positions.
  • Entering Positions: If not currently in the market and certain entry conditions are met, enter either a long or short position at market price, set the initial stop loss price, and enable adjustments with sladj.
  • Dynamic Adjustment: While in a long position, if the high price minus the stop loss distance is greater than the current stop loss price plus the required move, adjust the stop loss price upwards but not below the minimum stop distance. A similar adjustment is made for short positions but in the opposite direction.
  • Executing Stop Orders: Finally, execute the stop orders for both long and short positions based on the adjusted or initial stop loss price.

This example provides a practical implementation of trailing stops which are crucial for managing risk in trading strategies.

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