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/21/2021 at 2:52 PM #162196
No, I was talking about your previous post, to exit 75% of the whole position in three steps, without using different flags and sevarel IF…ENDIF’s:
1234567891011121314151617// partial closeONCE partialclose = 1ONCE PerCent = 0.25 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimum//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0endif//IF close >= (PositionPrice * (1 + PerCentGain)) AND LongOnMarket AND CloseQuantity > 0 THENSELL CloseQuantity Contracts AT MarketENDIF02/21/2021 at 2:54 PM #162197Example on Dax, daily TF:
123456789101112131415161718192021222324// partial closeONCE partialclose = 1ONCE PerCent = 0.25 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimum//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0endif//IF close crosses over average[200] and Not OnMarket thenbuy 10 contracts at marketset target pProfit 1000set stop pLoss 500endif//IF close >= (PositionPrice * (1 + PerCentGain)) AND LongOnMarket AND CloseQuantity > 0 THENSELL CloseQuantity Contracts AT MarketENDIFgraph abs(countofposition)02/21/2021 at 3:03 PM #162199The use of [1] in line 8 is to make sure it never falls below the previous value, so that the percentage of 25%, whatever the number it is calculated on, remains unchanged.
02/21/2021 at 3:12 PM #162203So that will make a 25% partial close at each .5% increase? Doesn’t it need the flag to keep it from repeating each step?
02/21/2021 at 4:39 PM #162238You are right ,
I added Increments to my example above, so that the percentage is increased each new closure by PerCentGain steps:
1234567891011121314151617181920212223242526272829// partial closeONCE partialclose = 1ONCE PerCent = 0.25 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//IF close crosses over average[200] and Not OnMarket thenbuy 10 contracts at marketset target pProfit 1000set stop pLoss 500endif//IF close >= (PositionPrice * (1 + (PerCentGain * Increments))) AND LongOnMarket AND CloseQuantity > 0 THENSELL CloseQuantity Contracts AT MarketIncrements = Increments + 1ENDIFgraph abs(countofposition)graph PerCentGain * Incrementsgraph ExitQuantityyou can also remove and Not OnMarket to accumulate positions.
1 user thanked author for this post.
02/21/2021 at 5:32 PM #162249that looks great, and far more elegant.
but now I’m thinking it would be better to be able to alter the percent and percentgain independently. so, for example, it might close 0.3 of the position after a 0.6% gain, then 0.5 at 1.6% gain (all percentages to be optimized).
This seems to work, but it looks very clunky compared to yours:
1234567891011121314151617181920212223242526272829303132ONCE partialclose = 1ONCE PerCent = pc //10% positions to closeONCE PerCent2 = pc2 //25% positions to closeONCE PerCentGain = pcg //0.5% increaseONCE PerCentGain2 = pcg2 //1% 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) - LeftQty2IF Not OnMarket THENFlag = 1ENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF Not OnMarket THENFlag2 = 1ENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain2)) AND Flag2 THENSELL CloseQuantity2 Contracts AT MarketFlag2 = 0endifendif02/21/2021 at 6:07 PM #162258Yes, but if percentages need to be different you have no choice but do as you’ve done.
Well… you could use arrays… but that’s more complicated!
02/21/2021 at 6:16 PM #162260Ok, I’ll stay with what I’ve got then – thanks for confirming.
03/28/2021 at 4:13 AM #165530I was wondering if this code is valid, bit confused with use of ONCE function
What I am trying is – once the price trades above 10% of Positionprice, LTU condition is valid forever – even if price comes under 10% of position price. I am trying to replace ONCE PIP in above code with percentage.
1234567Once PercentLTU=1.1LTU=Close>PERCENTLTU*PositionpriceIF longonmarket and ExitClose and LTU thenSell at MARKETENDIF03/28/2021 at 6:48 AM #165538Line 3 changes every bar, so LTU isn’t valid forever!
ONCE is used only in line 1 because PercentLTU never changes.
03/28/2021 at 7:55 AM #165541thanks. Then how would it be possible to allow price to trade above the MA and then exit it when it closes below MA. For example, how to have the strategy do nothing before price trades above a Moving average after entry (eg. price is now above Daily 20 MA, but entry was well below that MA.
Please see the attached image.
03/28/2021 at 9:00 AM #165544Exit:
1234If OnMarket AND close CROSSES OVER average[20] THENSELL AT MARKET //LongEXITSHORT AT MARKET //ShortENDIF03/28/2021 at 9:04 AM #165545I am not sure I really understood your question, though.
Can you make an example with the percentage you talked about?
03/28/2021 at 9:27 AM #1655471234567891011121314Timeframe (daily)MA=Average[20]Dailyclose=close//Entry price is well below daily MA or Daily MA is still bearish at EntryTimeframe (1 Hour)If price crosses over Average [10] thenBuy 1 contract at MarketEndif//Below condition is triggered when Daily MA tries to turn bullish and exit trade prematurelyIf longonmarket and Dailyclose<MA thenSell at marketEndifEntry is no issue. Problem is that the entry is below the daily MA, as the price hovers around the daily MA in the process of turning bullish for first time after trade entry, the exit condition is triggered. To solve this i wanted price to trade 20% above positionprice. Once the 20% mark is reached, the exit condition always remains true. For example, If entry price is $10, price goes to $15 and then returns to $11 – I want to exit if Dailyclose<MA in below code.
123456Once PercentLTU=1.1LTU<span class="crayon-o">=</span><span class="crayon-st">Close</span><span class="crayon-o">></span>PERCENTLTU<span class="crayon-o">*</span><span class="crayon-st">Positionprice</span>If longonmarket and Dailyclose<MA and LTU thenSell at marketEndifI was assuming the below code does what I am saying above. I wanted to replace pips with percentage. For below code when price trades above 20 pips from position price, the condition is still true if the price comes back to say 10 pips from position price.
1234567891011ONCE PerCent = 0.5 //50% = positions to closeONCE Pips = 20 * PipSizeONCE MinLotSize = 0.5 //0.5 lots minimumExitQuantity = abs(CountOfPosition) * PerCentRemainQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - RemainQtyIF close >= (PositionPrice + Pips) AND LongOnMarket THENSELL CloseQuantity Contracts AT MarketELSIF close <= (PositionPrice - Pips) AND ShortOnMarket THENEXITSHORT CloseQuantity Contracts AT MarketENDIF03/28/2021 at 10:49 AM #165549This is the code you need (not tested):
12345678910111213141516171819Timeframe (daily)MA=Average[20]Dailyclose=close//Entry price is well below daily MA or Daily MA is still bearish at EntryTimeframe (1 Hour)IF Not OnMarket THENExitFlag = 0ENDIFIf price crosses over Average [10] thenBuy 1 contract at MarketEndifIF close >= (PositionPrice * 1.2) THENExitFlag = 1 //Signal when current price reaches +20%ENDIF//Below condition is triggered when Daily MA tries to turn bullish and exit trade prematurelyIf longonmarket and Dailyclose<MA AND ExitFlag thenSell at marketEndifI just added a flag to signal when exit is allowed (after reaching +20%) once it retraces below MA.
-
AuthorPosts
Find exclusive trading pro-tools on