• 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

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by reedstickets, Today, 07:53 PM
        0 responses
        5 views
        0 likes
        Last Post reedstickets  
        Started by Atilla, Today, 04:05 PM
        0 responses
        17 views
        0 likes
        Last Post Atilla
        by Atilla
         
        Started by Displacer, Today, 02:57 PM
        2 responses
        13 views
        0 likes
        Last Post Displacer  
        Started by SAILIKITY, 05-22-2019, 05:41 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by rmillar, 01-09-2019, 06:02 PM
        6 responses
        75 views
        1 like
        Last Post eDanny
        by eDanny
         
        Working...
        X