Cancelling pending orders

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #148591 quote
    Khaled
    Participant
    Veteran

    Good evening, I’m having a similar issue and tried various combinations but couldn’t solve it. I’m running a small programme on TF 15 minutes. I’ve read a few posts thanks to Nicolas which say that non executed STOP Orders are cancelled at the end of the candle. This doesn’t work in my case. So I tried to add the following lines and still doesn’t cancel the pending orders. The objective is to auto cancel the non executed orders at the end of the 15 minutes candle. I’m really stuck and this results either in keeping orders with irrelevant conditions. Please help me see clearer. Many thanks.

    ONCE LONG = 0
    ONCE SHORT = 0
    ONCE NbBarLimit = 1
    
    IF LongOnMarket THEN
    LONG = 1
    MyIndex = Barindex
    ENDIF
    
    IF ShortOnMarket THEN
    SHORT = 1
    MyIndex = Barindex
    ENDIF
    
    IF (Barindex >= (MyIndex + NbBarLimit)) THEN
    LONG = 0
    SHORT = 0
    ENDIF
    
    IF LONG=0 THEN
    Varible A = max ( Value X1 , Value Y1)
    ENDIF
    
    IF SHORT=0 THEN
    Varible A = max ( Value X2 , Value Y2)
    ENDIF
    #148596 quote
    robertogozzi
    Moderator
    Master

    Pending orders are automatically cancelled every bar.

    If  one of them keeps being placed again is because somewhere you have written your strategy to.

    Khaled thanked this post
    #148598 quote
    Khaled
    Participant
    Veteran

    Thank you Roberto for taking the time. Can you please help me with what’s wrong in my code that makes the pending orders alive after the end of the 15 minutes candle? Much appreciated.

    defparam cumulateorders = false 
    defparam preloadbars    = 2000
    TIMEFRAME (15 minutes)
    N = 1 //Number of Contracts
    /////////////////////////////////////////
    // CALCULATION OF VALUE X AND VALUE Y  //
    ////////////////////////////////////////
    
    boxsizeL = max ( ValueX , 20)
    boxsizeS = max ( ValueY , 10)
    
    renkomaxL = round(close / boxsizeL) * boxsizeL
    renkominL = renkomaxL - boxsizeL
    
    renkomaxS = round(close / boxsizeS) * boxsizeS
    renkominS = renkomaxS - boxsizeS
    
    if high > renkomaxl + boxsizel then
    renkomaxl = renkomaxl + boxsizel
    renkominl = renkominl + boxsizel
    endif
    if low < renkomins - boxsizes then
    renkomaxs = renkomaxs - boxsizes
    renkomins = renkomins - boxsizes
    endif
    
    
    IF time>=133000 AND time>200000 then
    spread = 0.5
    ELSIF time>=200000 AND time>220000 then
    spread = 2.5
    ELSIF time>=220000 AND time>133000 then
    spread = 1
    ENDIF
    
    IF NOT ONMARKET THEN
    BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
    SELLSHORT N CONTRACT at (renkoMinS - boxSizeS - spread) STOP
    ENDIF
    
    SET STOP pTRAILING 5
    SET TARGET pPROFIT 1000
    #148599 quote
    Khaled
    Participant
    Veteran

    Roberto, can it be the pTRAILINGSTOP that keeps the pending order alive after the end of the candle? Thanks

    #148600 quote
    robertogozzi
    Moderator
    Master

    No, it’s because at line 36 the only condition to enter a trade is NOT ONMARKET, so when this condition is true it opens a new position.

    You need to add another condition. When do you want your trades to be opened?

    Khaled thanked this post
    #148602 quote
    Khaled
    Participant
    Veteran

    Roberto, you’re “l’As des As”, THE MASTER! It was simple, but it kept me awake for a few days… Thank you so much!!!!

    #148677 quote
    Khaled
    Participant
    Veteran

    Roberto, I’ve spoken a little bit too quickly I think. I’ve tried a few conditions to enter a trade and the problem persists: pending orders are not cancelled at the end of each candle, and I keep carying a pending order with irrelevant entry conditions for a few hours. So, I’ve just removed all the conditions to trigger a trade and the result is the same. I stopped the Algo after the Orders have been sent and still they are not cancelled. Below you will see a snaphopt of two pending orders on 3min TF. The most recent is the Short one (3.18pm), but the Long (3.15pm) should have been cancelled.

    The most important question for me is how to get the pending order cancelled, not how to get a new one executed.

    Thanks for your help.

    defparam cumulateorders = false 
    defparam preloadbars    = 2000
    TIMEFRAME (15 minutes)
    N = 1 //Number of Contracts
    Condition = 1
    /////////////////////////////////////////
    // CALCULATION OF VALUE X AND VALUE Y  //
    ////////////////////////////////////////
     
    boxsizeL = max ( ValueX , 20)
    boxsizeS = max ( ValueY , 10)
     
    renkomaxL = round(close / boxsizeL) * boxsizeL
    renkominL = renkomaxL - boxsizeL
     
    renkomaxS = round(close / boxsizeS) * boxsizeS
    renkominS = renkomaxS - boxsizeS
     
    if high > renkomaxl + boxsizel then
    renkomaxl = renkomaxl + boxsizel
    renkominl = renkominl + boxsizel
    endif
    if low < renkomins - boxsizes then
    renkomaxs = renkomaxs - boxsizes
    renkomins = renkomins - boxsizes
    endif
     
     
    IF time>=133000 AND time>200000 then
    spread = 0.5
    ELSIF time>=200000 AND time>220000 then
    spread = 2.5
    ELSIF time>=220000 AND time>133000 then
    spread = 1
    ENDIF
     
    IF CONDITION=1 THEN
    BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
    SELLSHORT N CONTRACT at (renkoMinS - boxSizeS - spread) STOP
    ENDIF
     
    SET STOP pTRAILING 5
    SET TARGET pPROFIT 1000
    20201027_15h18_orders-list.png 20201027_15h18_orders-list.png
    #148686 quote
    robertogozzi
    Moderator
    Master

    Pending orders ARE ALWAYS cancelled every candle (in your case every 15 minutes). If you don’t focus on this you will never be able to understand why this happens.

    If ProOrder keeps placing them is because your strategy KEEPS placing them again and again.

    You were using only NOT ONMARKET, which is true whenever there’s no open position. So I suggested you to add another condition to filter entries and you replaced NOT ONMARKET (instead of adding a new one) with CONDITION=1  which is set  at the beginning and NEVER changes, so it will always be true candle after candle and pending orders will be entered each time. That’s why nothing changed.

    Choose a condition that makes sense to you and use it. YOU know what condition must be used, I can’t be of any help, just as an example I could write:

    IF Not OnMarket AND close CROSSES OVER average[200,0](close) THEN

    I used two conditions, but you can use just one or add more.

    #148695 quote
    robertogozzi
    Moderator
    Master

    I decided to make a topic of its own because:

    • the subject is a little be different
    • the topic is growing
    Khaled thanked this post
    #148706 quote
    Khaled
    Participant
    Veteran

    Thank you Roberto. Are you saying that as far as the conditions for entry stay the same, the pending order is not cancelled?

    Example:

    IF Not OnMarket AND Close > average[200,0](close) THEN

    if I understand what you said: if the Close stays above the SMA200, the pending order stays active?

    Thanks

    Khaled

    #148713 quote
    robertogozzi
    Moderator
    Master

    I don’t know how to explain that!

    As I already said twice, pending orders are ALWAYS cancelled when a bar closes. You need to be  aware of this, because you think a pending order has to be cancelled, while the opposite is true… you have to place again a pending order when you need to. Forget about cancelling them, ProOrder gets rid of them  automatically when a bar closes!

    In your example if the next bar is Not OnMarket and Close > average[200,0](close), a NEW pending order (the previous one has already been cancelled automatically by ProOrder)  is placed.

    Instructions within IF…ENDIF are executed when  the condition (or more than one) is true. If your conditions are true bar after bar, then a pending order (or more than one) will be placed again bar after bar.

    Khaled thanked this post
    #148718 quote
    Khaled
    Participant
    Veteran

    Dear Roberto,

    First, I want to really thank you again for taking the time to answer my questions. I’m a beginner and may ask candide questions and I hope one day I’ll be in position to generously help others, like you do.

    Secondly, I understand your point but as you can see on the screenshot I’ve attached above, it shows two pending orders, created with the same Algo running under 3min TF, the first generated at 3.15pm and the second generated  at 3.18pm, meaning that the first Order has not been cancelled at 3.18pm despite the closure of the previous 3min candle. If the first order was cancelled automatically and has been replaced by a new one at the smae price conditions, then it would show the 3.18pm timing instead of 3.15pm.

    I didn’t want to bother you with so much details, but below is the complete code of the Algo that doesn’t work as expected, may be something wrong with my coding skills or the order of appearance of the instructions. This code is largely inspired from https://www.prorealcode.com/topic/machine-learning-in-proorder/page/26/#post-129120

    Again, my concern is that the pending orders are not cancelled automatically at the end of each candle.

    Your help is much appreciated.

    defparam cumulateorders = false
    defparam preloadbars    = 2000
    N = 0.5
    C1 = 1
    C2 = 2
    heuristicscyclelimit = 2
    once heuristicscycle = 0
    once heuristicsalgo1 = 1
    once heuristicsalgo2 = 0
    if heuristicscycle >= heuristicscyclelimit then
    if heuristicsalgo1 = 1 then
    heuristicsalgo2 = 1
    heuristicsalgo1 = 0
    elsif heuristicsalgo2 = 1 then
    heuristicsalgo1 = 1
    heuristicsalgo2 = 0
    endif
    heuristicscycle = 0
    else
    once valuex = startingvalue
    once valuey = startingvalue2
    endif
    if heuristicsalgo1 = 1 then
    
    //heuristics algorithm 1 start
    if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
    optimise = optimise + 1
    endif
    
    //Settings 1 & 2
    
    startingvalue   = 40 //5, 100, 10 //LONG BOXSIZE
    ResetPeriod     = 3 //1, 0.5 Specify no of months after which to reset optimisation
    increment       = 10 //10 //5, 20, 10
    maxincrement    = 20 //5, 10 limit of no of increments either up or down
    reps            = 3  //1 number of trades to use for analysis //2
    maxvalue        = 5 //10 //70 //50 //50, 20, 300, 150 //maximum allowed value
    minvalue        = increment //15, 5, minimum allowed value
    
    startingvalue2  = 15 //40 //5, 100, 50 //SHORT BOXSIZE
    ResetPeriod2    = 3 //1, 0.5 Specify no of months after which to reset optimisation
    increment2      = 10 //10 //5, 10
    maxincrement2   = 5 //20 //1, 30 limit of no of increments either up/down //4
    reps2           = 2 //3  //1, 2 nos of trades to use for analysis //3
    maxvalue2       = 5 //5 //50, 20, 300, 200 maximum allowed value
    minvalue2       = increment2 //15, 5, minimum allowed value
    
    once monthinit = month
    once yearinit = year
    If (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) Then
    ValueX = StartingValue
    WinCountB = 0
    StratAvgB = 0
    BestA = 0
    BestB = 0
    monthinit = month
    yearinit = year
    EndIf
    
    once valuex  = startingvalue
    once pincpos = 1 //positive increment position
    once nincpos = 1 //negative increment position
    once optimise = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
    once mode1 = 1 //switches between negative and positive increments
    //once wincountb = 3 //initialize best win count
    //graph wincountb coloured (0,0,0) as "wincountb"
    //once stratavgb = 4353 //initialize best avg strategy profit
    //graph stratavgb coloured (0,0,0) as "stratavgb"
     
    if optimise = reps then
    wincounta = 0 //initialize current win count
    stratavga = 0 //initialize current avg strategy profit
    heuristicscycle = heuristicscycle + 1
     
    for i = 1 to reps do
    if positionperf(i) > 0 then
    wincounta = wincounta + 1 //increment current wincount
    endif
    stratavga = stratavga + (((positionperf(i)*countofposition[i]*close)*-1)*-1)
    next
    stratavga = stratavga/reps //calculate current avg strategy profit
    //graph (positionperf(1)*countofposition[1]*100000)*-1 as "posperf1"
    //graph (positionperf(2)*countofposition[2]*100000)*-1 as "posperf2"
    //graph stratavga*-1 as "stratavga"
    //once besta = 300
    //graph besta coloured (0,0,0) as "besta"
    if stratavga >= stratavgb then
    stratavgb = stratavga //update best strategy profit
    besta = valuex
    endif
    //once bestb = 300
    //graph bestb coloured (0,0,0) as "bestb"
    if wincounta >= wincountb then
    wincountb = wincounta //update best win count
    bestb = valuex
    endif
     
    if wincounta > wincountb and stratavga > stratavgb then
    mode1 = 0
    elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 1 then
    valuex = valuex - (increment*nincpos)
    nincpos = nincpos + 1
    mode1 = 2
    elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 1 then
    valuex = valuex + (increment*pincpos)
    pincpos = pincpos + 1
    mode1 = 1
    elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 2 then
    valuex = valuex + (increment*pincpos)
    pincpos = pincpos + 1
    mode1 = 1
    elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 2 then
    valuex = valuex - (increment*nincpos)
    nincpos = nincpos + 1
    mode1 = 2
    endif
     
    if nincpos > maxincrement or pincpos > maxincrement then
    if besta = bestb then
    valuex = besta
    else
    if reps >= 10 then
    weightedscore = 10
    else
    weightedscore = round((reps/100)*100)
    endif
    valuex = round(((besta*(20-weightedscore)) + (bestb*weightedscore))/20) //lower reps = less weight assigned to win%
    endif
    nincpos = 1
    pincpos = 1
    elsif valuex > maxvalue then
    valuex = maxvalue
    elsif valuex < minvalue then
    valuex = minvalue
    endif
    
    optimise = 0
    endif
    // heuristics algorithm 1 end
    
    elsif heuristicsalgo2 = 1 then
    
    // heuristics algorithm 2 start
     
    if (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) then
    optimise2 = optimise2 + 1
    endif
    
    //Settings 2
     
    once monthinit2 = month
    once yearinit2 = year
    If (year = yearinit2 and month = (monthinit2 + ResetPeriod2)) or (year = (yearinit2 + 1) and ((12 - monthinit2) + month = ResetPeriod2)) Then
    ValueY = StartingValue2
    WinCountB2 = 0
    StratAvgB2 = 0
    BestA2 = 0
    BestB2 = 0
    monthinit2 = month
    yearinit2 = year
    EndIf
    once valuey = startingvalue2
    once pincpos2 = 1 //positive increment position
    once nincpos2 = 1 //negative increment position
    once optimise2 = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)
    once mode2 = 1 //switches between negative and positive increments
     
    if optimise2 = reps2 then
    wincounta2 = 0 //initialize current win count
    stratavga2 = 0 //initialize current avg strategy profit
    heuristicscycle = heuristicscycle + 1
     
    for i2 = 1 to reps2 do
    if positionperf(i2) > 0 then
    wincounta2 = wincounta2 + 1 //increment current wincount
    endif
    stratavga2 = stratavga2 + (((positionperf(i2)*countofposition[i2]*close)*-1)*-1)
    next
    stratavga2 = stratavga2/reps2 //calculate current avg strategy profit
    if stratavga2 >= stratavgb2 then
    stratavgb2 = stratavga2 //update best strategy profit
    besta2 = valuey
    endif
    if wincounta2 >= wincountb2 then
    wincountb2 = wincounta2 //update best win count
    bestb2 = valuey
    endif
     
    if wincounta2 > wincountb2 and stratavga2 > stratavgb2 then
    mode2 = 0
    elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 1 then
    valuey = valuey - (increment2*nincpos2)
    nincpos2 = nincpos2 + 1
    mode2 = 2
    elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 1 then
    valuey = valuey + (increment2*pincpos2)
    pincpos2 = pincpos2 + 1
    mode2 = 1
    elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 2 then
    valuey = valuey + (increment2*pincpos2)
    pincpos2 = pincpos2 + 1
    mode2 = 1
    elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 2 then
    valuey = valuey - (increment2*nincpos2)
    nincpos2 = nincpos2 + 1
    mode2 = 2
    endif
     
    if nincpos2 > maxincrement2 or pincpos2 > maxincrement2 then
    if besta2 = bestb2 then
    valuey = besta2
    else
    if reps2 >= 10 then
    weightedscore2 = 10
    else
    weightedscore2 = round((reps2/100)*100)
    endif
    valuey = round(((besta2*(20-weightedscore2)) + (bestb2*weightedscore2))/20) //lower reps = less weight assigned to win%
    endif
    nincpos2 = 1
    pincpos2 = 1
    elsif valuey > maxvalue2 then
    valuey = maxvalue2
    elsif valuey < minvalue2 then
    valuey = minvalue2
    endif
    optimise2 = 0
    endif
    // heuristics algorithm 2 end
    endif
    
    //
    boxsizeL = max ( ValueX , (maxvalue+minvalue)/2) //added (maxvalue+minvalue)/2 to avoid division by zero error
    boxsizeS = max ( ValueY , (maxvalue2+minvalue2)/2) //added (maxvalue+minvalue)/2 to avoid division by zero error
    //
    renkomaxl = round(close / boxsizel) * boxsizel
    renkominl = renkomaxl - boxsizel
    
    renkomaxs = round(close / boxsizes) * boxsizes
    renkomins = renkomaxs - boxsizes
    //
    if high > renkomaxl + boxsizel then
    renkomaxl = renkomaxl + boxsizel
    renkominl = renkominl + boxsizel
    endif
    if low < renkomins - boxsizes then
    renkomaxs = renkomaxs - boxsizes
    renkomins = renkomins - boxsizes
    endif
    
    // IG SPREAD for DAX 1€
    IF time>=001500 AND time>060000 then
    spread = 2
    ELSIF (time>=060000 AND time>070000) AND (time>=153000 AND time>200000)then
    spread = 1
    ELSIF time>=070000 AND time>153000 then
    spread = 0.6
    ELSIF time>=200000 AND time>001500 then
    spread = 2.5
    ENDIF
    
    // Conditions C1 and C2 (can be customized) set to 1 to test the programme
    IF C1=1 THEN
    BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
    ENDIF
    IF C2=2 THEN
    SELLSHORT N CONTRACT at (renkoMinS - boxSizeS - spread) STOP
    ENDIF
    
    // SL and TP and Breakeven
    set stop ptrailing 3
    set target pprofit 500
    
    startBreakeven = 6 
    PointsToKeep = 1
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
    IF SHORTONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
    IF breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    #148720 quote
    robertogozzi
    Moderator
    Master

    There are some basic programming rules that we cannot discuss further here, such as IF…ENDIF and conditions. I already explained you basically what it is all about and you continue adding conditions that are ALWAYS true. What sense does it make writing:

    C1 = 1
     .
     .
     .
    IF C1 = 1 THEN
       //place pending order
    ENDIF

    and complaining again and again about pending orders not being cancelled! C1 is set to 1 initially and NEVER changes, but you still keep it as a condition to place a pending order. Why after the first time, ProOrder should not place it again and again…. ?

    Pending orders ARE automatically cancelled (I will stop replying to you until this is clear, there are tons of examples and topics about this, beyond official documentation and forum searches).

    It’s YOU who have coded you strategy so that it places pending orders continuosly.

    I can help you code your conditions if you can’t, but you need to tell me WHICH are those conditions that you need for a pending order to be PLACED (not cancelled, forget about cancelling them and focus on how to place them).

    Khaled thanked this post
    #148727 quote
    kaq52
    Participant
    Average

    Khaled, let me also try to explain, may be you will get it.

    Your condition to buy is

                     

    IF C1=1 THEN
    BUY N CONTRACT at (renkoMaxL + boxSizeL + spread) STOP
    ENDIF

     

    For first candle, when C1=1, your strategy places one stop order, which stays active for the duration of the candle, and gets cancelled if not fulfilled during that candle’s lifetime.
    Then for second candle again, C1=1 and an exactly similar order gets placed (to you it seems like same order as earlier), and just like above, gets cancelled if not fulfilled during that candle’s lifetime.
    And this loops continues at every candle as long as C1=1.
    Your problem is that C1(or C2) never gets updated…so, as Roberto has advised, unless you write a logic to update your conditions, you will keep on seeing the issue.
    Hope it helps….kaq
    Khaled thanked this post
    #148729 quote
    Khaled
    Participant
    Veteran

    Thank you Kaq. I get the point. Just want to understand then why the time of the first order doesn’t change, order that is supposed to be cancelled and replaced by another order at the same price conditions doesn’t change? stays 3.15 pm, while the most recent order is marked 3.18pm? This is what leads me to think that the first order is not cancelled. I’m not arguing or whatsover, I’m just trying to understand and learn properly.

    20201027_15h18_orders-list-1.png 20201027_15h18_orders-list-1.png
Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.

Cancelling pending orders


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Khaled @khaled Participant
Summary

This topic contains 18 replies,
has 4 voices, and was last updated by GraHal
2 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/26/2020
Status: Active
Attachments: 4 files
Logo Logo
Loading...