Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot cancel Limit order in Submitted status once strategy reaches realtime

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Cannot cancel Limit order in Submitted status once strategy reaches realtime

    Scenario:
    • Using DataSeries in which the End Date is not today. The Days to Load is set to 6.
    • I am using order objects to manage the trade
    • I am implementing a pullback strategy (for longs and a retracement for shorts) using a Limit order and I also have a property which counts the number of bars which have passed since the the pullback order was created. If it reaches a set number of bars, it will cancel the Limit order.
    • Connecting using NT Continuum. The broker is Phillip Capital. I am using the SIM account, not my real account
    • When the strategy reaches the last bar, it reaches the Realtime state. Once it reaches the Realtime state, any non-null orders gets converted to a Realtime order
    Code:
    else if (State == State.Realtime)
     {
    
    
    
                    if (entryOrder != null)
    
                    {
    
                        entryOrder = GetRealtimeOrder(entryOrder);
    
                        Print("I AM HERE");
    
                    }
    }


    Issue:
    • The pullback logic and the number of bars before the Limit order gets cancelled is working correctly when processing the historical bars; no issue there!
    • Before the strategy transitions to Realtime a limit order gets created and it is NOT canceled due to the number of bars.
    • Using the OnOrderUpdate() method and displaying the order details using order.ToString(), I can see that last order went through the following order states: Submitted, Accepted, and Working. I create my entryOrder object if the object is null and once the order.State == OrserState.Working.
      • Since it reaches the working state, I am creating the entryOrder. This is happening in historical bars
    • The Working order gets converted into the Realtime order since the strategy reaches the Realtime state. When this happens, I can see an Order record in the Orders tab of the Control Center. Interesting enough, the State of this order is Submitted. But I know it reached the working state. I am confuse about this!
    • Before I disable the strategy and because no new bars are getting created (the End Date of the Data Series is not Today), the strategy never cancels the entryOrder which should be in working status as the Limit order never gets filled.
    • I decide to disable to cancel the strategy by unchecking the Enable check box
      • I have code in the Terminate state
    Code:
    else if (State == State.Realtime)
     {
            if (entryOrder != null)
    
                {
    
                    Print("I am going to cancel entryOrder ");
    
                    CancelOrder(entryOrder);
    
                }
    }
    • When I try to cancel the entryOrder, the order that resides in the Orders tab in the control center changes from Submitted to Cancel Pending. Once it reaches the Cancel Pending state, it never gets resolve. If I run the strategy again, the CancelPending order creates to error:
      • Unable to cancel out live orders. Strategy yyyy was not Started.
      • Strategy yyyy has been disabled because it attempted to modify a historical order that has transitioned to live order. Please see the help guide.....
    • To eliminate the Cancel Pending order, I need to reset the SIM account. Since I am not canceling the submitted order correctly, this issue will happen again.
    Questions:
    • How to cancel an order (because I disable the strategy) that was never filled in historical and now it has been converted to Realtime order?

    #2
    Hello GARZONJ,

    Thank you for your post.

    If I'm understanding correctly, you are running this on a chart with historical data that does not end today, is that correct? So no live data is actually coming in to this chart.

    If that's true, while the strategy will reach State.Realtime, it will not process anything once it reaches that state because there is no real time data.

    I would suggest trying to cancel the order on the last historical bar instead of waiting until State.Realtime in this case.

    To find the last historical bar:

    Code:
    if (State == State.Historical && CurrentBar == Count — 2)
    {
            if (entryOrder != null)
    
                {
    
                    Print("I am going to cancel entryOrder ");
    
                    CancelOrder(entryOrder);
    
                }
    }
    Below is a link to the help guide on the 'State' NinjaScript property.



    Please let me know if this does not resolve your inquiry.


    Comment


      #3
      Kate,

      Your assumption is correct. I am running the strategy on a chart. I set the chart’s data series where I do not include today’s date. Since I am not including today’s date, there should not be any live data.

      As you noted, although the strategy never gets live data, the strategy does reach the RealTime state.

      As you can see from the code that I included in my original post, I have some code to transition orders from historical to live. I am also using a NT method that accomplishes this. Now that I am working through this issue, when/why would someone convert a historical order object to a RealTime order? I can see the use of this when you transition from historical to RealTime in a simulation account. But if I transition from historical to RealTime on a funded account, should I first cancel all of my historical orders so that the funded account is not out of synch?

      As far as the code that you recommended, I will implement it once I have access to the computer. I will get back to you once I try it. It sounds like processing orders should be done within the OnBarUpdate method with the exception of transitioning orders from historical to RealTime. If this a correct assumption?

      Comment


        #4
        The reason why I was getting an Order stuck in Submitted status and then in CancelPending status was because I was trying to cancel the not-filled Limit order within the OnStateChange() method and instead of the OnBarUpdate() method.

        If one is trying to cancel an order within one of the states of the OnStateChange() method, NT will not be able to properly cancel the order. Once I move the logic into the OnBarUpdate() method, the order was able to cancel successfully.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        43 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X