Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Cancellation rejected by the Order Management System - Order is complete...

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

    Cancellation rejected by the Order Management System - Order is complete...

    Hello Ninjatrader, I'm getting this same error every single time my ExitLong(); or ExitShort(); conditions are met. The code is insanely simple, yet somehow the communication between NT8 and my broker gets error coded when the exit conditions are met. I've done everything from restarting ninjatrader to resetting DB, to disconnecting etc...
    The exit logic is quite simple Please help.
    // The buyBar is simply the bar that has been entered on, When the market is under it by 4 ticks close the trade.
    if (Close[1] < Open[CurrentBar - buyBar]- 4 * TickSize)
    {
    if (counter < 6)
    {
    orderQuantityReverse +=1;
    }
    ExitLong("Stop Loss", "");
    if (eminiTradeSize != 0)
    {
    ExitLong(1, Positions[1].Quantity, "Stop Loss", "");
    }
    takeTrade = true;
    }

    #2
    Hello Spellman,

    Thank you for your post.

    I would first suggest using Signal Names so you can more easily identify which orders are affecting how the positions are entered and exited.

    Your logic still may be calling Enter/Exit on the same bar. You can test with the Exit behavior removed to confirm if the Exit behavior is causing the issue.

    I would then suggest adding a bool to the strategy so you can better control the logic.

    For example, once the bool is created in Inputs and Variables, create a new condition set in Conditions and Actions that does not have any conditions. This should be Condition Set 1. In this Condition Set, create an action that sets the bool to true. This will be done on each bar that the strategy processes, before the rest of the logic is processed.

    In your entry/reversal condition sets, set the bool to false.

    In your Exit condition sets, check if the bool is true before allowing the Exit to take place.

    This will ensure that these methods are not called on the same bar.

    We look forward to assisting.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon, the problem I have is that this is only happening on a live account.(Don't worry all positions are flattened). The sim101 and playback101 account never have these issues. They enter and exit at the right time. This seems to only be happening on my rhithmic brokerage account.

      Comment


        #4
        Hello Spellman,

        Thank you for your note.

        The Sim101 and Playback account simulates a standard broker and may not have this error message. Not all brokers have the same errors or stipulations surrounding rejection errors or account requirements. If this error only occurs on your Live account, then it is likely the error is coming from the broker.

        Let us know if you have further questions.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Ok, I will check with my brokerage and report back. But in the meantime I see that in the log, it says Received order in an unknown state 'Cancellation Failed'...
          Any thoughts on what that is and why that is happening?

          Comment


            #6
            Hello Spellman,

            Thank you for your note.

            Please send your log and trace files to scriptingsupport[AT]ninjatrader.com so we may further investigate this. In the subject of your email please include ATTN: Brandon H and in the body of the email please provide a link to this forum post.

            Follow the steps below to manually attach your log and trace files to your response.
            • Open your NinjaTrader folder under, "Documents" (sometimes called, "My Documents")
            • Right click on the 'log' and 'trace' folders and select Send To> Compressed (zipped) Folder.
            • Send the 2 compressed folders as attachments to this email.
            • Once complete, you can delete these compressed folders.
            I look forward to further assisting.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Problem Found(?):
              I'm posting this here in case this helps someone in the future.
              So I noticed my strategy kept entering in the opposite direction after the exit conditions were met. My strategy uses one tick renko bars. So what I believe would happen is that the market (NQ) would move so fast that the exit long/Short condition would send the order to my broker(Rhythmic) then ninja trader will cancel them because the managed approach will cancel exit orders as soon as the bar closes(1tick) then resubmit them, because more often than not the conditions are still true. My broker will fill all orders submitted. So the original exit orders, not cancel in time, then submit the new exit orders that have been resubmitted. I believe this is what's happening and why the log says "failed to cancel"

              Comment


                #8
                Hello Spellman,

                After further investigation, we see that your script is calling multiple Exit orders or Entry orders within the same iterations. An example of this is seen between lines 226-229. ExitLong and ExitShort are called in the same iteration for the secondary data series as well as the primary data series.

                Please modify your script so that your strategy submits entry and exit orders explicitly to a single BarsInProgress. Ensure that your entry and exit methods are not called on the same iteration in your script. We also see that your script places reversals which may cause the strategy to place an order in the opposite direction. Ensure that there are no reversal orders becoming true at the same time other orders are called. Since you are not using Set methods in your script, please remove the SetStopLoss and SetProfitTarget methods from your script.

                Also, you can add a counter to your script in OnBarUpdate that increments each time OnBarUpdate is called. This would act like a tick counter and prints could be used to allow you to see if multiple orders are being placed on the same tick.

                After making those changes to your script, do you still see the same behavior occur? If so, please send us a copy of your strategy so we may test it on our end.

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hey Spellman,

                  Did the suggestions in the last post from BrandonH fix the problem?

                  I have the exact same issue going on with Rithmic.

                  Thank you,

                  Nick

                  Comment


                    #10
                    BrandonH,

                    Can you provide some code or direction as to how to add the tick counter in OnBarUpdate that you suggested in your last post?

                    Thank you,

                    Nick

                    Comment


                      #11
                      Hello njmeyer713,

                      Thanks for your note.

                      Creating a counter (incrementing a variable) in a script would be considered C# education. In the Support department we are not able to provide C# education since we are a small team.

                      That said, I am able to provide you with some direction to get you started.

                      To make a counter that increments with each tick, you could create a class level int variable named something like 'myCounter'. In the OnBarUpdate() method you would increment the variable by calling 'mycounter++;'. Note that you will need to run the script with a Calculate mode of OnEachTick for the counter to increment with each tick.

                      See the help guide documentation below for more information.
                      Calculate: https://ninjatrader.com/support/help.../calculate.htm

                      See this publicly available documentation for more information about incrementing a variable: https://www.tutorialspoint.com/Incre...ators-in-Chash

                      Let me know if I may assist further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Brandon,

                        I'm having this issue, using managed orders, the problem happens when:

                        1) A long position with a stop is active.
                        2) An exit signal triggers, and during that exit, the stop is hit
                        3) NT8 sends a sell order to exit long position, but also gets into a short position due to the stop order being triggered.
                        4) Error is shown that says could not cancel stop order (because stop order is already filled)

                        This should be handled by NT automatically, right? Should not remain with a short position?

                        Regards
                        UIises
                        ulisesguerrero
                        NinjaTrader Ecosystem Vendor - Thrifty Coders

                        Comment


                          #13
                          Hello ulisesguerrero,

                          Thanks for your note.

                          You would need to cancel the stop order before exiting the long position.

                          To do so, you would track the stop order in an Order object. Then, you would call CancelOrder() to cancel that order before you submit the exit order method to exit the position.

                          See this help guide page for more information about CancelOrder() and sample code: https://ninjatrader.com/support/help...ancelorder.htm

                          Let me know if I may assist further.
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Brandon,

                            Thanks for the quick reply. So, when I send the CancelOrder, can I send the exit signal immediately after that, or do I need to start a timer or monitor that the stop loss is cancelled onexecutionupdate?

                            As far as I could understand, using managed orders would save me from having to do so much checking no?

                            Also, how can I check after that I got the "Cancellation rejected" message from the code?

                            Thanks
                            Ulises
                            Last edited by ulisesguerrero; 07-28-2022, 09:41 AM.
                            ulisesguerrero
                            NinjaTrader Ecosystem Vendor - Thrifty Coders

                            Comment


                              #15
                              Hello ulisesguerrero,

                              Thanks for your note.

                              You should ensure that the stop loss order was canceled in OnExecutionUpdate() as you have stated.

                              See this help guide page for more information about OnExecutionUpdate() and sample code: https://ninjatrader.com/support/help...tionupdate.htm

                              And, see this reference sample demonstrating using OnExecutionUpdate(): https://ninjatrader.com/support/help...and_onexec.htm

                              Note that the Managed Approach for order entry can be further broken down into a Basic/Common Managed approach and a more Advanced Managed approach. This type of strategy would fall under the Advanced Managed Approach which would require you to track order objects using the Order class.

                              See this help guide page for more information: https://ninjatrader.com/support/help...#TheOrderClass

                              You could use RealtimeErrorHandling to determine the behavior of a strategy when the strategy places an order that is returned "Rejected". The default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions.

                              RealtimeErrorHandling could be set to IgnoreAllErrors to ignore any order errors received or StopCancelCloseIgnoreRejects to perform the default strategy behavior on all errors except order rejections. Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking if the OrderState is Rejected within OnOrderUpdate() followed by defining your own order rejection handling behavior for the rejected order.

                              Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping a rejected order in OnOrderUpdate().

                              RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm

                              Let us know if we may assist further.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ttrader23, Today, 09:04 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post ttrader23  
                              Started by geotrades1, Today, 07:54 AM
                              8 responses
                              18 views
                              0 likes
                              Last Post geotrades1  
                              Started by SnailHorn, Today, 08:38 AM
                              1 response
                              1 view
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
                              26 responses
                              145 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by leonardomocci, Today, 08:28 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X