No announcement yet.

Partner 728x90


Need to make strategy pausing while position updating

  • Filter
  • Time
  • Show
Clear All
new posts

    Need to make strategy pausing while position updating


    I came across serious problem working with real accounts (
    Suppose i have a signal to open position. In OnBarUpdate handler i check if my position is flat and if it is, I send the order to broker. Since broker will respond me asynchronously, I need to pause my strategy until this order will be proceeded. Ok, no problem: I check in OnBarUpdate whether I have non-filled orders. If yes, then I skip this bar update.

    So far so good: I still have flat position and one order being processed. Some milliseconds later broker changes the status of the order to "filled", but position state is still "flat". Then OnBarUpdate event raises and my strategy has:
    a) still flat position
    b) no orders in processing

    and it decides to open position once again. It sends new order. Some milliseconds later OnPositionUpdate event triggers, but it's too late, because the second order is already sent! This situation repeats until OnPositionUpdate triggers before OnBarUpdate. Sometimes I have 4 or even 5 orders in a row for the same signal!

    In other words, if i have this sequence of events, i have multiple entries:

    - OnOrderUpdate (position is flat, order filled)
    - OnBarUpdate(positon is still flat, all orders filled).
    - OnPositionUpdate (position is long/short)

    How can i distinguish in OnBarUpdate event, that my position will be updated soon from flat state? I thought about setting some flag in OnOrderUpdate handler and then resetting it in OnPositionUpdate handler. But it seems not very reliable

    The issue may be replicated only with real accounts. In Sim accounts OnPositionUpdate follows right after OnOrderUpdate. OnBarUpdate never occurs between these events.

    Any recommendations?

    Hello rfsettling,

    Thanks for the post.

    The best way to handle this situation would be to use the Order object as a flag to prevent OnBarUpdate from running if it is null. So you can check for your trade condition along with a check for the order object == null. Alternatively, you could set up a bool condition to let OnBarUpdate know that you are in a waiting state.

    Please see the sample here on the OnOrderUpdate page that shows how to set up an Order object and check for null in OnOrderUpdate. After you have submitted an intitial order, you can prevent OnOrderUpdate from running as long as that Order object is null and not filled.

    Please let me know if you have any questions on the material described here.
    Chris L.NinjaTrader Customer Service


      I have a new problem now

      I supposed that each order filling must cause OnPositionUpdate firing (maybe not immediately, but it must). In 99% it does. But today I had an issue, when order was filled, but OnPositionUpdate has never been called.

      How it can be?

      I've attached 2 logs of my strategy: with correct behaviour, and incorrect
      Attached Files


        Hello rfsettling,

        Thanks for the reply.

        Could you please post a sample of what your script is doing?

        Please also follow this guide on implementing and using OnOrderUpdate and OnExecutionUpdate.

        The OnExecutionUpdate function can be very useful. If your script's logic relies on order fill events then you should use OnExecutionUpdate for useful exection event information.

        I look forward to being of further assistance.
        Chris L.NinjaTrader Customer Service



          Is this a common issue? I have several strategies that I built in Strategy Builder but have not yet run live. Do I need to build in protection against this kind of thing, or does this seem to have been a one-off situation? I have no programming experience and hate to hire somebody to address this if it's likely to be unnecessary.

          On a related note, I went to the above linked page about using OnOrderUpdate and imported the zip file. Dumb question, but now that I've done that, how do I access it?

          Thanks in advance for your help.


            Hello Ranger Brad,

            OnBarUpdate is a data driven method which updates as data is received and a bar updates and OnPositionUpdate is an order / position driven method that updates from the order processing thread.

            These threads are no synchronous and there is no guarantee to the order. For anything that relies on the position being changed should be coded in the OnPositionUpdate method.

            However, if the position changes, this will trigger OnPositionUpdate after the position is fully updated.

            If you are able to demonstrate the position of the strategy is changing without OnPositionUpdate triggering, I would like an export of a simple demonstration script that demonstrates.
            Chelsea B.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by Jeremytang, 07-14-2018, 06:45 PM
            21 responses
            Last Post mav8rick  
            Started by LeonK, Yesterday, 09:26 PM
            1 response
            Last Post bltdavid  
            Started by NicholasJoannette, Yesterday, 03:01 PM
            2 responses
            Last Post NicholasJoannette  
            Started by rach1994, Yesterday, 12:20 PM
            1 response
            Last Post bltdavid  
            Started by litamm89, Yesterday, 11:00 AM
            5 responses
            1 like
            Last Post rafael_delima86