Code for partial close with percentage
Forums › ProRealTime English forum › ProOrder support › Code for partial close with percentage
- This topic has 61 replies, 7 voices, and was last updated 4 years ago by
nonetheless.
-
-
02/17/2021 at 9:13 AM #161755
No, it’s not halving a position despite Flag=0, it’s Flag=0 because a position had been halved (because Flag=1 before that partial closure).
Bear in mind that GRAPH shows the value retained at the end of the execution, not the one retained ar the beginning (the beginning is just the end of the previous candle).
02/17/2021 at 10:25 AM #161777I get what you mean. See image below.
Note that only 1 buy order was executed and all the orders closed are half of remaining position.
The first halving is correct, and Flag thereafter is zero. So, I wonder why its continuing to execute the code despite the FLAG=1 condition not being met. If I check previous bars from 2nd halving as highlighted in image, Flag is zero, so code should not run after the first 50% is closed. In this case, its halving until, the qty is less than 0.5 contract.
02/17/2021 at 10:57 AM #161785I can only try to replicate your trades, provided:
- you post the code you used in your tests
- you post what instrument, TF, date and time those trades refer to
02/17/2021 at 12:42 PM #161808This is the code,
Instrument- APT (Afterpay Ltd) in 1 Min Timeframe. 3K unit data loaded
1234567891011121314151617181920212223242526272829303132333435363738394041// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedTimeframe (Daily)indicator1d, indicator2d, ignored, ignored = CALL DTOSC[1, 0](close)Timeframe (1 Hour)indicator1, indicator2, ignored, ignored = CALL DTOSC[1, 0](close)c1 = (indicator1 >= indicator2 ) or (indicator2<25)Condition=C1 and (indicator1d>indicator2d)// Conditions to enter long positionsONCE PerCent = 0.5 //50% = positions to closeONCE Pips = 20 * PipSizeONCE MinLotSize = 0.5 //0.5 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag = 1ENDIFIF close crosses over highest[20](high[1]) and Condition and not OnMarket THENbuy 10 contract at marketendif//IF close crosses under lowest[20](low[1]) and not OnMarket THEN//sellshort 1 contract at market//endifIF close >= (PositionPrice + Pips) AND LongOnMarket AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag THENEXITSHORT CloseQuantity Contracts AT MarketFlag = 0ENDIFset target pprofit 200set stop ploss 100graph abs(countofposition)graph Flaggraph positionperf*positionpricegraph positionprice02/17/2021 at 4:51 PM #161827The DTOSC in the library requires no parameters.
Please post a complete working code or I won’t be able to help you!
Post the ITF file of the indicator.
02/17/2021 at 4:54 PM #16182802/18/2021 at 12:23 AM #161863Itested it on DAX because with Afterpay Ltd I couldn’t get any trade.
Everything works fine and it halves positions only once per trade.
02/18/2021 at 5:15 AM #16187002/18/2021 at 9:12 AM #161890Ok, I got it.
It’s because you are using the MTF support incorrectly.
You are using a 1-minute TF but have no support for the DEFAULT TF, so it is executed every minute but you have writtem only instructions for the Daily and 1h TF’s.
If you add UPDATEONCLOSE to your TIMEFRAME instruction everything is fine.
If you DO NOT want to use UPDATEONCLOSE then you have to use TIMEFRAME(default) for the FLAG variable, as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041424344// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedTimeframe (Daily)indicator1d, indicator2d, ignored, ignored = CALL DTOSC[1, 0](close)Timeframe (1 Hour)indicator1, indicator2, ignored, ignored = CALL DTOSC[1, 0](close)c1 = (indicator1 >= indicator2 ) or (indicator2<25)Condition=C1 and (indicator1d>indicator2d)// Conditions to enter long positionsONCE PerCent = 0.5 //50% = positions to closeONCE Pips = 20 * PipSizeONCE MinLotSize = 0.5 //0.5 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtytimeframe(default)IF Not OnMarket THENFlag = 1ENDIFTimeframe (1 Hour)IF close crosses over highest[20](high[1]) and Condition and not OnMarket THENbuy 10 contract at marketendif//IF close crosses under lowest[20](low[1]) and not OnMarket THEN//sellshort 1 contract at market//endifTimeframe(default)IF close >= (PositionPrice + Pips) AND LongOnMarket AND Flag=1 THENSELL CloseQuantity Contracts AT MarketFlag = 0ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag=1 THENEXITSHORT CloseQuantity Contracts AT MarketFlag = 0ENDIFset target pprofit 200set stop ploss 100//graph abs(countofposition)graph Flaggraph positionperf*positionprice02/18/2021 at 11:56 AM #16190902/21/2021 at 11:54 AM #162168Ciao Roberto, thanks very much for the work you’ve done on the partial close code.
If I wanted to close 25% of my position after a .5% gain, another 25% after 1% gain, and the same after 1.5% (leaving the remainder to run to the TP or trailing stop) would this be the best way to do it?
1234567891011121314151617181920212223242526272829303132333435363738394041// partial closeONCE partialclose = 1if partialclose thenONCE PerCent = 0.25 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag = 1ENDIFIF close >= (PositionPrice * (1 + PerCentGain)) AND LongOnMarket AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0ENDIFONCE PerCentGain2 = 0.01 //1% increaseExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag2 = 1ENDIFIF close >= (PositionPrice * (1 + PerCentGain2)) AND LongOnMarket AND Flag2 THENSELL CloseQuantity Contracts AT MarketFlag2 = 0ENDIFONCE PerCentGain3 = 0.015 //1.5% increaseExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag3 = 1ENDIFIF close >= (PositionPrice * (1 + PerCentGain3)) AND LongOnMarket AND Flag3 THENSELL CloseQuantity Contracts AT MarketFlag3 = 0ENDIFendif02/21/2021 at 12:29 PM #162170That’s correct, but you only need the EXITING calculations and instructions only ONCE, since the next time it will still be 25% of the remaining opositions. Well… not exactly, if you BUY 8 contracts the first time you will close 2 contracts (8 * 0.25), while next time you will close 1.5 contracts (6 * 0.25), etc…
To overcome this, if you plan to always close, not a percentage, but a fixed number like the first time, you simply need to compute the number of contracts to exit the first time and use that number (instead of a percentage) for any subsequent closure.
In any case you don’t need to use 3 IF…ENDIF’s.
Of course that would be completely different if you wanted to close, say 10% the first time, then 13% the second time, 21% the third time, etc…
02/21/2021 at 12:49 PM #162175you only need the EXITING calculations and instructions only ONCE
ah yes, of course (too much copy and pasting), thanks for that!
02/21/2021 at 1:47 PM #162182that would be completely different if you wanted to close, say 10% the first time, then 13% the second time, 21% the third time
Something like this then?
1234567891011121314151617181920212223242526272829303132333435363738394041// partial closeONCE partialclose = 1if partialclose thenONCE PerCent = 0.1 //10% positions to closeONCE PerCent2 = 0.2 //20% positions to closeONCE PerCent3 = 0.4 //40% positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE PerCentGain2 = 0.01 //1% increaseONCE PerCentGain3 = 0.015 //1.5% increaseONCE 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) - LeftQty2ExitQuantity3 = abs(CountOfPosition) * PerCent3LeftQty3 = max(MinLotSize,abs(CountOfPosition) - ExitQuantity3)CloseQuantity3 = abs(CountOfPosition) - LeftQty3IF Not OnMarket THENFlag = 1 and Flag2 = 1 and Flag3 = 1ENDIFIF close >= (PositionPrice * (1 + PerCentGain)) AND LongOnMarket AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF close >= (PositionPrice * (1 + PerCentGain2)) AND LongOnMarket AND Flag2 THENSELL CloseQuantity2 Contracts AT MarketFlag2 = 0endifIF close >= (PositionPrice * (1 + PerCentGain3)) AND LongOnMarket AND Flag3 THENSELL CloseQuantity3 Contracts AT MarketFlag3 = 0ENDIFendif02/21/2021 at 2:36 PM #162194turns out that must be wrong as it either runs with no effect at all (same result as with no partial close) or it gets a parsing error (must begin and end with the same entity).
what am i doing wrong?
-
AuthorPosts
Find exclusive trading pro-tools on