Hello!
I’m trying to combine partial close with a breakeven, but am unable to get it to work. The problem seems to be that the breakeven doesn’t “reset” between positions. Any ideas on how to do this?
defparam cumulateorders = false
positionsize = 1
indicator1 = average [25]
indicator2 = average [200]
c1 = indicator1 crosses over indicator2
c2 = indicator1 crosses under indicator2
if c1 then
buy positionsize contract at market
endif
if c2 then
sellshort positionsize contract at market
endif
ONCE partialclose = 1
IF partialclose THEN
ONCE PerCent = 0.9 //% positions to close
ONCE PerCent2 = 0.01 //% positions to close
ONCE MinLotSize = 1 //1 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 LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THEN
exitshort CloseQuantity2 Contracts AT Market
Flag2 = 0
endif
endif
if flag2 = 0 then
set stop breakeven
endif
IF Not OnMarket THEN
Flag = 1
Flag2 = 1
ENDIF
If you want to reset variables to their initial states, please do it preferably at the top of the code.
So this:
IF Not OnMarket THEN
Flag = 1
Flag2 = 1
ENDIF
should be located before this:
if flag2 = 0 then
set stop breakeven
endif
Understood! Doesn’t seem to make a difference in this case though. Did you happen to try it out?
If you try with two separate snippets? (Dax 15m)
defparam cumulateorders = false
positionsize = 10
indicator1 = average [25]
indicator2 = average [200]
c1 = indicator1 crosses over indicator2
c2 = indicator1 crosses under indicator2
if c1 then
buy positionsize contract at market
endif
if c2 then
sellshort positionsize contract at market
endif
//-------------------------------------------------------------------------------------
once partialcloseGain = 1
If partialcloseGain then
ONCE PerCent = 0.9 //close 90% size
ONCE PerCentGain = 0.01 //= 1%
ONCE MinLotSize = 1 //IG minimum size
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
CloseQuantity = abs(CountOfPosition) - LeftQty
IF Not OnMarket THEN
Flag = 1
ENDIF
IF partialcloseGain AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF partialcloseGain AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain)) AND Flag THEN
exitshort CloseQuantity Contracts AT Market
Flag = 0
endif
endif
//--------------------------------------------------------------------------------------------------------------------------
percent = 1 //1%
pointToReachBreakeven=(close/100)*percent
if not onmarket then
breakevenX = 0 // with the insertion of the new instructions you can no longer write breakeven!
endif
if longOnMarket and close-positionPrice>=pointToReachBreakEven then
breakevenX = 1
endif
if shortOnMarket and positionPrice-close>=pointToReachBreakeven then
breakevenX = 1
endif
if breakevenX = 1 then
sell at positionPrice STOP
exitShort at positionPrice STOP
endif
//-----------------------------------------------------------------------------------------------------
I think you are right, this version works much better, I set a wide stoploss at orders launch, then the breakeven is set upon your conditions (I fixed a partial exit size calculation problem too).
defparam cumulateorders = false
IF Not OnMarket THEN
Flag = 1
Flag2 = 1
ENDIF
positionsize = 5
indicator1 = average [25]
indicator2 = average [200]
c1 = indicator1 crosses over indicator2
c2 = indicator1 crosses under indicator2
if c1 then
buy positionsize contract at market
set stop ploss 200
endif
if c2 then
sellshort positionsize contract at market
set stop ploss 200
endif
ONCE partialclose = 1
IF onmarket and partialclose THEN
ONCE PerCent = 0.9 //% positions to close
ONCE PerCent2 = 0.01 //% positions to close
ONCE MinLotSize = 1 //1 lots minimum
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
CloseQuantity = max(minlotsize,abs(CountOfPosition) - LeftQty)
ExitQuantity2 = abs(CountOfPosition) * PerCent2
LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)
CloseQuantity2 = max(minlotsize,abs(CountOfPosition) - LeftQty2)
IF LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THEN
exitshort CloseQuantity2 Contracts AT Market
Flag2 = 0
endif
endif
if flag2 = 0 then
set stop breakeven
endif
//graph flag2
//graph CloseQuantity
//graph LongOnMarket and close >= (PositionPrice + 4*pipsize)
Thanks! That works well.
I now have another issue. When I use set stop ploss 200, or set stop %loss 1, it works. But when I use my custom stop loss, it doesn’t. I don’t understand why. Any ideas?
defparam cumulateorders = false
IF Not OnMarket THEN
Flag = 1
Flag2 = 1
ENDIF
positionsize = 5
indicator1 = average [25]
indicator2 = average [200]
c1 = indicator1 crosses over indicator2
c2 = indicator1 crosses under indicator2
If c1 then
buy positionsize contract at market
sl=AverageTrueRange[14]*2
slpricelong= close - sl
endif
If c2 then
sellshort positionsize contract at market
sl=AverageTrueRange[14]*2
slpriceshort = close - sl
endif
if positionperf < 0 and longonmarket and close > slpricelong then
sell at sl stop
endif
if positionperf < 0 and shortonmarket and close < slpriceshort then
exitshort at sl stop
endif
ONCE partialclose = 1
IF onmarket and partialclose THEN
ONCE PerCent = 0.9 //% positions to close
ONCE PerCent2 = 0.01 //% positions to close
ONCE MinLotSize = 1 //1 lots minimum
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
CloseQuantity = max(minlotsize,abs(CountOfPosition) - LeftQty)
ExitQuantity2 = abs(CountOfPosition) * PerCent2
LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)
CloseQuantity2 = max(minlotsize,abs(CountOfPosition) - LeftQty2)
IF LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THEN
exitshort CloseQuantity2 Contracts AT Market
Flag2 = 0
endif
endif
if flag2 = 0 then
set stop breakeven
endif
//graph flag2
//graph CloseQuantity
//graph LongOnMarket and close >= (PositionPrice + 4*pipsize)
Because once you set SET STOP BREAKEVEN, it will last forever and for any subsequent orders, until you set the stoploss to 0 or any other value with a SET STOP instruction (SET STOP LOSS 0 for instance at order launch).