Partial close and breakeven
Forums › ProRealTime English forum › ProOrder support › Partial close and breakeven
- This topic has 7 replies, 3 voices, and was last updated 3 years ago by
ProRealP.
-
-
05/16/2022 at 4:10 PM #193263
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?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253defparam cumulateorders = falsepositionsize = 1indicator1 = average [25]indicator2 = average [200]c1 = indicator1 crosses over indicator2c2 = indicator1 crosses under indicator2if c1 thenbuy positionsize contract at marketendifif c2 thensellshort positionsize contract at marketendifONCE partialclose = 1IF partialclose THENONCE PerCent = 0.9 //% positions to closeONCE PerCent2 = 0.01 //% positions to closeONCE MinLotSize = 1 //1 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyExitQuantity2 = abs(CountOfPosition) * PerCent2LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)CloseQuantity2 = abs(CountOfPosition) - LeftQty2IF LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THENexitshort CloseQuantity2 Contracts AT MarketFlag2 = 0endifendifif flag2 = 0 thenset stop breakevenendifIF Not OnMarket THENFlag = 1Flag2 = 1ENDIF05/16/2022 at 4:58 PM #193268If you want to reset variables to their initial states, please do it preferably at the top of the code.
So this:
1234IF Not OnMarket THENFlag = 1Flag2 = 1ENDIFshould be located before this:
123if flag2 = 0 thenset stop breakevenendif05/16/2022 at 5:22 PM #19326905/16/2022 at 8:50 PM #193284If you try with two separate snippets? (Dax 15m)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950defparam cumulateorders = falsepositionsize = 10indicator1 = average [25]indicator2 = average [200]c1 = indicator1 crosses over indicator2c2 = indicator1 crosses under indicator2if c1 thenbuy positionsize contract at marketendifif c2 thensellshort positionsize contract at marketendif//-------------------------------------------------------------------------------------once partialcloseGain = 1If partialcloseGain thenONCE PerCent = 0.9 //close 90% sizeONCE PerCentGain = 0.01 //= 1%ONCE MinLotSize = 1 //IG minimum sizeExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag = 1ENDIFIF partialcloseGain AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF partialcloseGain AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain)) AND Flag THENexitshort CloseQuantity Contracts AT MarketFlag = 0endifendif//--------------------------------------------------------------------------------------------------------------------------percent = 1 //1%pointToReachBreakeven=(close/100)*percentif not onmarket thenbreakevenX = 0 // with the insertion of the new instructions you can no longer write breakeven!endifif longOnMarket and close-positionPrice>=pointToReachBreakEven thenbreakevenX = 1endifif shortOnMarket and positionPrice-close>=pointToReachBreakeven thenbreakevenX = 1endifif breakevenX = 1 thensell at positionPrice STOPexitShort at positionPrice STOPendif//-----------------------------------------------------------------------------------------------------05/17/2022 at 9:02 AM #193304I 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).
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556defparam cumulateorders = falseIF Not OnMarket THENFlag = 1Flag2 = 1ENDIFpositionsize = 5indicator1 = average [25]indicator2 = average [200]c1 = indicator1 crosses over indicator2c2 = indicator1 crosses under indicator2if c1 thenbuy positionsize contract at marketset stop ploss 200endifif c2 thensellshort positionsize contract at marketset stop ploss 200endifONCE partialclose = 1IF onmarket and partialclose THENONCE PerCent = 0.9 //% positions to closeONCE PerCent2 = 0.01 //% positions to closeONCE MinLotSize = 1 //1 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = max(minlotsize,abs(CountOfPosition) - LeftQty)ExitQuantity2 = abs(CountOfPosition) * PerCent2LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)CloseQuantity2 = max(minlotsize,abs(CountOfPosition) - LeftQty2)IF LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THENexitshort CloseQuantity2 Contracts AT MarketFlag2 = 0endifendifif flag2 = 0 thenset stop breakevenendif//graph flag2//graph CloseQuantity//graph LongOnMarket and close >= (PositionPrice + 4*pipsize)1 user thanked author for this post.
05/18/2022 at 2:23 PM #193383Thanks! 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?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566defparam cumulateorders = falseIF Not OnMarket THENFlag = 1Flag2 = 1ENDIFpositionsize = 5indicator1 = average [25]indicator2 = average [200]c1 = indicator1 crosses over indicator2c2 = indicator1 crosses under indicator2If c1 thenbuy positionsize contract at marketsl=AverageTrueRange[14]*2slpricelong= close - slendifIf c2 thensellshort positionsize contract at marketsl=AverageTrueRange[14]*2slpriceshort = close - slendifif positionperf < 0 and longonmarket and close > slpricelong thensell at sl stopendifif positionperf < 0 and shortonmarket and close < slpriceshort thenexitshort at sl stopendifONCE partialclose = 1IF onmarket and partialclose THENONCE PerCent = 0.9 //% positions to closeONCE PerCent2 = 0.01 //% positions to closeONCE MinLotSize = 1 //1 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = max(minlotsize,abs(CountOfPosition) - LeftQty)ExitQuantity2 = abs(CountOfPosition) * PerCent2LeftQty2 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)CloseQuantity2 = max(minlotsize,abs(CountOfPosition) - LeftQty2)IF LongOnMarket and close >= (PositionPrice + 4*pipsize) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF ShortOnMarket and close <= (PositionPrice - 8*pipsize) AND Flag2 THENexitshort CloseQuantity2 Contracts AT MarketFlag2 = 0endifendifif flag2 = 0 thenset stop breakevenendif//graph flag2//graph CloseQuantity//graph LongOnMarket and close >= (PositionPrice + 4*pipsize)05/18/2022 at 4:39 PM #193394Because 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).
1 user thanked author for this post.
05/18/2022 at 4:56 PM #193403 -
AuthorPosts
Find exclusive trading pro-tools on