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

"KeepRunning and Recalculate

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

    "KeepRunning and Recalculate

    Hello.
    I have couple question about details of how KeepRunning and Recalculate modes work with unmanaged strategies.

    Let's assume, we have:
    - Long position, 100 contracts;
    - take profit 1 order (tp1), 40 contracts;
    - take profit 2 order (tp2), 60 contracts;

    And this moment connection lost.
    Tp1 filled with 40 contracts.
    Tp2 partially filled with 30 contracts.
    Connection established again.
    Keep running Recalculate
    Position 1. Will OnPositionUpdate() be triggered for events that happened while disconnected?
    2. Will OnPositionUpdate() will be triggered for all events that will happens after connection established again.
    3. Will Account Position be the same to Strategy Position?
    Orders 1. Will OnOrderUpdate() and OnExecutionUpdate() be triggered for events that happened while disconnected?
    2. Will OnOrderUpdate() and OnExecutionUpdate() will be triggered for all events (I mean Tp2 mainly) that will happens after connection established again.
    Lost bars 1. Will all price data be uploaded and added to history?
    2. Will OnBarUpdate() be triggered for all these bars?
    3. Are all these bars will be considered as historical, or real-time?
    3.1 If these bars will be considered as real-time: What if processing or these bars cause sending orders? Will strategy send them or not?
    3.2 If these bars will be considered as historical: will strategy fall in "waiting until flat" state (if StartBehavior.WaitUntilFlat
    ?)





















    Thank you.
    Last edited by fx.practic; 01-23-2020, 04:33 AM.
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    Thank you for your post.

    Did you mean to fill in some data there, or did you have a question we can try to answer for you? There's not much in your post.

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate, sorry, I occasionally posted unprepared question and did not how to remove thread.
      In about 15 mins I'll fill the table above.
      Last edited by fx.practic; 01-22-2020, 04:38 PM.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        The very first post with the question is ready.
        fx.practic
        NinjaTrader Ecosystem Vendor - fx.practic

        Comment


          #5
          Hello fx.practic,

          Thank you for your reply.

          As this is an unmanaged strategy, even though NinjaTrader provides connection loss handling features it is still important to ensure your recovered strategy's internal state is not in limbo. Should you have internal variables tracking various information it may be necessary for you to program your own additional connection loss handling into OnConnectionStatusUpdate() to properly recover all aspects of your strategy in the manner you desire.

          Let's go through your questions one at a time.

          Position
          1. Will OnPositionUpdate() be triggered for events that happened while disconnected?

          No events can occur while disconnected, regardless of which ConnectionLossHandling setting you use - if no data is coming in, OnBarUpdate cannot run.

          2. Will OnPositionUpdate() will be triggered for all events that will happens after connection established again.

          Strategy events that occur after reconnection will trigger position updates for orders, yes.

          3. Will Account Position be the same to Strategy Position?

          This depends on which ConnectionLossHandling setting you pick - Keep running would keep the strategy running, and since it's not like you can place manual orders while your connection is down, when the connection is reestablished the strategy will resume as if no disconnect occurred and your account should be synced with the strategy still.

          When using ConnectionLossHandling.Recalculate, recalculations will only occur if the strategy was stopped based on the conditions below. Should the connection be reestablished before the strategy was stopped, the strategy will continue running without recalculating as if no disconnect occurred.
          • If data feed disconnects for longer than the time specified in DisconnectDelaySeconds, the strategy is stopped.
          • If the order feed disconnects and the strategy places an order action while disconnected, the strategy is stopped.
          • If both the data and order feeds disconnect for longer than the time specified in DisconnectDelaySeconds, the strategy is stopped.
          When the data feed then reconnects, if the strategy was stopped because of one of the above items, it will attempt to recalculate the current position it should be in. If that's different than the current account position the two could get out of sync, depending on the Start Behavior of the strategy.

          Orders
          1. Will OnOrderUpdate() and OnExecutionUpdate() be triggered for events that happened while disconnected?


          No orders will be triggered for events that occurred while disconnected - no events will be triggered until live data resumes. However, when using Recalculate, it will include these new bars in its recalculations and the calculated position on restart may be different than what it was on disable.

          2. Will OnOrderUpdate() and OnExecutionUpdate() will be triggered for all events (I mean Tp2 mainly) that will happens after connection established again.

          Again, Strategy events that occur after reconnection will trigger order and execution updates for orders, yes.

          Lost bars
          1. Will all price data be uploaded and added to history?


          On reconnection, the chart should automatically fill in missing historical data up to the current price, yes.

          2. Will OnBarUpdate() be triggered for all these bars?

          No. If you're using recalculate, it will be used to recalculate the new expected position of the Strategy, but no live orders would be sent.

          3. Are all these bars will be considered as historical, or real-time?

          Historical.

          3.1 If these bars will be considered as real-time: What if processing or these bars cause sending orders? Will strategy send them or not?

          3.2 If these bars will be considered as historical: will strategy fall in "waiting until flat" state (if StartBehavior.WaitUntilFlat?)


          If start behavior is wait until flat, and you're using Recalculate, yes. If you are using keep running, no; the strategy never stopped, it just kept running, so the Start Behavior wouldn't apply here.

          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hello, Kate.
            Thank you for the great and detailed answer.
            Let me now, please, check myself did I catch it well.

            When connection lost \ re-established, the strategy went through multiple stages.
            All my questions concerned only one stage, “processing of missed bars”.

            I fill in the table based on how I understand your answers.
            Is this correct?
            Keep running Recalculate
            State 1, regular work before connection lost - -
            State 2, connection lost, 77 bars missed - -
            State 3, connection established - -
            State 4, processing of 77 missed bars Not processed 1. Each of 77 bars will be processed.
            2. OnBarUpdate() will be triggered 77 times.
            3. State == State.Historical
            4. Calculate == Calculate.OnBarClose
            State 5, regular work - -
            Last edited by fx.practic; 01-28-2020, 01:28 PM.
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment


              #7
              Hello fx.practic,

              Thank you for your reply.

              That pretty much sums things up, yes. I would add that the processing of the 77 missed bars when using recalculate will calculate using OnBarClose as the Calculation mode since these bars would be considered historical data.

              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Great clarification, Kate, thanks!
                I updated the table for those, who will read it after us.

                You can be sure about further questions )

                UPD: here is one: How can I determine in code (in OnBarUpdate(), for example) that one of 77 processing now?
                Last edited by fx.practic; 01-28-2020, 01:29 PM.
                fx.practic
                NinjaTrader Ecosystem Vendor - fx.practic

                Comment


                  #9
                  Hello fx.practic,

                  Thank you for your reply.

                  There wouldn't really be a way to know specifically as the strategy is going to start at the beginning again when it recalculates, so those 77 bars would just be treated like they were historical data and the strategy was just starting up. I suppose if you did a print from on bar update if the bar is historical, you could tell since there would be more prints after the disconnect than before, but there's not really a way to monitor that it's recalculating using bars it didn't previously have from the code.

                  Please let us know if we may be of further assistance to you.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Clear, thank you, Kate.
                    fx.practic
                    NinjaTrader Ecosystem Vendor - fx.practic

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Mizzouman1, Today, 07:35 AM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by Radano, 06-10-2021, 01:40 AM
                    20 responses
                    616 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by i019945nj, 12-14-2023, 06:41 AM
                    6 responses
                    67 views
                    0 likes
                    Last Post i019945nj  
                    Started by aa731, Today, 02:54 AM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by BarzTrading, Today, 07:25 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post BarzTrading  
                    Working...
                    X