LéoParticipant
Average
Dear All,
I am working for few days on the code below.
This code works fine with DEFPARAM CumulateOrders = False but the trailing stop strategy does not work when I turn this option to true (DEFPARAM CumulateOrders = True)
(The trailing stop strategy is coming from the MOD code – Thank you to the original author of this code)
I am wondering if my issue is coming from the “Set Stop Ploss 200” that I added at the end of the code or if it is linked to the reset of the stoploss value.
I would be gratefull if you can help me with this issue !
Thank you in advance.
Regards.
// Definition of code parameters
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
DEFPARAM preloadbars = 5000
positionsize=0.2
Periode = 30
//Definition du range
TopRange = highest[periode](high)
LowRange = Lowest [periode](low)
C1 = TopRange-LowRange <200
// Conditions to enter positions
if C1 then
buy positionsize CONTRACT at TopRange+1 stop
sellshort positionsize CONTRACT at LowRange-1 stop
Endif
//%trailing stop function
trailingPercent = .26
stepPercent = .014
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
Set Stop Ploss 200
When accumulating positions, you will have more than one single TRADEPRICE, so ProOrder calculates the average of all trade prices and position sizes (attached pic).
Replacing TRADEPRICE(1) with POSITIONPRICE should do.
You can always use POSITIONPRICE even when you are not accumulating positions, in that case TRADEPRICE and POSITIONPRICE will match.
LéoParticipant
Average
Hi Roberto,
Thank you for this input. I will adapt the code and test accordingly !
Thank you again.