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:
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.sladj), the stop loss price (slprice) is recalculated and adjustment flag is reset. The same logic applies for short positions.sladj.This example provides a practical implementation of trailing stops which are crucial for managing risk in trading strategies.