How can I cancel a stop entry order intrabar??

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #107907 quote
    Finning
    Participant
    Veteran

    Hi all,

    I have the question – can you cancel an stop entry order – intrabar?

    Let’s say I’m running the code below:

    If TEMA>TEMA[1] then
    
    buy positionsize contract at close+4.1 stop
    sellshort positionsize contracts at close-4.1 stop
    
    endif
    
    set target profit 0.5
    set stop loss 4.1

    I 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

    #107910 quote
    robertogozzi
    Moderator
    Master

    Pending 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.

    Nicolas thanked this post
    #107923 quote
    Vonasi
    Moderator
    Master

    The 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.

    #107925 quote
    robertogozzi
    Moderator
    Master

    Yes 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!

    #107931 quote
    Vonasi
    Moderator
    Master

    One 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!

    #107935 quote
    robertogozzi
    Moderator
    Master

    Maybe this (just tested for syntax errors):

    DEFPARAM CumulateOrders = false
    TIMEFRAME(default)
    ONCE Offset       = 10 * pipsize
    ONCE Psize        = 1
    ONCE 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 THEN
       IF abs(close - EntryPrice) <= (Offset * 1.5) THEN
          UseDefaultTF = 0
       ENDIF
    ENDIF
    //
    // when OnMarket switch back to the default TF
    //
    IF OnMarket THEN
       UseDefaultTF = 1
    ENDIF
    //
    TIMEFRAME(5 minute,updateonclose)
    MyTema  = Tema[10](close)
    //
    AvgCond = MyTema > MyTema[1]
    IF AvgCond = 1 THEN
       EntryPrice = close
    ENDIF
    //
    IF AvgCond AND Not OnMarket AND UseDefaultTF = 0 THEN
       BUY       Psize CONTRACTS AT EntryPrice + Offset STOP
       SELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOP
    ENDIF
    //
    TIMEFRAME(default)
    IF AvgCond AND Not OnMarket AND UseDefaultTF = 1 THEN
       BUY       Psize CONTRACTS AT EntryPrice + Offset STOP
       SELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOP
    ENDIF
    SET Target pProfit 60
    SET Stop   pLoss   30
    Vonasi thanked this post
    #107941 quote
    Vonasi
    Moderator
    Master

    The only thing that is missing compared to my original suggestion is that when we switch to the slower time frame we need to only place one order – the one that price is closest to.

    #107948 quote
    Vonasi
    Moderator
    Master

    Something like this I guess. Not tested.

    DEFPARAM CumulateOrders = false
    TIMEFRAME(default)
    ONCE Offset       = 10 * pipsize
    ONCE Psize        = 1
    ONCE 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 THEN
       IF abs(close - EntryPrice) <= (Offset * 1.5) THEN
          UseDefaultTF = 0
       ENDIF
    ENDIF
    //
    // when OnMarket switch back to the default TF
    //
    IF OnMarket THEN
       UseDefaultTF = 1
    ENDIF
    //
    TIMEFRAME(5 minute,updateonclose)
    MyTema  = Tema[10](close)
    //
    AvgCond = MyTema > MyTema[1]
    IF AvgCond = 1 THEN
       EntryPrice = close
    ENDIF
    //
    IF AvgCond AND Not OnMarket AND UseDefaultTF = 0 THEN
    if (entryprice + Offset) - close < close - (EntryPrice - Offset) then
       BUY       Psize CONTRACTS AT EntryPrice + Offset STOP
    endif
    if (entryprice + Offset) - close > close - (EntryPrice - Offset) then
       SELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOP
    ENDIF
    //
    TIMEFRAME(default)
    IF AvgCond AND Not OnMarket AND UseDefaultTF = 1 THEN
       BUY       Psize CONTRACTS AT EntryPrice + Offset STOP
       SELLSHORT Psize CONTRACTS AT EntryPrice - Offset STOP
    ENDIF
    SET Target pProfit 60
    SET Stop   pLoss   30
    #108020 quote
    Finning
    Participant
    Veteran

    Hi 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.

     

    //-------------------------------------------------------------------------
    // Main code : Intrabar asx200 15min 5 dol
    //-------------------------------------------------------------------------
    defparam cumulateorders = false
    defparam flatbefore = 095900
    defparam flatafter = 155900
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Optimal f position sizing
    
    psize = 1
    pvalue = 5
    
    Usedfromaccount = 0.05
    
    Invested = (1747+strategyprofit)*Usedfromaccount
    
    Tier1Margin = 0.005
    Tier1Max = 315
    Tier1DolPerContract = Tier1Margin*(close/Psize)*pvalue
    ContDolinTier1 = Tier1Margin*(close/Psize)*pvalue*Tier1Max
    
    Tier2Margin = 0.01
    Tier2Max = 3150
    Tier2DolPerContract = Tier2Margin*(close/Psize)*pvalue
    //ContDolinTier2 = Tier2Margin*(close/Pipsize)*pointvalue*Tier2Max
    
    a = Invested/Tier1DolPerContract //This is initial contracts if all on tier 1 margin
    
    If (a>Tier1Max) then     //Tier 1 number of contracts
    a = 315
    b = (Invested - ContDolinTier1)/Tier2DolPerContract
    else
    b = 0
    endif
    
    d = a + b   // this is the total number of tiered contracts
    
    if d > Tier2Max then
    d = Tier2Max
    endif
    
    positionsize = round(d)
    
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Total Margin Calculation
    If countofposition  <=315 then
    ee = countofposition
    else
    if countofposition>315 then
    rr = countofposition-315
    endif
    endif
    
    //TotalMargin2 = ABS((ee * Tier1DolPerContract) + (rr * Tier2DolPerContract))
    TotalMargin = ABS((ee * Tier1DolPerContract) + (rr * Tier2DolPerContract))
    
    if not onmarket then
    totalmargin = 0
    endif
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    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=RVComb
    RVCombTEMA=RVCombTEMA
    atr = atr
    don8=don8
    
    
    /////////////////////////////////////////////////////////////////////////////////////////////
    // Using triangular average
    
    If RVCombTEMA>RVCombTEMA[1] and don8>2 and av>av[1]  then
    
    buy positionsize contract at close+4.1 stop
    sellshort positionsize contracts at close-4.1 stop
    
    endif
    
    
    If RVCombTEMA<RVCombTEMA[1] and don8<7 and av<av[1]  then
    
    sellshort positionsize contract at close-4.1 stop
    buy positionsize contracts at close+4.1 stop
    
    endif
    
    /////////////////////////////////////////////////////////////////////////////////////////////
    // No Triangular average but strong Donchian trend and price ROC
    
    If RVCombTEMA>RVCombTEMA[1] and don8=8 and RVComb>8 and RC>0  then
    
    buy positionsize contract at close+4.1 stop
    sellshort positionsize contracts at close-4.1 stop
    
    endif
    
    If RVCombTEMA>RVCombTEMA[1] and don8=1 and RVComb<-8 and RC<0  then
    
    sellshort positionsize contract at close-4.1 stop
    buy positionsize contracts at close+4.1 stop
    
    endif
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    
    If abs(RC)<=0.03 then
    set target profit 0.4
    endif
    
    If abs(RC)<=0.05 then
    set target profit 0.5
    endif
    
    If abs(RC)>0.05 then
    set target profit 0.6
    endif
    
    If abs(RC)>0.06 then
    set target profit 0.7
    endif
    
    If abs(RC)>0.07 then
    set target profit 0.8
    endif
    
    If abs(RC)>0.08 then
    set target profit 0.9
    endif
    
    
    
    set stop loss 4.1
    
    if strategyprofit < -150 then
    quit
    endif
    
    //if totalmargin>invested then
    //quit
    //endif
    totalmargin=totalmargin
    //graph positionsize*Tier1DolPerContract
    //graph strategyprofit+1747
    
    
    if positionsize*Tier1DolPerContract>strategyprofit+1747 then
    quit
    endif
    
    
    //-------------------------------------------------------------------------
    // 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 term
    Upper = HIGHEST[a](HIGH[0])
    
    If Upper > upper[1]  then
    Ucol = 1
    else
    if Upper = upper[1] and  ucol = 1 then
    ucol = 1
    endif
    endif
    
    if upper < upper[1]  then
    ucol = 0
    else
    if upper = upper[1] and ucol = 0  then
    ucol = 0
    endif
    endif
    //////////////////////////////////////////////////////////////////////
    Lower = Lowest[a](Low[0])
    
    If Lower > lower[1]  then
    lcol = 1
    else
    if Lower = Lower[1] and  lcol = 1 then
    lcol = 1
    endif
    endif
    
    if lower < lower[1]  then
    lcol = 0
    else
    if lower = lower[1] and lcol = 0  then
    lcol = 0
    endif
    endif
    ///////////////////////////////////////////////////////////////////////
    
    Mid = (Upper + Lower) / 2
    
    If Mid > Mid[1]  then
    mcol = 1
    else
    if Mid = Mid[1] and  mcol = 1 then
    mcol = 1
    endif
    endif
    
    if Mid < Mid[1]  then
    mcol = 0
    else
    if Mid = Mid[1] and mcol = 0  then
    mcol = 0
    endif
    endif
    
    
    
    
    
    
    
    
    if lcol=1 and mcol=1 and ucol=1  then
     
    c10 = 8
    
    endif
    
    if lcol=0 and mcol=1 and ucol=1  then
     
    c10 = 7
    
    endif
    
    if lcol=1 and mcol=0 and ucol=1  then
     
    c10 = 6
    
    endif
    
    if lcol=0 and mcol=0 and ucol=1  then
     
    c10 = 5
    
    endif
    
    if lcol=1 and mcol=1 and ucol=0  then
     
    c10 = 4
    
    endif
    
    if lcol=0 and mcol=1 and ucol=0  then
     
    c10 = 3
    
    endif
    
    if lcol=1 and mcol=0 and ucol=0  then
     
    c10 = 2
    
    endif
    
    if lcol=0 and mcol=0 and ucol=0  then
     
    c10 = 1
    
    endif
    
    
    return 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 dd
    
    1.jpg 1.jpg 2.jpg 2.jpg 2-1.jpg 2-1.jpg
    #108024 quote
    Finning
    Participant
    Veteran

    Of 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.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

How can I cancel a stop entry order intrabar??


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Finning @finning Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by Finning
6 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/19/2019
Status: Active
Attachments: 3 files
Logo Logo
Loading...