Good morning,
I am trying to code a partial reentry code that, in case that an open position goes near the SL, it should buy/sell a position equal to positionsize/2. I am using cumulateorders = true and Countofposition <=2, and I want the robot to perform the reentry only once. I am not sure what am I doing wrong but the robot does not perform the reentry correctly, could someone give me some light about that topic? I am attaching the code for the reentry:
ONCE partialreentry = 1
IF partialreentry and Countofposition<= 2 THEN
ONCE Partialreentrypos = Positionsize/2 //Position to reenter
IF Not OnMarket THEN
Flagr = 1
Flagr2 = 1
ENDIF
IF LongOnMarket and close <= (PositionPrice - PositionPrice*0.022) AND Flagr THEN //0.22 corresponds to the point where I want to set the order, as long as my stoploss is 2,5% away from entry price
BUY Partialreentrypos Contracts AT Market
Set stop %loss 0.3
Set target %profit 2.2
Flagr = 0
endif
IF ShortOnMarket and close >= (PositionPrice + PositionPrice*0.018) AND Flagr2 THEN // Same here, my stoploss is 2% away from entry price so I would like the robot to sell another position in case that the price is 0,2% away from the SL.
BUY Partialreentrypos Contracts AT Market
Set stop %loss 0.2
Set target %profit 1.8 // The profit corresponds to the BE price of the first position
Flagr2 = 0
endif
endif
Thank you so much.
Remove ONCE from line 3, because it will be set when the strategy starts only, with an incorrect value (you can GRAPH it).
Line 10 could also be written like that:
(PositionPrice*0.978)
and line 17:
(PositionPrice*1.018)
Thank you for your answer, one more question: In case it opens another position, if I use PositionPrice command which price will the robot take, the one corresponding to the first opened position or the second one?
Do not use QUOTE when unnecessary, as it makes posts more difficult to read and too long!
Thank you 🙂
PositionPrice (apart when there’s only one position open), is an average of all entry prices and SL + TP will refer to that price (see pic to see how it is calculated).