How can I cancel a stop entry order intrabar??
Forums › ProRealTime English forum › ProOrder support › How can I cancel a stop entry order intrabar??
- This topic has 9 replies, 3 voices, and was last updated 5 years ago by Finning.
-
-
09/19/2019 at 1:46 AM #107907
Hi all,
I have the question – can you cancel an stop entry order – intrabar?
Let’s say I’m running the code below:
123456789If TEMA>TEMA[1] thenbuy positionsize contract at close+4.1 stopsellshort positionsize contracts at close-4.1 stopendifset target profit 0.5set stop loss 4.1I would like to be able to, let’s say if the buy stop is activated, and the target profit is taken, I would then like to be able to take the sellshort off the market – all within the same bar – so I’m not left exposed after the profit has already been made.
I guess I’m putting my own spread on the market, which if it then crosses I’m in profit – either way.
It would just work a hell of a lot better if I could eliminate my after profit residual risk – the second leg which is a stop that could take my profit away.
It would be even better if I could eliminate the second stop after the 1st one is activated, regardless of profit.
Another – bundled – question. I’m doing “buy positionsize contract at close+4.1 stop” – Ideally I want “buy positionsize contract at market+X” – can X be smaller than the market stop size (listed at 4)? If so, how would I code that?
Thanks for your help,
Finning
09/19/2019 at 7:27 AM #107910Pending orders ALWAYS expire after one bar, all you can do is not to place them, or one of them, if you don’t need it anymore.
There’s no way to cancel pending orders while a bar is being built.
The only workaround is to resort to MTF, Multiple Time Frame, support. With it you can use your current TF for setup, while placing orders on a smaller TF, say 1 minute or 10 seconds. Orders would still last a whole bar, but being much smaller they would have a greater chance not to be both triggered on the same one.
As for your example above be warned that it can only work on some instruments, such as Dax, but not with Forex pairs, since you are not using PIPSIZE to convert pips into a price (profit and loss require a difference in prices). If you want to use pips you should use Ploss and Pprofit, instead.
1 user thanked author for this post.
09/19/2019 at 9:21 AM #107923The problem with using an MTF faster time frame to place the pending orders is that in for example the 1 second time frame price will slowly creep second by second closer to your order price but once it is under the brokers minimum stop distance then all orders will be rejected and so you will no longer be able to enter at the desired price.
09/19/2019 at 9:58 AM #107925Yes Vonasi, the MTF solution can be mainly appreciated by those who use to set their conditions on a TF of at least 30+ minutes, so that a 1-minute (I do not recommend using a TF lower than that) default TF to place pending orders is likely to be enough to meet the broker’s minimum distance requirements.
Best options are IMO 1-hour as setup TF and 5-minute or 1-minute as default TF, or a Daily TF for setup and a 30-minute or 15-minute or 10-minute default TF. These are just examples.
Using a 5-minute setup and a 5-second TF is likely not to work as expected!
09/19/2019 at 12:00 PM #107931One possible compromise would be to first of all start placing our two orders on a 1 second time frame so that if in a sudden moment of volatility one of them got hit in a spike then the opposite one was instantly cancelled – but if price slowly creeps up or slowly creeps down towards one of our orders then once it starts to get close and whilst we are far enough away to not get rejected orders then we stop placing the orders every second and switch to placing just one order (the one we are closest to) on say the 5 minute time frame. This allows our order to remain on the market for five minutes without any possibility of it being rejected. Yes price might reverse and we miss the opposite trade but at least we have removed the risk of opening a trade in one direction only to have it closed quickly and one opened in the opposite direction resulting in a guaranteed loss.
There’s a coding challenge for someone!
09/19/2019 at 12:36 PM #107935Maybe this (just tested for syntax errors):
12345678910111213141516171819202122232425262728293031323334353637383940DEFPARAM CumulateOrders = falseTIMEFRAME(default)ONCE Offset = 10 * pipsizeONCE Psize = 1ONCE UseDefaultTF = 1//// when the difference gets too close to ENTRYPRICE (<= offset * 1.5) switch to 5-minute TF, instead of the default one//IF Not OnMarket AND AvgCond = 1 THENIF abs(close - EntryPrice) <= (Offset * 1.5) THENUseDefaultTF = 0ENDIFENDIF//// when OnMarket switch back to the default TF//IF OnMarket THENUseDefaultTF = 1ENDIF//TIMEFRAME(5 minute,updateonclose)MyTema = Tema[10](close)//AvgCond = MyTema > MyTema[1]IF AvgCond = 1 THENEntryPrice = closeENDIF//IF AvgCond AND Not OnMarket AND UseDefaultTF = 0 THENBUY Psize CONTRACTS AT EntryPrice + Offset STOPSELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOPENDIF//TIMEFRAME(default)IF AvgCond AND Not OnMarket AND UseDefaultTF = 1 THENBUY Psize CONTRACTS AT EntryPrice + Offset STOPSELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOPENDIFSET Target pProfit 60SET Stop pLoss 301 user thanked author for this post.
09/19/2019 at 1:06 PM #10794109/19/2019 at 1:53 PM #107948Something like this I guess. Not tested.
12345678910111213141516171819202122232425262728293031323334353637383940414243DEFPARAM CumulateOrders = falseTIMEFRAME(default)ONCE Offset = 10 * pipsizeONCE Psize = 1ONCE UseDefaultTF = 1//// when the difference gets too close to ENTRYPRICE (<= offset * 1.5) switch to 5-minute TF, instead of the default one//IF Not OnMarket AND AvgCond = 1 THENIF abs(close - EntryPrice) <= (Offset * 1.5) THENUseDefaultTF = 0ENDIFENDIF//// when OnMarket switch back to the default TF//IF OnMarket THENUseDefaultTF = 1ENDIF//TIMEFRAME(5 minute,updateonclose)MyTema = Tema[10](close)//AvgCond = MyTema > MyTema[1]IF AvgCond = 1 THENEntryPrice = closeENDIF//IF AvgCond AND Not OnMarket AND UseDefaultTF = 0 THENif (entryprice + Offset) - close < close - (EntryPrice - Offset) thenBUY Psize CONTRACTS AT EntryPrice + Offset STOPendifif (entryprice + Offset) - close > close - (EntryPrice - Offset) thenSELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOPENDIF//TIMEFRAME(default)IF AvgCond AND Not OnMarket AND UseDefaultTF = 1 THENBUY Psize CONTRACTS AT EntryPrice + Offset STOPSELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOPENDIFSET Target pProfit 60SET Stop pLoss 3009/19/2019 at 11:09 PM #108020Hi Vonasi/Roberto,
have attached a code that I’m running at the moment. On the Australia 200 ($1 or $5 market) – but – you must set your time to UTC+10/Brisbane time – so that the code operates in the spread=1 hours.\
Have had the problem that the platform can’t get/runs out of tick data, and that’s why the test stops where it does. Just have to go through and do different tests at different (x) units, but the test I did shown gives a good number of trades at least in up/down conditions.
Thank you for your code ideas – I don’t have MTF yet I just have 10.3 on IG – I’ll have to look at trying to get that upgrade then.
Haven’t fully studied your posts above but I will in detail – just sending this now to add fuel to fire.
A couple of things:
- Would be great if you could defeat the market stop limit of 4 – eg – is “If close<close[1]-2 then sell at market” – would this work not as a stop order but just as a market order? Even if it’s the open of the next bar it would be functional – just drop timeframe.
- And again (I can see that it’s touched on above), being able to buy within that market stop limit of 4 – so effectively being able to buy at “buy at market +2” would open up a whole world of opportunities with this method.
To be fair I have tried to make this briefly work with FTSE and DAX, but their market structure seems to be a lot more complex (market moves up and down through the open point a lot more in a bar or reverts before a move is completed) – or – it needs the functionality of the above 2 problems solved would help a great deal.
I guess what you could call what I’m trying to do a breakout from a spread?
Cheers,
Finning.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318//-------------------------------------------------------------------------// Main code : Intrabar asx200 15min 5 dol//-------------------------------------------------------------------------defparam cumulateorders = falsedefparam flatbefore = 095900defparam flatafter = 155900////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Optimal f position sizingpsize = 1pvalue = 5Usedfromaccount = 0.05Invested = (1747+strategyprofit)*UsedfromaccountTier1Margin = 0.005Tier1Max = 315Tier1DolPerContract = Tier1Margin*(close/Psize)*pvalueContDolinTier1 = Tier1Margin*(close/Psize)*pvalue*Tier1MaxTier2Margin = 0.01Tier2Max = 3150Tier2DolPerContract = Tier2Margin*(close/Psize)*pvalue//ContDolinTier2 = Tier2Margin*(close/Pipsize)*pointvalue*Tier2Maxa = Invested/Tier1DolPerContract //This is initial contracts if all on tier 1 marginIf (a>Tier1Max) then //Tier 1 number of contractsa = 315b = (Invested - ContDolinTier1)/Tier2DolPerContractelseb = 0endifd = a + b // this is the total number of tiered contractsif d > Tier2Max thend = Tier2Maxendifpositionsize = round(d)/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Total Margin CalculationIf countofposition <=315 thenee = countofpositionelseif countofposition>315 thenrr = countofposition-315endifendif//TotalMargin2 = ABS((ee * Tier1DolPerContract) + (rr * Tier2DolPerContract))TotalMargin = ABS((ee * Tier1DolPerContract) + (rr * Tier2DolPerContract))if not onmarket thentotalmargin = 0endif////////////////////////////////////////////////////////////////////////////////////////////////////////RVComb, RVCombTEMA = CALL "RV comb"[20,20]don8 = CALL "8 average direction Donchian"[2]av = triangularaverage[60](weightedclose)RC =ROC[2](typicalprice)atr = AverageTrueRange[2](totalprice)RVComb=RVCombRVCombTEMA=RVCombTEMAatr = atrdon8=don8/////////////////////////////////////////////////////////////////////////////////////////////// Using triangular averageIf RVCombTEMA>RVCombTEMA[1] and don8>2 and av>av[1] thenbuy positionsize contract at close+4.1 stopsellshort positionsize contracts at close-4.1 stopendifIf RVCombTEMA<RVCombTEMA[1] and don8<7 and av<av[1] thensellshort positionsize contract at close-4.1 stopbuy positionsize contracts at close+4.1 stopendif/////////////////////////////////////////////////////////////////////////////////////////////// No Triangular average but strong Donchian trend and price ROCIf RVCombTEMA>RVCombTEMA[1] and don8=8 and RVComb>8 and RC>0 thenbuy positionsize contract at close+4.1 stopsellshort positionsize contracts at close-4.1 stopendifIf RVCombTEMA>RVCombTEMA[1] and don8=1 and RVComb<-8 and RC<0 thensellshort positionsize contract at close-4.1 stopbuy positionsize contracts at close+4.1 stopendif////////////////////////////////////////////////////////////////////////////////////////////If abs(RC)<=0.03 thenset target profit 0.4endifIf abs(RC)<=0.05 thenset target profit 0.5endifIf abs(RC)>0.05 thenset target profit 0.6endifIf abs(RC)>0.06 thenset target profit 0.7endifIf abs(RC)>0.07 thenset target profit 0.8endifIf abs(RC)>0.08 thenset target profit 0.9endifset stop loss 4.1if strategyprofit < -150 thenquitendif//if totalmargin>invested then//quit//endiftotalmargin=totalmargin//graph positionsize*Tier1DolPerContract//graph strategyprofit+1747if positionsize*Tier1DolPerContract>strategyprofit+1747 thenquitendif//-------------------------------------------------------------------------// Function : RV comb//-------------------------------------------------------------------------RV = CALL "Recent Volatility"[a]RV2 = CALL "Recent Volatility Negative"[a]dd = abs(RV2) - abs(RV)TMA = TEMA[AV](dd)return dd as "RV Comb", TMA as "RV Comb TEMA"//-------------------------------------------------------------------------// Function : 8 average direction Donchian//-------------------------------------------------------------------------//short termUpper = HIGHEST[a](HIGH[0])If Upper > upper[1] thenUcol = 1elseif Upper = upper[1] and ucol = 1 thenucol = 1endifendifif upper < upper[1] thenucol = 0elseif upper = upper[1] and ucol = 0 thenucol = 0endifendif//////////////////////////////////////////////////////////////////////Lower = Lowest[a](Low[0])If Lower > lower[1] thenlcol = 1elseif Lower = Lower[1] and lcol = 1 thenlcol = 1endifendifif lower < lower[1] thenlcol = 0elseif lower = lower[1] and lcol = 0 thenlcol = 0endifendif///////////////////////////////////////////////////////////////////////Mid = (Upper + Lower) / 2If Mid > Mid[1] thenmcol = 1elseif Mid = Mid[1] and mcol = 1 thenmcol = 1endifendifif Mid < Mid[1] thenmcol = 0elseif Mid = Mid[1] and mcol = 0 thenmcol = 0endifendifif lcol=1 and mcol=1 and ucol=1 thenc10 = 8endifif lcol=0 and mcol=1 and ucol=1 thenc10 = 7endifif lcol=1 and mcol=0 and ucol=1 thenc10 = 6endifif lcol=0 and mcol=0 and ucol=1 thenc10 = 5endifif lcol=1 and mcol=1 and ucol=0 thenc10 = 4endifif lcol=0 and mcol=1 and ucol=0 thenc10 = 3endifif lcol=1 and mcol=0 and ucol=0 thenc10 = 2endifif lcol=0 and mcol=0 and ucol=0 thenc10 = 1endifreturn c10//-------------------------------------------------------------------------// Function : Recent Volatility Negative//-------------------------------------------------------------------------ll = lowest[p](low)dd = (typicalprice - ll)return dd//-------------------------------------------------------------------------// Function : Recent Volatility//-------------------------------------------------------------------------HH = highest[p](high)dd = (hh-typicalprice)return dd09/19/2019 at 11:23 PM #108024Of course the other problem that I haven’t mentioned with this method is the close+4.1 or close-4.1 entry point… you don’t get the reliable fills that you do as what you would have with “at market”. Seems to be a lot of cancelled and amended orders in my trade history. Would be fantastic to have a “at market +2” or “at market -2” functionality – effectively if the market breaks out in a direction at a distance from the current bar’s open – which is what the close+4.1 is trying to do.
-
AuthorPosts
Find exclusive trading pro-tools on