Code for partial closures by percentage.
Forums › ProRealTime English forum › ProOrder support › Code for partial closures by percentage.
- This topic has 5 replies, 3 voices, and was last updated 2 years ago by robertogozzi.
Viewing 6 posts - 1 through 6 (of 6 total)
-
-
01/19/2022 at 10:08 AM #185903
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.
01/19/2022 at 10:56 AM #185906There you go (not tested):
123456789101112If not OnMarket thenTally = 0EndifProfitPC = PositionPrice * PositionPerf * 100If ProfitPC >= 0.25 and Tally = 0 thenClose PositionSize/5 contracts at MarketTally = Tally + 1EndifIf ProfitPC >= 0.50 and Tally = 1 thenClose PositionSize/5 contracts at MarketTally = Tally + 1Endif01/19/2022 at 12:30 PM #185908Here’s another version (also coded by Roberto👍)
12345678910111213141516171819202122232425262728ONCE partialclose = 1ONCE PerCent = pc //0.2 = 20% positions to closeONCE PerCent2 = pc2 //0.2 = 20% positions to closeONCE PerCentGain = pcg //.0025 = 0.25% gainONCE PerCentGain2 = pcg2 //.005 = 0.5% gainONCE MinLotSize = 0.5 //0.5 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 Not OnMarket THENFlag = 1Flag2 = 1ENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF partialclose AND shortOnMarket and close <= (PositionPrice * (1 - PerCentGain2)) AND Flag2 THENexitshort CloseQuantity2 Contracts AT MarketFlag2 = 0endif1 user thanked author for this post.
01/19/2022 at 1:51 PM #18592301/20/2022 at 9:45 AM #18601801/20/2022 at 11:18 AM #186037Sorry, there are two mistakes:
- I typed CLOSE in line 6 and 10 instead of SELL (or SELLSHORT)
- line 4 should read
1ProfitPC = PositionPerf * 100 -
AuthorPosts
Viewing 6 posts - 1 through 6 (of 6 total)
Find exclusive trading pro-tools on
Similar topics: