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

Unable to modify order with IB

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

    Unable to modify order with IB

    Hello,

    I am encountering an error while modifying live orders with interactive brokers. When I run the strategy in a backtest, or live in a simulated account, I do not generate any errors. But when I run live with my IB account, I get an unable to modify - order not found error with every single attempt to modify an order. I am placing the order with this code:

    EnterLongLimit(0, true, CurrentShares, (MAX1[0] * (1 - ((NATR1[0] * LongEntryFx) / 100))), @"longOrder");

    and I am modifying with this:

    ChangeOrder(longOrder, CurrentShares, (MAX1[0] * (1 - ((NATR1[0] * LongEntryFx) / 100))), 0);

    Any ideas?

    Thanks!



    Here is what shows in the log: (I redacted the account number)
    U(acctnumber), Unable to modify order: Not found orderId='1610958443' account='U(acctnumber)' name='shortOrder' orderState=ChangePending instrument='FLT' orderAction=SellShort orderType='Limit' limitPrice=252.29 stopPrice=0 quantity=19 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=4032 time='2020-10-14 08:47:12' gtd='2099-12-01' statementDate='2020-10-14' affected Order: SellShort 19 Limit @ 252.29
    Order='1610958443/U(acctnumber)' Name='shortOrder' New state='Working' Instrument='FLT' Action='Sell short' Limit price=252.29 Stop price=0 Quantity=19 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Unable to change order' Native error='Unable to modify order: Not found orderId='1610958443' account='U(acctnumber)' name='shortOrder' orderState=ChangePending instrument='FLT' orderAction=SellShort orderType='Limit' limitPrice=252.29 stopPrice=0 quantity=19 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=4032 time='2020-10-14 08:47:12' gtd='2099-12-01' statementDate='2020-10-14''
    Order='1610958443/U(acctnumber)' Name='shortOrder' New state='Accepted' Instrument='FLT' Action='Sell short' Limit price=252.29 Stop price=0 Quantity=19 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''


    #2
    Hello RobotSyndicate,

    Thanks for your post.

    I would first like to rule out TWS/IB Gateway versions. Do you see these symptoms using the latest version of NinjaTrader, and the version of TWS/IB Gateway we have noted in our Connection Guide?

    Connection Guide - https://ninjatrader.com/ConnectionGu...nnection-Guide

    Download - https://ninjatrader.com/PlatformDirect

    Next, I would like to rule out how you are submitting and changing orders. Are you using OnOrderUpdate, OnExecutionUpdate or OnPositionUpdate or any combination of these methods for order handling? Keep in mind, Interactive Brokers and Rithmic do not have an expected ordering for Order Execution and Position events. It can be possible that an Execution event comes before an OrderUpdate event and the proper order ID is not updated in the Order object when you call ChangeOrder. I have attached an example strategy that uses OnOrderUpdate only (as well as the unmanaged approach to manage a Position object based on order fills instead of on Executions. The strategy's Position object is updated on Execution events, so we use our own Position object when using OnOrderUpdate only.) Do you see issues testing this strategy on your end?

    Please let me know if you are seeing issues with this test script as it may provide a good path to build a strategy for use with Interactive Brokers.

    If we are still stuck, could you provide an export of a small test script demonstrating the issue on NinjaTrader 8 Release 22 and on TWS/IB Gateway 978.2c?

    I look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Also, not sure if it is related, but I have open live orders in IB and I launch the NT8 program and connect to my IB account, I get this error message in the log for every single live Order in IB: "Unable to process order status: unable to find order (number), status 'Submitted'.

      Comment


        #4
        Hello RobotSyndicate,

        It can be related if you are not using the current version of NinjaTrader and the version of TWS/IB Gateway we suggest. We need to rule this out first.

        Please confirm if you see symptoms from posts 1 and 3 after confirming you are running these versions.

        Please also confirm on the strategy related issue when testing with an approach like that suggested in post 2.

        We look forward to hearing back.
        JimNinjaTrader Customer Service

        Comment


          #5
          Originally posted by RobotSyndicate View Post
          I am encountering an error while modifying live orders with interactive brokers. When I run the strategy in a backtest, or live in a simulated account, I do not generate any errors. But when I run live with my IB account, I get an unable to modify - order not found error with every single attempt to modify an order. I am placing the order with this code:

          EnterLongLimit(0, true, CurrentShares, (MAX1[0] * (1 - ((NATR1[0] * LongEntryFx) / 100))), @"longOrder");

          and I am modifying with this:

          ChangeOrder(longOrder, CurrentShares, (MAX1[0] * (1 - ((NATR1[0] * LongEntryFx) / 100))), 0);

          Any ideas?
          Where is the longOrder variable assigned a value?

          Comment


            #6
            Hello,

            First of all thank you for the detailed information and sample code! Turns out this was exactly what I needed. I WAS using OnPositionUpdate and OnExecutionUpdate for several things. After switching to the unmanaged approach and using your very helpful code to keep track of positions and submit/update stop and target orders most of my errors went away. I was still having issues with residual orders not being found on reconnect to IB after the infamous daily IB restart, but after incorporating Adopt Account Position and adding unmanaged stop/target order submission to the switch to realtime, it correctly detects account positions and submits appropriate orders! So now I am using RoboTask to shut off strategies and cancel out live orders prior to the reboot, and reboot NT for good measure, and restart everything and presto -- all is working as desired.

            One error I started having after going unmanaged is with the OCO, something like "uniqueID cannot be reused." And so it wont place the desired stop/target orders. My strat places hundreds of orders a day across the S&P, and this only happened with about 15-20 of them. Any thoughts?

            Again thank you, this was extremely helpful.
            Last edited by RobotSyndicate; 10-21-2020, 04:38 PM.

            Comment


              #7
              Does GetAtmStrategyUniqueId() generate a different result every single time its used? Like if I had two different order sets placed during a single OnBarUpdate that both created an OCO for their orderset, would it generate different OCOs? Or is it possible that I randomly actually got the same result a handful of times with 800 orders placed within a few hours?

              I added a random number generator and added a different random number to each OCO in addition to the UniqueId, and so far (fingers crossed... the real action happens tomorrow morning...) no errors.

              Comment


                #8
                Hello RobotSyndicate,

                I'm glad the example was helpful.

                GetAtmStrategyUniqueId is intended to give you a unique ID each time.

                I set up a quick test using this method in a strategy set to calculate on each tick and I have also ran multiple instances of the strategy on the same instrument to increase the likelihood of getting the same value. I have not been able to receive matching values in my tests and I would trust it for generating unique values.

                I cannot be certain that GetAtmStrategyUniqueId created the same ID for you without actually seeing it take place. You can take further steps to ensure it is unique by adding your own random number, but if you do have context where GetAtmStrategyUniqueId did in fact create a previous value, we would like to note it for improvement.

                We look forward to assisting.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jim, after going through my code carefully, I noticed I had a section that would place a market order to exit positions if the price had moved passed in originally intended limit order. After converting things to unmanaged, these single orders were trying to use an OCO that I think would have already been assigned to the existing exit orders. Would this have produced that error? I have removed this code, and will see this morning if it fixes the problem.

                  Comment


                    #10
                    Hello RobotSyndicate,

                    Yes, if an order is submitted with an OCO ID that was already used, this type of error can be encountered.

                    Please be sure that you are always creating a new OCO ID when submitting a new OCO pair and that those ID's are not reused in your strategy.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Jim,

                      It looks like this recent fix has corrected the problem and I am now error free. Thanks for your help!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Barry Milan, Yesterday, 10:35 PM
                      5 responses
                      16 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by DanielSanMartin, Yesterday, 02:37 PM
                      2 responses
                      13 views
                      0 likes
                      Last Post DanielSanMartin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      4 responses
                      13 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by terofs, Today, 04:18 PM
                      0 responses
                      12 views
                      0 likes
                      Last Post terofs
                      by terofs
                       
                      Started by nandhumca, Today, 03:41 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post nandhumca  
                      Working...
                      X