• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need to make strategy pausing while position updating

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

    Need to make strategy pausing while position updating

    Hello

    I came across serious problem working with real accounts (Forex.com).
    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?

    #2
    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.

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

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

    Comment


      #3
      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

      Comment


        #4
        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.

        https://ninjatrader.com/support/foru...ead.php?t=7499

        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.

        https://ninjatrader.com/support/help...tionupdate.htm

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

        Comment


          #5
          Greetings,

          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.

          Comment


            #6
            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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by swcooke, Yesterday, 11:18 AM
            5 responses
            24 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by Lisa Marshall, 01-25-2020, 04:11 PM
            1 response
            18 views
            0 likes
            Last Post NinjaTrader_Jim  
            Started by utilizator, 01-25-2020, 11:36 AM
            2 responses
            34 views
            0 likes
            Last Post utilizator  
            Started by Aporshnev, Today, 08:46 AM
            2 responses
            15 views
            0 likes
            Last Post Aporshnev  
            Started by fx.practic, Today, 08:54 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X