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

Unmanaged Approach Error: "OCO orders cannot be modified....

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

    Unmanaged Approach Error: "OCO orders cannot be modified....

    Hi,

    I am using NT8 and using the unmanaged approach. Strategy seems to be working fine when submitting Entry and the initial OCO orders however I want to modify the order so that it updates my OCO order every time price goes up.

    For example: if(Low[0] > Low [1] && Low[1] > StopLoss)
    ChangeOrder(...);
    ChangeOrder(...);

    I tried using the Change Order function in the execution section of the script however I got an error message and strategy was disabled
    Error: "OCO orders cannot be modified. You must cancel the OCO pair and resubmit the orders"
    Strategy submitted an order that generated the following error 'Unable to change order'. Strategy has sent cancel requests, attempted to close the position and terminated itself.

    ***Please note that strategy does seem to be working fine when I am back testing it or with Historical Data.

    I will really appreciate if someone can help me out or point me in the right direction.

    Thanks,


    #2
    Hello bjunaid,

    Thanks for your post.

    This sounds like a broker/exchange given native error. NinjaTrader submitted the change request for the order, but the broker/exchange has not accepted the change request and returned an error. When the strategy sees an order error, it will enter its RealtimeErrorHandling behavior and by default will Stop, Cancel Orders and Close positions.

    I would suggest doing what the error suggests: to cancel the orders and resubmit them instead of trying to change them.

    Orders can be cancelled with CancelOrder, and you can look for order cancellation to be completed by checking if the updating order in OnOrderUpdate reaches an OrderState of OrderState.Cancelled.

    CancelOrder - https://ninjatrader.com/support/help...ancelorder.htm

    Cancelling Orders - https://ninjatrader.com/support/help...thod_to_ca.htm

    OnOrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi,
      I have a question regarding my unmanaged approached strategy.

      1. My first order gets submitted then stop loss and profit target orders are submitted fine however when my second order is submitted then my stoploss and profit target orders are not triggered. Also stopOrder and Target Orders are submitted after I manually close the second position that was submitted without the stop and profit target orders. Any help or guidance will be greatly appreciated, please let me know what I can do to fix this error.

      Thanks,

      Comment


        #4
        Hello bjunaid,

        Where are the profit target and stop loss submitted?

        I suggest checking here and then to use prints to confirm if the logic is in fact reaching these order methods. If the order methods are reached, then I would suggest testing again with TraceOrders enabled.

        If the order methods are not reached, I suggest using prints to check the conditions controlling those order methods to see why the logic is not allowing the code to be reached.

        Note that if you are using Order objects, you should be mindful to assign them in OnOrderUpdate, set them to null if the order is cancelled or rejected in OnOrderUpdate, and to set them to null after the order is filled in OnExecutionUpdate.

        Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

        TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,

          Profit targets and stop loss are submitted in the OnExecution method and OnOrderupdate method which is after execution method. I made a few adjustments to the code and I am noticing that my second order is not being submitted immediately after my first position ends which is a good sign however, my second order still executes without the target and stoploss orders. Once I close my second position my target and stop orders are triggered automatically.

          I added the print statement in each method. I see that the second order is updated in OnOrderUpdate but I can't figure whatis suppose to happen next.

          I have been trying to debug this for the last 18 hours. Any help will be greatly appreciated. I searched forum and I am not sure if other users came across similar issues.

          Thanks,

          __________________1st Order_____________
          OnOrderUpdate: shortentryOrder= Order
          OnOrderUpdate: shortentryOrder= Order
          OnOrderUpdate: shortentryOrder= Order
          OnOrderUpdate: shortentryOrder= Order
          In Execution Order State = Filled && Sumfilled = OrdeQuanity and Stop and Target Orders Triggered
          In Execution Entry Order Cancelation initiated
          OnOrderUpdate: Long Profit Target = Order
          OnOrderUpdate: short stopOrder = Order
          In Execution Method StopLoss Reached stopOrder status is null
          In Execution Method Stop Loss Reached Cancelling targetOrder
          In Execution Method StopLoss Reached TragetOrder status is null
          OnOrderUpdate: targetOrder or stop Order cancelled target and stop order = null

          _________________2nd Order_______________
          OnOrderUpdate: shortentryOrder = Order
          OnOrderUpdate: shortentryOrder= Order
          OnOrderUpdate: shortentryOrder= Order
          OnOrderUpdate: shortentryOrder= Order

          I also changed the trace order to true and reviewed the log file step by step but I can't seem to figure it out:
          Last edited by bjunaid; 09-29-2020, 05:05 AM.

          Comment


            #6
            Hello bjunaid,

            Debugging steps should be focused so we are not distracted with too much debug output.

            We need to focus on:

            Are the order submission methods getting reached when I expect them to be reached.

            First step:

            Add a print next to the order submission method and test to reproduce the issue. Is the logic preventing your order method from being reached?

            Second step:

            If the order method is reached, and confirmed with a print, what is the the TraceOrders message that is seen when the order method is called? (You can disable other prints for this test, but some prints that let us know the specific order method is being called can be helpful.

            If the order method is not reached, (you don't see your print that is next to your order method,) there is an issue with your logic, and you need to trace the logic back to see which condition is not evaluating as true. This involves printing out the values for the condition before the condition is reached. (Just above it.)

            For example, if I wanted to debug my OnExecutionUpdate method that fires my target/stop, I would want to make prints that look like the following so I can see which conditions are not evaluating as true:

            Code:
            /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
            which ensures your strategy has received the execution which is used for internal signal tracking. */
            
            Print("(entryOrder != null && entryOrder == execution.Order): " + (entryOrder != null && entryOrder == execution.Order));
            if (entryOrder != null && entryOrder == execution.Order)
            {
                Print("(execution.Order.OrderState == OrderState.Filled): " + (execution.Order.OrderState == OrderState.Filled));
                Print("(execution.Order.OrderState == OrderState.PartFilled): " + (execution.Order.OrderState == OrderState.PartFilled));
                Print("((execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)): " + ((execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)));
            
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
                    // We sum the quantities of each execution making up the entry order
                    sumFilled += execution.Quantity;
            
                    Print("(execution.Order.OrderState == OrderState.PartFilled): " + (execution.Order.OrderState == OrderState.PartFilled));
                    Print("(execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled): " + (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled));
            
                    // Submit exit orders for partial fills
                    if (execution.Order.OrderState == OrderState.PartFilled)
                    {
                        Print("Submissions sent 1");
                        stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                        targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                    }
                    // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
                    else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
                    {
                        Print("Submissions sent 2");
                        // Stop-Loss order for OrderState.Filled
                        stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                        targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                    }
            
                    // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
                    if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
                    {
                        entryOrder = null;
                        sumFilled = 0;
                    }
                }
            }
            Note: OnOrderUpdate comes before OnExecutionUpdate. Rithmic and Interactive Brokers are the exception where these events do not come in an intended order. Those connections should not use logic that relies on the order of these events.

            I suggest comparing your logic against our SampleOnOrderUpdate example which works and submits target/stop in OnExecutuionUpdate.

            I then suggest narrowing your focus to:

            1. Verify if the order submission method is reached.
            2. If it is reached, but is ignored, what is the associated TraceOrder message from the submission?
            3. If it is not reached, you will need to trace your logic back in an organized fashion to see why your logic is not allowing the method to be reached.

            Looking at the snippet from your logic, it does not appear that your logic is allowing further prints to be reached. Your issue looks to be logical.

            We look forward to assisting.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim,

              I included the Print statement in the OnExecutionUpdate and OnOrderUpdate and I reviewed the Output file. I made some changes to the script and my orders were getting filled and stop and target orders are triggering however it is very sporadic behavior. It worked fine for the first two orders and OCO stop and target orders were executed, however for some reason it is is giving me an error which says that OnExecutionUpdate Object reference is not set to an instance of an object.

              I have attached my code for onexecution udpate and onbardate with output file after testing and for the last 10 hours.

              Please see attached.

              Thanks,




              Attached Files

              Comment


                #8
                Hello bjunaid,

                "Object reference not set to an instance of an object" means you referenced an object that is null.

                Considering you added debugging prints and have seen the null reference was hit after your "(execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled): False" print, I would suggest looking after that to identify the line of code that has the null reference.

                When you find the line of code that has the null reference, you would then check each object on the line to see which is null.

                You would then either use a null check to skip over the line of code that makes the null reference (if that line of code is not necessary to be executed) or you would correct your code so the object is not null when your code reaches this point.

                Checking for null references - https://ninjatrader.com/support/help...references.htm

                If this is an Order object that is null, I suggest reviewing our SampleOnOrderUpdate example to see how Order objects are used there.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jim,

                  I don't have a lot of time these days as I have a day job and I have spent lots of nights just trying debug this code. Will you be able to fix my script if I email it to you and I can pay you for it. I believe it is in the final stage in terms of testing ensuring that it works smoothly.

                  Please let me know.

                  Thanks,
                  Junaid

                  Comment


                    #10
                    Hello Junaid,

                    I'm flattered, but I would not be able to accept this offer. Being hired for coding assistance would not be something that I can do as a support technician working directly for NinjaTrader, and the task would be better suited for a NinjaScript Consultant in our EcoSystem.

                    I will have a colleague chime in with more information on how you can find NinjaScript Consultants in our EcoSystem who would be willing to work with you on the strategy.

                    We look forward to assisting.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Junaid,

                      This is Chris following up on behalf of Jim.

                      You can search our list of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

                      The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

                      Let me know if I may be of further assistance.

                      Comment


                        #12
                        Hi Chris,
                        Thank you for your suggestion.

                        Hi Jim,
                        Thank you so much for your help, I was able to spend some time today to debug the code and finally got the code to work and it seems to be working fine.

                        Thanks again,
                        Junaid

                        Comment


                          #13
                          Hi bjunaid,

                          I don't have the link but if you have not seen them might take a look at Sample Zip files posted by Chelsea named something like "Profit Chase and Trailing Stop" (for managed and unmanaged orders.

                          Look at the "Simulated OCO" in the sample for MANAGED orders. I converted that code for use with unmanaged orders to easy the pains of working with OCOs on fast moving orders. All but very short term orders still get a server side OCO.

                          I recommend adding that simulated OCO to your UNMANAGED toolkit to gain speed an flexibility when the use properly fits your current risk model.

                          Hedgeplay

                          Comment


                            #14
                            Hi Hedgeplay,

                            Thank you for this information. I will definitely look into this.
                            What do you mean by server side OCO. Did you mean server side error?
                            "Look at the "Simulated OCO" in the sample for MANAGED orders. I converted that code for use with unmanaged orders to easy the pains of working with OCOs on fast moving orders. All but very short term orders still get a server side OCO."

                            Comment


                              #15
                              "All but very short term orders still get a server side OCO."

                              If your broker supports server side OCO (like NinjaTrader) the OCO remains in place if your machine loses it's connection, or you shut down the PC.

                              Simulated OCOs only work correctly as long as your PC has a good healthy connection to your broker's data feed so I would not recommend for longer term orders.

                              HedgePlay

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X