Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

race condition between onorderupdate and onbarupdate

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

    race condition between onorderupdate and onbarupdate

    i have code which does the following

    protected override void OnOrderUpdate(IOrder order)
    {
    loops thro all orders.
    if all orders are filled which means my positins are closed..
    set order objects to null
    print("setting obects = null");
    }

    protected onBarupdate()
    {
    if ((Position.MarketPosition == MarketPosition.Short) && (Position.Quantity>0))
    {
    // do some action with the order objects
    The code is getting triggered for few ticks after the exitorders are filled. becoz i had set the order objects = null. i get exceptions. I dunno why the Position.Quantity>0 is true becoz its printed after the printing of "setting obects = null"



    }



    }

    what is a good practise to check within the barupdate if the positions are closed becoz Position.Quantity does not reflect reality during runtime.
    mind you, this is random that it happens only for USD.CAD instrument and not for the others.

    #2
    Hi Junkone,

    It depends what you are trying to do. You comment // do some action with the order objects and it may be better off placing these actions in a handler designed for them.

    OnBarUpdate() is raised whenever there has been a trade, but if you need to work with order objects, look at OnOrderUpdate(), OnExecution().


    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      What do you mean onbarupdate called when there is a trade?
      your documentation states its called only on incoming ticks.

      The OnBarUpdate() method is called whenever a bar is updated. If the "CalculateOnBarClose" property is set to true, it is only called on the close of each bar, otherwise it is called on each incoming tick. This is the method where all of your strategy or indicator core calculation logic should be contained.

      the issue that i see is that i can see on my onOrderUpdate when my orders are executed and my positions are closed based on total count buyquantity-sellquantity=0. then i set the orders objects = null.

      however for the next 2-3 ticks, the following condition is true.
      if ((Position.MarketPosition == MarketPosition.Long) && (Position.Quantity > 0))
      i dont know the order in which your internal objects are called. but when i process the onOrderUpdate and find that my exit orders have executed fully, i am assuming that the Position.MarketPosition should be MarketPosition.Flat. In some cases it is not the case.

      Comment


        #4
        Yes, tick = trade. Someone bought or sold and OBU is raised.

        If timing is critical, best practice is work with objects in the event handler designed for them. NinjaTrader is multithreaded, so there is no set order of this first and then that.

        If you need to do something as a result of a fill and timing is critical, then work in OnExecution.

        If there's something not working as you expect, please post a code snippet and we'll give a run here.
        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        649 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        370 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        573 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        576 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X