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.
-
-
12/20/2020 at 11:09 AM #154549
As to my code, Vonasi realized that the error is due to the fact the the name of that variable starts with REM which is a reserved word for COMMENTS (like double slashes).
Just replace Rem with MyRem and it should work.
12/20/2020 at 11:23 AM #15455102/16/2021 at 10:29 AM #16161302/16/2021 at 11:26 AM #161621There you go:
12345678910111213ONCE 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 close >= (PositionPrice + Pips) AND LongOnMarket THENSELL CloseQuantity Contracts AT MarketQUITELSIF close <= (PositionPrice - Pips) AND ShortOnMarket THENEXITSHORT CloseQuantity Contracts AT MarketQUITENDIFQUIT will exit the strategy from AutoTrading. Any open position will either be kept open or closed according to the option you have selected in the platform.
02/16/2021 at 11:48 AM #161628thanks Roberto, but I meant – once 50% position is closed, then no further 50% close even when condition is met.
To clarify,
I want 50% of positions to close when Condtion1 is met, the above code already does this, but the code closes 50% position each time the condition is met – I need it to run 50% close only once.
For the remaining 50% I have another set of condition. This 50% remaining position is intended for larger trend ride.
I have this code for closing 50%, but the conter concept isn’t working
123456789Counter=0ONCE Percent = 0.50 //0.50 = close half positionsCloseQuantity=(abs(CountOfPosition)*Percent)IF STUExitCondition and counter<=1 AND LongOnMarket THENSell CloseQuantity Contract AT MARKETELSIF STUExitCondition AND ShortOnMarket THENEXITSHORT CloseQuantity Contract AT Marketcounter=counter+1ENDIF02/16/2021 at 11:55 AM #161631Bob’s your uncle:
12345678910111213141516ONCE 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 >= (PositionPrice + Pips) AND LongOnMarket AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag THENEXITSHORT CloseQuantity Contracts AT MarketFlag = 0ENDIF02/16/2021 at 12:10 PM #161637this code didnt work, it kept closing the position by half each time condition was met, I have this code but the counter is not working,
See image below, I need the last two orders ie. Buy 233 contract then sell 116.5 contract. Once this is done, I don’t need the below code to run as I will close the remaining order based on another set of codes.
Hope it makes sense
123456789Once Counter=0ONCE Percent = 0.50 //0.50 = close half positionsCloseQuantity=(abs(CountOfPosition)*Percent)IF STUExitCondition and counter<=1 AND LongOnMarket THENSell CloseQuantity Contract AT MARKETELSIF STUExitCondition AND ShortOnMarket THENEXITSHORT CloseQuantity Contract AT Marketcounter=counter+1ENDIF02/16/2021 at 12:42 PM #161645It works like a charm for me:
12345678910111213141516171819202122232425262728ONCE 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 not OnMarket THENbuy 1 contract at marketendifIF close crosses under lowest[20](low[1]) and not OnMarket THENsellshort 1 contract at marketendifIF 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 600set stop ploss 100graph abs(countofposition)graph Flaggraph positionperf*positionpricegraph positionpriceonce half position is closed, no further action is taken.
02/16/2021 at 1:18 PM #161654I tried it. With BUY 1 Contract as in code above, it works.
But when I change the 1 contract to variable size of PositionSize Contract, the code closes 50% position multiple times. This makes no sense – it should work exactly same with Postitionsize contract as well I think
02/16/2021 at 1:27 PM #161656It’s weird, it trades when FLAG=1 which is set when NOT OnMarket.
Once a position is partially closed, FLAG is set to 0 and can’t be set again to 1 until ALL positions are closed.
Try using this at lines 16 and 19:
1AND Flag = 102/16/2021 at 1:35 PM #161657Looks like the code was working only because the buy quantity was set to 1. If I change it to anything else it does not work. I tried flag=1, no difference See below the exact code,’
1234567891011121314151617181920212223ONCE 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 not OnMarket THENbuy 10 contract at marketSet Stop Loss SLendifIF close crosses under lowest[20](low[1]) and not OnMarket THENsellshort Positionsize contract at marketendifIF 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 = 0ENDIF02/16/2021 at 4:12 PM #161676It works perfectly for me, with 10 positions.
I noticed that you are using POSITIONSIZE.
If you set POSITIONSIZE to an inituial value, than halve it without restoring its initial value when not onmarket, then the next trade it will be halved once more. I guess this might be the cause.
Simply add this line after line 7:
1PositionSize = X //restore initial value02/16/2021 at 4:33 PM #161679I tried with 10 position size, it doesn’t work. See the image, this is the code I ran
What am I doing wrong? When I change to quantity to 1, it works. Neigher a variable position size nor anything above 1 is working for me
12345678910111213141516171819202122232425ONCE 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 NOT OnMarket And ENTRY and OrderAllowed>0 THENBUY 10 CONTRACT AT MARKET//set stop Loss sl//set target $profit 50endifIF 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 = 0ENDIF02/16/2021 at 5:08 PM #161687Use the GRAPH instructions that I posted, it’s better to spot any weird change in values, candle by candle.
02/17/2021 at 3:31 AM #161741 -
AuthorPosts
Find exclusive trading pro-tools on