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

Does OnPositionUpdate() periodically update from Broker?

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

    Does OnPositionUpdate() periodically update from Broker?

    Dear Ninja team,

    So, I have a strategy running using unmanaged approach and it is finally working properly using multi-thread (trickier than I thought, that multi-thread). No errors/overfills on high pace tick data. It is set to "Keep Running" on disconnection and handles fine on connection loss, but something bothers me. (Broker: IB. Tests done on: IB's Paper Acct).

    What happens during the following hypothetical scenario, however unlikely:
    1. Inside Ninja Strategy: Call SubmitOrder(). Market Order.
    2. Order State says Pending Submit.
    3. Disconnection for 10 minutes. We don't get any Accepted or Working states.
    4. Meanwhile, Broker received the Market Order. Fills it. But we don't get any updates...as we are still disconnected.
    5. Connection comes back online.
    6. ??? What happens here?

    So, my questions are concerning step 6:

    1). After 10 minutes in the dark, will we receive a Filled Order state? maybe the broker sent it, while we were disconnected...so now, we don't get a Filled order state update?
    2). After 10 minutes in the dark, will OnPositionUpdate() get a broker communicaton? The documentation doesn't really mention if that method ever communicates with the broker. Does it? How often?
    2.5). If it does communicate, I assume Position.MarketPosition and Position.Quantity inside OnBarUpdate() will update just the same? (I know asynchronously)

    Thank you for your time,
    Xraider.

    #2
    Hello Xraider,
    Thanks for your post, and welcome to the NinjaTrader forum.

    For simplicity I will answer your questions in order.
    1). After 10 minutes in the dark, will we receive a Filled Order state? maybe the broker sent it, while we were disconnected...so now, we don't get a Filled order state update?
    Only orders in a state or "Working" or "Accepted" are guaranteed to have reached your broker. The order state "PendingSubmit" means that the order was only submitted to your connectivity provider and they are waiting to hear back from your broker to confirm that it was accepted. Your order state would update as soon as you are connected to the internet again and if your brokerage accepted the order then you would receive a Filled order state. That being said, there is no guarantee that a "PendingSubmit" order will be accepted or even reach your broker.

    Forum Post- Where do your orders reside?

    2). After 10 minutes in the dark, will OnPositionUpdate() get a broker communicaton? The documentation doesn't really mention if that method ever communicates with the broker. Does it? How often?
    OnPositionUpdate() is guaranteed to be called as soon as the strategies position changes. If you reconnect to the internet after an order state of "PendingSubmit" and your broker accepts the order, OnPositionUpdate() will be called.

    2.5). If it does communicate, I assume Position.MarketPosition and Position.Quantity inside OnBarUpdate() will update just the same? (I know asynchronously)
    These values would both update along with OnPositionUpdate() once you regain internet connectivity and the order is accepted by your broker.

    Please let me know if you have any further questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh G.,

      Thank you for getting back to me.

      The order state "PendingSubmit" means that the order was only submitted to your connectivity provider and they are waiting to hear back from your broker to confirm that it was accepted.
      Ah, yes I am aware of this. The order can come back Rejected. In this case, we are assuming it went through and was Accepted - though from our end we do not see the "Accepted" state as we are disconnected. Basically, the broker sees "Accepted" 1 second into our disconnection, but we are not online to see that "Accepted".

      Your order state would update as soon as you are connected to the internet again and if your brokerage accepted the order then you would receive a Filled order state.
      This part is what I'm really after. Just to confirm, I am guaranteed to receive some kind of "final type" Order State update? "Filled"/"Rejected"/"Cancelled"/"Unknown"? I am worried that if the disconnection is long enough (10-20 minutes), there is no update upon re-connection...or maybe the update was sent when we were offline and isn't re-sent?

      OnPositionUpdate() is guaranteed to be called as soon as the strategies position changes. If you reconnect to the internet after an order state of "PendingSubmit" and your broker accepts the order, OnPositionUpdate() will be called.
      The method OnPositionUpdate() doesn't get called particularly fast when working on a tick-by-tick trading. When I do a basic Long entry if last Close is lower than current and short on opposite, and then put Print() statements in OnBarUpdate() OnOrderUpdate and OnPositionUpdate(). I see starting Long Position in OnBarUpdate() first, then new Short position in OnOrderUpdate(), then same Short in OnBarUpdate(), then new Long position in OnOrderUpdate(), then same Long in OnBarUpdate(), then new Short in OnOrderUpdate(), then same Short in OnBarUpdate(), and now finally OnPositionUpdate() reflects this same short position. So, it took a while before OnPositionUpdate() was called even though the position changed multiple times from Long to Short and vice versa. (do note that my latency is 5ms and lower, so things move very very fast).

      While, yes I am able to handle my strategy fine by just placing Position.Quantity type references within OnBarUpdate() and OnOrderUpdate(). This tells me that OnPositionUpdate() is not necessarily called EVERY time that there is a position change - not at high speed tick-by-tick.

      Sorry about being a bit lengthy, but I want things to be clear and on the same page. So, if OnPositionUpdate() is not called 100% for every position change when using tick-by-tick basis, then will Position.Quantity reference be immediately triggered on reconnect and updated from Broker (Assume 10-20 minute disconnect)? Is an update to Position.Quantity (assuming strategy didn't stop) guaranteed after a lengthy disconnection? My worry is that at some point the Broker gives up, and doesn't update Ninja. Is that possible? If it is, when would the broker give up...20+hours? Or is Ninja pinging the Broker immediately on re-connection and demanding a Position.Quantity & Position.MarketPosition update.

      I know you generally answered that in your second sentence of the second quotation, but please confirm this for me Josh - any detail is appreciated.

      Apologies for being lengthy. Thank you for your help and time.
      -Xraider.

      Comment


        #4
        Hello Xraider,

        Thanks for your note.
        Just to confirm, I am guaranteed to receive some kind of "final type" Order State update? "Filled"/"Rejected"/"Cancelled"/"Unknown"?
        Yes, I am confirming that you are guaranteed to receive this order state update when you are once again connected to the internet. This will happen no matter if you reconnect in 10 seconds or 10 hours.
        The method OnPositionUpdate() doesn't get called particularly fast when working on tick-by-tick trading.
        This tells me that OnPositionUpdate() is not necessarily called EVERY time that there is a position change - not at high speed tick-by-tick.
        These methods are called when that particular event happens and not before. There is no guarantee to the order that these methods are called because they are event driven. OnPositionUpdate() is guaranteed to be called immediately when a strategies position changes. OnBarUpdate() will be called immediately when a bar closes. If you have a 1 minute bar then there is a full minute between calls to this method. OnOrderUpdate() is called immediately when an order managed by a strategy changes state. An order will change state when a change in order quantity, price or state occurs.

        OnPositionUpdate()
        https://ninjatrader.com/support/help...tionupdate.htm

        OnBarUpdate()
        https://ninjatrader.com/support/help...nbarupdate.htm

        OnOrderUpdate()
        https://ninjatrader.com/support/help...rderupdate.htm
        Is an update to Position.Quantity (assuming strategy didn't stop) guaranteed after a lengthy disconnection?
        Yes, you are correct. This value is guaranteed to be updated whenever there is a change to your strategies current position size.
        My worry is that at some point the Broker gives up, and doesn't update Ninja. Is that possible?
        There is not a risk of your broker 'giving up' and not communicating your account information back to the platform. You were correct, 'NinjaTrader will ping the Broker immediately on re-connection and demand a Position.Quantity & Position.MarketPosition update.'

        Please let me know if you have any further questions.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Great, I think that answers everything I was after. Thank you Josh.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by swestendorf, Today, 11:14 AM
          2 responses
          5 views
          0 likes
          Last Post NinjaTrader_Kimberly  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          4 responses
          13 views
          0 likes
          Last Post xiinteractive  
          Started by Mupulen, Today, 11:26 AM
          0 responses
          2 views
          0 likes
          Last Post Mupulen
          by Mupulen
           
          Started by Sparkyboy, Today, 10:57 AM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by TheMarlin801, 10-13-2020, 01:40 AM
          21 responses
          3,917 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Working...
          X