Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble with ChangeOrder()

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

    Trouble with ChangeOrder()

    I'm developing a strategy where I need to perform both bracketed orders and trade reversals. The bracketed orders are set up with OCO. I'm developing for FOREX data and I have contacted both Interactive Brokers and forex.com to ask them how they prefer to perform a reversal. Both say to simply set a stop loss and double the number of lots relative to the original trade in order to perform the reversal. Since you can't do bracketed orders or reversals with Managed Orders, I'm developing with Unmanaged Orders.

    Generally this works but occasionally orders seem to disappear. I put Print statements immediately before and after the SubmitOrderUnmanaged() and ChangeOrder() statements. When everything is working I see my first print statement and then system generated printouts within OnOrderUpdate() followed by my second print statement. I will generally see multiple OnOrderUpdate() printouts thereafter. But when things go wrong, I see that both of my Print statements execute but the orders never reach OnOrderUpdate(). As my strategy tries to update the orders, the erroneous condition continues. But sometimes thereafter it starts working again.

    When an order is completed, I set the order objects to null rather than calling CancelOrder(). Is there any time I should be using CancelOrder() rather than setting the order objects to null? The example code I have only set them to null, so that's what I'm doing.

    I really don't know how to debug this situation any better than I have. I have Print statements everywhere and therefore see when order statements seem to be ignored. I'm totally baffled how orders get ignored without any kind of error statements being generated. Any help would be greatly appreciated.

    #2
    Hello,

    Thank you for your post.

    Please provide output demonstrating the issue.

    Set TraceOrders to true in State.Configure and print the order object in OnOrderUpdate().

    TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.

    Printing the order object in OnOrderUpdate() will allow you to track the progression of the order from submitted, to working, to filled, cancelled, or rejected.

    These tools will let you know what happens to the order.
    TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

    Comment


      #3
      Thanks for your reply Gaby. Didn't know about TraceOrders.Will try that. I will say that I already have as the first line in OnOrderUpdate a Print statement that prints to order object. That's how I know OnOrderUpdate is not being entered into when the problem arises.

      Here's a snippet of output when everything is working properly:

      OnBarUpdate 12/20/2024 3:04:00 PM Entry Price: 1.04313 Direction: Long Stop Loss: 1.04280 Limit: 0.00000
      Analyzer: 12/20/2024 3:04:00 PM BP: 0.00000 SP: 0.00000 LS: 1.04313 SS: 0.00000 LT: 0.00000 RE: 1.04332 ML: 0.00000 BO: False
      12/20/2024 3:04:00 PM Buy Price: 0.00000 Sell Price: 0 Long SL:: 1.04313 Short SL: 0.00000 Limit Price: 0.00000 ReEntry Price: 1.04332 Do Bail: False
      ***** Entering update orders section of OnBarUpdate
      12/20/2024 3:04:00 PM OnBarUpdate() - ChangeOrder - Updating Long order stop to 1.04313 for 100000 lots
      12/20/2024 3:03:00 PM Strategy 'dcwAutoTrader25/344716799': Changed custom managed order: orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=Accepted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.0428 quantity=200,000 tif=Gtc oco='EURUSD _0302013132' filled=0 averageFillPrice=0 onBehalfOf='' id=1837 time='2024-12-20 15:02:01' gtd='2099-12-01' statementDate='2024-12-20' limitPriceChanged=0 quantityChanged=100000 stopPriceChanged=1.04313
      OnOrderUpdate orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=ChangePending instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.0428 quantity=200,000 tif=Gtc oco='EURUSD _0302013132' filled=0 averageFillPrice=0 onBehalfOf='' id=1837 time='2024-12-20 15:03:00' gtd='2099-12-01' statementDate='2024-12-20'
      OnOrderUpdate orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=ChangeSubmitted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04313 quantity=100,000 tif=Gtc oco='EURUSD _0302013132' filled=0 averageFillPrice=0 onBehalfOf='' id=1837 time='2024-12-20 15:03:00' gtd='2099-12-01' statementDate='2024-12-20'
      **** Done
      OnOrderUpdate orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=Accepted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04313 quantity=100,000 tif=Gtc oco='EURUSD _0302013132' filled=0 averageFillPrice=0 onBehalfOf='' id=1837 time='2024-12-20 15:03:00' gtd='2099-12-01' statementDate='2024-12-20'
      ==== 12/20/2024 3:04:00 PM OnOrderUpdate() - Long stop exit accepted at 1.04313 for 100000 lots
      OnOrderUpdate orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=Working instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04313 quantity=100,000 tif=Gtc oco='EURUSD _0302013132' filled=0 averageFillPrice=0 onBehalfOf='' id=1837 time='2024-12-20 15:03:13' gtd='2099-12-01' statementDate='2024-12-20'
      OnOrderUpdate orderId='535e3b73d07446a0b4f5de24dc5877fe' account='Sim101' name='Long Exit Stop' orderState=Filled instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04313 quantity=100,000 tif=Gtc oco='EURUSD _0302013132' filled=100000 averageFillPrice=1.04313 onBehalfOf='' id=1837 time='2024-12-20 15:03:13' gtd='2099-12-01' statementDate='2024-12-20'
      ==== 12/20/2024 3:04:00 PM OnOrderUpdate() - Long stop exit filled at 1.04313
      OnExecutionUpdate 12/20/2024 3:03:13 PM Long trade EXITED at 1.04313 for 100000 lots
      ++++ 12/20/2024 3:03:13 PM OnExecutionUpdate() - ReEnter long set at 1.04332 for 100000 lots
      12/20/2024 3:03:13 PM Strategy 'dcwAutoTrader25/344716799': Entered internal SubmitOrderUnmanaged() method at 12/20/2024 3:03:13 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=100,000 LimitPrice=0 StopPrice=1.0433'2 SignalName='Long Entry'
      OnOrderUpdate orderId='e14b95de23cf442787f028fb33f04df5' account='Sim101' name='Long Entry' orderState=Submitted instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.04332 quantity=100,000 tif=Gtc oco='EURUSD _0303133086' filled=0 averageFillPrice=0 onBehalfOf='' id=1838 time='2024-12-20 15:03:13' gtd='2099-12-01' statementDate='2024-12-20'
      OnOrderUpdate orderId='e14b95de23cf442787f028fb33f04df5' account='Sim101' name='Long Entry' orderState=Accepted instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.04332 quantity=100,000 tif=Gtc oco='EURUSD _0303133086' filled=0 averageFillPrice=0 onBehalfOf='' id=1838 time='2024-12-20 15:03:13' gtd='2099-12-01' statementDate='2024-12-20'
      OnOrderUpdate orderId='e14b95de23cf442787f028fb33f04df5' account='Sim101' name='Long Entry' orderState=Working instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.04332 quantity=100,000 tif=Gtc oco='EURUSD _0303133086' filled=0 averageFillPrice=0 onBehalfOf='' id=1838 time='2024-12-20 15:03:53' gtd='2099-12-01' statementDate='2024-12-20'
      OnOrderUpdate orderId='e14b95de23cf442787f028fb33f04df5' account='Sim101' name='Long Entry' orderState=Filled instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.04332 quantity=100,000 tif=Gtc oco='EURUSD _0303133086' filled=100000 averageFillPrice=1.04335 onBehalfOf='' id=1838 time='2024-12-20 15:03:53' gtd='2099-12-01' statementDate='2024-12-20'
      OnExecutionUpdate 12/20/2024 3:03:53 PM Long trade entered at 1.04335

      And here's a snippet later when it's not.

      OnBarUpdate 12/20/2024 3:07:00 PM Entry Price: 1.04335 Direction: Long Stop Loss: 0.00000 Limit: 0.00000
      Analyzer: 12/20/2024 3:07:00 PM BP: 0.00000 SP: 1.04304 LS: 1.04304 SS: 0.00000 LT: 0.00000 RE: 1.04328 ML: 0.00000 BO: False
      12/20/2024 3:07:00 PM Buy Price: 0.00000 Sell Price: 1.04304 Long SL:: 1.04304 Short SL: 0.00000 Limit Price: 0.00000 ReEntry Price: 1.04328 Do Bail: False
      ***** Entering update orders section of OnBarUpdate
      **** 12/20/2024 3:07:00 PM OnBarUpdate() - ChangeOrder to reverse long order to short at 1.04304 for 200000 lots
      **** Done

      Comment


        #4
        One correction. I did know about TraceOrders and was already using it.

        Comment


          #5
          I figured to provide an output showing the change from working to no longer working. Note that I perform my analysis and ordering at the beginning of a new bar. And I bracket every order command with a print statement before with the values used and one that says "Done" afterward. Where you see "Yesterday's Bid/Ask" I use both bid and ask data of the previous bar to make my decisions in my Analyzer and I iterate through OnBarUpdate until the bid and ask have the same date stamp.

          OnBarUpdate 12/23/2024 12:51:00 PM Entry Price: 1.04065 Direction: Long Stop Loss: 1.04065 Limit: 0.00000
          Analyzer: 12/23/2024 12:51:00 PM BP: 0.00000 SP: 0.00000 LS: 1.04067 SS: 0.00000 LT: 0.00000 RE: 1.04080 ML: 0.00000 BO: False
          12/23/2024 12:51:00 PM Buy Price: 0.00000 Sell Price: 0 Long SL:: 1.04067 Short SL: 0.00000 Limit Price: 0.00000 ReEntry Price: 1.04080 Do Bail: False
          ***** Entering update orders section of OnBarUpdate
          12/23/2024 12:51:00 PM OnBarUpdate() - ChangeOrder - Updating Long order stop to 1.04067 for 100000 lots
          12/23/2024 12:50:11 PM Strategy 'dcwAutoTrader25/344716799': Changed custom managed order: orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=Accepted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04065 quantity=100 tif=Gtc oco='EURUSD _1249032507' filled=0 averageFillPrice=0 onBehalfOf='' id=1850 time='2024-12-23 12:49:03' gtd='2099-12-01' statementDate='2024-12-23' limitPriceChanged=0 quantityChanged=100000 stopPriceChanged=1.04067
          OnOrderUpdate orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=ChangePending instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04065 quantity=100 tif=Gtc oco='EURUSD _1249032507' filled=0 averageFillPrice=0 onBehalfOf='' id=1850 time='2024-12-23 12:50:11' gtd='2099-12-01' statementDate='2024-12-23'
          OnOrderUpdate orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=ChangeSubmitted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04067 quantity=100,000 tif=Gtc oco='EURUSD _1249032507' filled=0 averageFillPrice=0 onBehalfOf='' id=1850 time='2024-12-23 12:50:11' gtd='2099-12-01' statementDate='2024-12-23'
          **** Done
          OnOrderUpdate orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=Accepted instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04067 quantity=100,000 tif=Gtc oco='EURUSD _1249032507' filled=0 averageFillPrice=0 onBehalfOf='' id=1850 time='2024-12-23 12:50:11' gtd='2099-12-01' statementDate='2024-12-23'
          ==== 12/23/2024 12:51:00 PM OnOrderUpdate() - Long stop exit accepted at 1.04067 for 100000 lots
          OnOrderUpdate orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=Working instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04067 quantity=100,000 tif=Gtc oco='EURUSD _1249032507' filled=0 averageFillPrice=0 onBehalfOf='' id=1850 time='2024-12-23 12:50:12' gtd='2099-12-01' statementDate='2024-12-23'
          OnOrderUpdate orderId='62b6238afaa54cfe9da4823548019b4a' account='Sim101' name='Long Exit Stop' orderState=Filled instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.04067 quantity=100,000 tif=Gtc oco='EURUSD _1249032507' filled=100000 averageFillPrice=1.04067 onBehalfOf='' id=1850 time='2024-12-23 12:50:12' gtd='2099-12-01' statementDate='2024-12-23'
          ==== 12/23/2024 12:51:00 PM OnOrderUpdate() - Long stop exit filled at 1.04067
          OnExecutionUpdate 12/23/2024 12:50:12 PM Long trade EXITED at 1.04067 for 100000 lots
          ++++ 12/23/2024 12:50:12 PM OnExecutionUpdate() - ReEnter long set at 1.0408 for 100000 lots
          12/23/2024 12:50:12 PM Strategy 'dcwAutoTrader25/344716799': Entered internal SubmitOrderUnmanaged() method at 12/23/2024 12:50:12 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=100,000 LimitPrice=0 StopPrice=1.0408'0 SignalName='Long Entry'
          OnOrderUpdate orderId='c9eac8971ef146a08cbff5b4452721f4' account='Sim101' name='Long Entry' orderState=Submitted instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.0408 quantity=100,000 tif=Gtc oco='EURUSD _1250122217' filled=0 averageFillPrice=0 onBehalfOf='' id=1851 time='2024-12-23 12:50:12' gtd='2099-12-01' statementDate='2024-12-23'
          OnOrderUpdate orderId='c9eac8971ef146a08cbff5b4452721f4' account='Sim101' name='Long Entry' orderState=Accepted instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.0408 quantity=100,000 tif=Gtc oco='EURUSD _1250122217' filled=0 averageFillPrice=0 onBehalfOf='' id=1851 time='2024-12-23 12:50:12' gtd='2099-12-01' statementDate='2024-12-23'
          OnOrderUpdate orderId='c9eac8971ef146a08cbff5b4452721f4' account='Sim101' name='Long Entry' orderState=Working instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.0408 quantity=100,000 tif=Gtc oco='EURUSD _1250122217' filled=0 averageFillPrice=0 onBehalfOf='' id=1851 time='2024-12-23 12:50:21' gtd='2099-12-01' statementDate='2024-12-23'
          OnOrderUpdate orderId='c9eac8971ef146a08cbff5b4452721f4' account='Sim101' name='Long Entry' orderState=Filled instrument='EURUSD' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=1.0408 quantity=100,000 tif=Gtc oco='EURUSD _1250122217' filled=100000 averageFillPrice=1.04081 onBehalfOf='' id=1851 time='2024-12-23 12:50:21' gtd='2099-12-01' statementDate='2024-12-23'
          OnExecutionUpdate 12/23/2024 12:50:21 PM Long trade entered at 1.04081
          ***** New bar
          Yesterday's Bid 12/23/2024 12:51:00 PM open 1.0407, high 1.04081, low 1.04065, close 1.04081
          Yesterday's Bid 12/23/2024 12:51:00 PM open 1.0407, high 1.04081, low 1.04065, close 1.04081
          Yesterday's Ask 12/23/2024 12:51:00 PM open 1.04072, high 1.04083, low 1.04069, close 1.04083
          OnBarUpdate 12/23/2024 12:52:00 PM Entry Price: 1.04081 Direction: Long Stop Loss: 0.00000 Limit: 0.00000
          Analyzer: 12/23/2024 12:52:00 PM BP: 0.00000 SP: 1.04064 LS: 1.04064 SS: 0.00000 LT: 0.00000 RE: 1.04085 ML: 0.00000 BO: False
          12/23/2024 12:52:00 PM Buy Price: 0.00000 Sell Price: 1.04064 Long SL:: 1.04064 Short SL: 0.00000 Limit Price: 0.00000 ReEntry Price: 1.04085 Do Bail: False
          ***** Entering update orders section of OnBarUpdate
          **** 12/23/2024 12:52:00 PM OnBarUpdate() - ChangeOrder to reverse long order to short at 1.04064 for 200000 lots
          **** Done
          ***** New bar​

          Comment


            #6
            Hello dweems,

            Can you please post a reduced sample script demonstrating the issue? The reduced script should only contain the code necessary to reproduce issue. Additionally, please remove any prints that aren't necessary as well. The only info we need is the information from TraceOrders(), the order object from OnOrderUpdate(), and a print notifying when ChangeOrder() is called.

            Comment


              #7
              I am happy to report I figured out what the problem has been. I wasn't properly setting order objects to null in OnOrderUpdate() when the orders were filled. Funny that I was setting all kinds of other order objects to null but not those. Why your system gives no error and simply stops processing, that's for you to figure out. But at least my strategy is working. It happens to be Christmas Eve when I solved the problem, so I guess I got a really nice gift for Christmas. Now to figure out if my strategy will actually be profitable......... Thanks for you help.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              52 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              130 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              70 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              44 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              48 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X