Good.
I needed code for multiple partial closures within one operation.
If the operation is positive and earns equal to or more than 0.25%, close 1/5 of the position.
If the operation is positive and earns equal to or more than 0.5%, close 1/5 of the position.
It’s an example, thanks.
There you go (not tested):
If not OnMarket then
Tally = 0
Endif
ProfitPC = PositionPrice * PositionPerf * 100
If ProfitPC >= 0.25 and Tally = 0 then
Close PositionSize/5 contracts at Market
Tally = Tally + 1
Endif
If ProfitPC >= 0.50 and Tally = 1 then
Close PositionSize/5 contracts at Market
Tally = Tally + 1
Endif
Here’s another version (also coded by Roberto👍)
ONCE partialclose = 1
ONCE PerCent = pc //0.2 = 20% positions to close
ONCE PerCent2 = pc2 //0.2 = 20% positions to close
ONCE PerCentGain = pcg //.0025 = 0.25% gain
ONCE PerCentGain2 = pcg2 //.005 = 0.5% gain
ONCE MinLotSize = 0.5 //0.5 lots minimum
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
CloseQuantity = abs(CountOfPosition) - LeftQty
ExitQuantity2 = abs(CountOfPosition) * PerCent2
LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)
CloseQuantity2 = abs(CountOfPosition) - LeftQty2
IF Not OnMarket THEN
Flag = 1
Flag2 = 1
ENDIF
IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF partialclose AND shortOnMarket and close <= (PositionPrice * (1 - PerCentGain2)) AND Flag2 THEN
exitshort CloseQuantity2 Contracts AT Market
Flag2 = 0
endif
I’m sorry, but in Roberto’s code, line 5, it doesn’t matter if I put 0.25, 0.5, or 1, he always wins the same in backtest.
I think the code is not working properly.
Sorry, there are two mistakes:
- I typed CLOSE in line 6 and 10 instead of SELL (or SELLSHORT)
- line 4 should read
ProfitPC = PositionPerf * 100