This code snippet demonstrates how to implement a dynamic trailing stop strategy in the ProBuilder language, used within the ProRealTime trading platform. The strategy is designed to adjust the stop loss level dynamically based on the maximum price achieved since entry (Maximum Favorable Excursion or MFE). It is particularly useful for managing open positions and locking in profits while allowing room for price movement.
DEFPARAM CumulateOrders = False
trailingstop1 = 40
bottomTrailingStopValue1 = 30
TopTrailingStopValue1 = 70
if not onmarket then
MAXPRICE1 = 0
priceexit1 = 0
endif
a = close-tradeprice(1) > bottomTrailingStopValue1*pointsize
b = close-tradeprice(1) < TopTrailingStopValue1*pointsize
if longonmarket then
MAXPRICE1 = MAX(MAXPRICE1,close) //saving the MFE of the current trade
if a and b then
if MAXPRICE1-tradeprice(1)>=trailingstop1*pointsize then //if the MFE is higher than the trailingstop
priceexit1 = MAXPRICE1-trailingstop1*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
endif
if onmarket and priceexit1>0 then
EXITSHORT AT priceexit1 STOP
SELL AT priceexit1 STOP
endif
IF close CROSSES OVER average[20,0](close) THEN
BUY AT MARKET
SET TARGET pPROFIT 250
SET STOP pLOSS 200
ENDIF
graph a
graph b
graph MAXPRICE1
graph priceexit1
graph MAXPRICE1-tradeprice
Explanation of the Code:
This snippet is a practical example of how to manage trades dynamically to potentially increase profitability while controlling downside risk.
Check out this related content for more information:
https://www.prorealcode.com/topic/need-help-with-fx-trail/#post-73718
Visit Link