Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.



    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.

        The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


        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

        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 CortexZenUSA, Today, 12:53 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            5 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,265 views
            0 likes
            Last Post sidlercom80  
            Started by Barry Milan, Yesterday, 10:35 PM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X