Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CancelOrder...

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

    CancelOrder...

    Hello,

    Running a strategy using the simulated account, live data.
    CancelOrder, if it works, take many seconds.

    "o0" is the order name.

    Example:

    o0 Submitted <--OnOrderUpdate
    Order entry, short: 4028.75 <--mine
    o0 Accepted <--OnOrderUpdate
    o0 Working <--OnOrderUpdate
    0 <--mine
    1 <--mine
    Order cancelled (timeout) o0 <--mine, 5 seconds after order placed, CancelOrder called
    2 <--mine
    Mills: 6377 <--mine
    0 <--mine
    1 <--mine
    o0 CancelPending <--OnOrderUpdate, 7+ seconds after CancelOrder called.
    o0 CancelSubmitted <--OnOrderUpdate
    o0 Cancelled <--OnOrderUpdate​

    Not sure what I am doing wrong.

    Ideas?

    Thanks,

    Mark
    Last edited by Mark--; 03-03-2023, 11:39 AM.

    #2
    Hello Mark,

    Thank you for your post.

    So I may better assist you, please answer the following questions:
    • What version of NinjaTrader are you using? This may be found at Control Center > Help > About
    • Is this happening on a strategy that you have developed? If possible, please provide a snippet of the script that shows your use of the CancelOrder() method
    • How are you measuring the time it takes for CancelOrder to take effect? Are you referring to the Log tab of the Control Center and/or the NinjaScript Output window?
    If you have not already, I suggest adding Print statements to your script as well as enabling Trace Orders in State.SetDefaults. This can help to get accurate outputs, including timestamps, for when certain parts of your logic are hit as well as when order actions occur. For more information, please see the links below:I look forward to assisting you further.

    Comment


      #3
      Hello,

      Thanks for the response.

      >What version of NinjaTrader are you using?

      8.0.26.1 64-bit

      >Is this happening on a strategy that you have developed?

      Yes.

      if (entryOrder != null)
      {
      Print("Order cancelled (timeout) " + entryOrder.Name);
      CancelOrder(entryOrder);
      }
      else
      Print("Order cancelled (timeout) failed *** entryOrder was null."); //should never happen​

      >How are you measuring the time it takes for CancelOrder to take effect?

      OnOrderUpdate tells me the submitted order was cancelled.

      >NinjaScript Output window?

      Yes. All those lines from above are via print commands. The <---says what generated the line.

      Mark

      Last edited by Mark--; 03-03-2023, 12:56 PM.

      Comment


        #4
        Hello Mark,

        Thank you for your reply.

        First, I see that you're using an older version of NinjaTrader 8. To update NinjaTrader, please follow the steps below:
        • First, copy your license key from NinjaTrader under Help> License Key then exit NinjaTrader
        • Click on the link: https://ninjatrader.com/PlatformDirect
        • Enter your license key and press Submit
        • Select 'Download'
        • Critical: Before running the installer, ensure NinjaTrader is closed.
        Next, it is important to consider that OnOrderUpdate() will process the order states in a sequence, and if you are looking for the most up-to-date order state you should use the core order object's properties such as entryOrder.OrderState. This is explained on the following page under the header "Multi-threaded consideration for Order, Execution and Position objects"


        With this in mind, you could add prints that include a timestamp and check for entryOrder.OrderState and compare those to the timestamps of OnOrderUpdate() events.

        Additionally, we have a reference sample demonstrating the use of the CancelOrder() method here that might be helpful to review:


        Please let me know if I may be of further assistance.

        Comment


          #5
          Hello,

          Thanks for the response.

          I will update NJ in a few moments.

          Here is the scene.
          I know the order state is not filled, even the DOM has the order as not filled.
          I know 5 seconds has elapsed since the order was submitted.
          I know I call cancel order.

          And the problem:

          3,4,5... seconds after I call CancelOrder, the order can get filled, my code sees it get filled, the dome sees it get filled.

          I read (before) about the new multi-threading in NJ (most of the programing I do is threaded) and I have taken that into account.

          3,4,5... seconds is not a threading/race/out of order issue. Or if it is...

          Hopefully updating to the current version will solve the issue. I will playback today's data and see. If no joy I will create a time stamped log example and post it.

          Cheers,

          Mark

          Later same day. Updated to current version. The market replay does not have the same resolution as live so I will test again Monday.

          .
          Last edited by Mark--; 03-03-2023, 04:57 PM.

          Comment


            #6
            Hello,

            It appears updating to the current release has not solved the issue.

            I added the time stamp as the first line for OnOrderUpdate. "o0" is the order name.

            Here is an example of the cancel order not being processed for 8 seconds.

            o0 3/6/2023 9:23:12 AM
            o0 Submitted
            o0 3/6/2023 9:23:12 AM
            o0 Accepted
            o0 3/6/2023 9:23:12 AM
            o0 Working
            Order cancelled (timeout) o0 < 5 second timer timed out. was 9:23:17 AM

            [no indication cancel order processed]

            o0 3/6/2023 9:23:24 AM
            o0 Filled​

            I also log all comments and error code values passed in with the callback.

            Mark

            Later, and here is one that worked as expected.

            o0 3/6/2023 10:46:52 AM
            o0 Submitted
            10:46:53 AM Cancel timer start < I added sense last log
            o0 3/6/2023 10:46:53 AM
            o0 Accepted
            o0 3/6/2023 10:46:53 AM
            o0 Working
            10:46:58 AM Cancel timer event < I added sense last log.
            Order cancelled (timeout) o0
            o0 3/6/2023 10:47:01 AM <3 seconds...OK, could think pending would be < 1 second
            o0 CancelPending
            o0 3/6/2023 10:47:01 AM
            o0 CancelSubmitted
            o0 3/6/2023 10:47:01 AM
            o0 Cancelled

            And one that did not work in a "reasonable" time.

            o2 3/6/2023 10:53:10 AM
            o2 Submitted
            10:53:11 AM Cancel timer start
            o2 3/6/2023 10:53:10 AM
            o2 Accepted
            o2 3/6/2023 10:53:10 AM
            o2 Working
            10:53:16 AM Cancel timer event
            Order cancelled (timeout) o2
            o2 3/6/2023 10:54:00 AM < 44 seconds...not good
            o2 CancelPending
            o2 3/6/2023 10:54:00 AM
            o2 CancelSubmitted
            o2 3/6/2023 10:54:00 AM
            o2 Cancelled​

            Testing will continue. Seems to me something is odd.

            Last edited by Mark--; 03-06-2023, 10:58 AM.

            Comment


              #7
              Hello Mark,

              Thank you for your patience.

              I appreciate the updates as you continue to test. If you'd like, please provide a reduced sample of your script that can still demonstrate the unexpected behavior and export it via Control Center > Tools > Export > NinjaScript AddOn. I would be glad to test it on my end and see if I get the same/similar results.

              Are you able to think of or identity any differences between the prints that worked as expected vs. the ones that seemed off to you? That might help give us a clue as to what might be causing the discrepancies.

              Please let me know if I may be of further assistance.

              Comment


                #8
                Hello,

                Thanks for the response.

                I thought that might be an incoming request. You by chance, would not have a sample script that shows the cancelOrder call works every time?

                >Are you able to think of or identity any differences between the prints that worked as expected vs. the ones that seemed off to you?

                No. Only the one that worked OK, if 3 seconds to submit is OK, is the only one that worked. While typing this another cancel order took 31 seconds to submit.

                Mark


                Comment


                  #9
                  Hello,

                  Attached is a simple strategy, it enters a long limit order on the five second mark of each minute.
                  The cancel order is placed five seconds later. It takes 30 to 50 seconds before the cancel order is "CancelPending".
                  Though now it appears to not cancel at all and cancels on end of bar/start new bar. Time in force is GTC.

                  Not sure what I am doing wrong. Perhaps forest for all the trees is active.

                  Mark
                  Attached Files

                  Comment


                    #10
                    Hello Mark,

                    Thank you for your reply.

                    We do have a reference sample that demonstrates the use of CancelOrder() that I previously mentioned in post #4, and you could modify the script to add print statements with timestamps that you may compare to the results of your own strategy:


                    Otherwise, I do see you are using a timer and you are processing your code inside of the timer event processor; as a best practice, you should use TriggerCustomEvent() instead so that internal indexes and pointers are properly set before processing the code triggered by the custom event. For more information about TriggerCustomEvent():


                    I look forward to hearing the results after adding TriggerCustomEvent() and I would be glad to test out your script on my end tomorrow when there is live market data.

                    Thank you for your time and patience.

                    Comment


                      #11
                      Mark-- I have experienced similar delays at times. My thoughts are along these lines:
                      • Orders are recorded/managed in the NT8 db file
                      • As the number of Orders grows, so does the db file
                      • If you have been testing over a significant period of time, with possibly lots of Orders, the db file will have grown significantly
                      • The efficiency of the db as it grows is of potential concern, I think, although I have only my own anecdotal experience to go on
                      • To see if this is a contributing factor, for you, you could try exiting NT, renaming your current db so you do not lose it, restarting NT and seeing how your tests go
                      • Obviously, you will not be able to access all the data that is in the renamed db, but this is just a test to see if the db is part of the issue, and you can rename back later, regardless of the outcome of the test
                      • This is a relatively easy, quick and non-destructive way to test if the db file is a contributing factor
                      It would be useful to have some objective data on this type of effect on performance. It would also be good to have a supported way to optimise the db file from time-to-time to try to minimise any such impact.

                      Hope this provides some insights for you.

                      Thanks.
                      Last edited by jeronymite; 03-06-2023, 06:19 PM.
                      Multi-Dimensional Managed Trading
                      jeronymite
                      NinjaTrader Ecosystem Vendor - Mizpah Software

                      Comment


                        #12
                        Originally posted by NinjaTrader_Emily View Post
                        Otherwise, I do see you are using a timer and you are processing your code inside of the timer event processor; as a best practice, you should use TriggerCustomEvent()...
                        Thanks for the response.

                        OK, I changed the example, attached, to use TriggerCustomEvent(), and was able to test a few times. No change.

                        Cheers,

                        Mark


                        Attached Files

                        Comment


                          #13
                          Originally posted by jeronymite View Post
                          I have experienced similar delays at times.
                          Thanks for the suggestion. I did not need any old data so I did a "reset database" in the tools menu. Did that accomplish the same goal as suggested? If so, no change. I made the change Emily suggested and reset the database, was able to get a few test runs in with the same result.

                          Ciao,

                          Mark

                          Comment


                            #14
                            Not sure what the Reset does "under the covers". Did it change the size of the db file? In any case, it can't hurt to do a rename (or delete if OK) and see if a "brand new" db file makes any difference. Just a thought.

                            Thanks.
                            Multi-Dimensional Managed Trading
                            jeronymite
                            NinjaTrader Ecosystem Vendor - Mizpah Software

                            Comment


                              #15
                              Originally posted by jeronymite View Post
                              Not sure what the Reset does "under the covers". Did it change the size of the db file?
                              Ditto and I do not know. The "NinjaTrader.sqlite" file is 3.48 MB (3,657,728 bytes).
                              I renamed the file, started NJ and a new file was created; it is 3.43 MB (3,600,384 bytes)


                              Mark

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              71 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              143 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              76 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              47 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              51 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X