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

Account - Multi connection disconnect protection

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

    Account - Multi connection disconnect protection

    Hi,

    I hoping for some advice to protect from disconnects during automated trading.

    I want to start automated trading using 2 connections, connection 1 for data and connection 2 for broker orders only (no data). The problem I see is that if I get a disconnect from connection 1, NT8 will automatically switch to connection 2 for data for the period of disconnection and therefore my chart will have innacurate data as it will be a mix of data from connection 1 and connection 2. I do not want my chart data to become innacurate and needs to all come from conneciton 1 only at all times.

    I would like to put some logic in place which will stop NT8 processing any chart data from connection2 in the event of disconnect from connection 1. Actually I don;t want connection 2 to ever supply chart data if that is an easier solution. I'm not sure how to program the logic within ConnectionStatusUpdate to switch off data from connection 2. I considered a simple logic that if connection one disconnects then use the Disconnect() method on connection 2 however I see 2 problems with this;

    1. I ideally want to stay connected to the broker so that I can implement a failsafe to flatten everything if the connection 1 data does not reconnect within a set time period
    2. If I disconnect() connection 2, I cannot see a way to reconnect connection 2 once connection 1 is established.

    If anyone can suggest or share logic to turn off the data from connection 2 that would be most appreciated.

    Thanks for any input.

    #2
    Could it be as simple as using the following within ConnectionStatusEventArgs?

    if(eCopy.Connection.Options.Name!="name of first connection")
    {
    e.Connection.Disconnect();
    }

    That way, if connection event arg eCopy isn;t the name of connection 1, it will just disconnect?
    Last edited by b16_aln; 10-29-2020, 06:58 AM.

    Comment


      #3
      Hello b16_aln,

      Thanks for your post.

      You could consider using a bool that is controlled by the PriceStatus updates for your desired data feed, and then you could control your logic to process only when your bool is true.

      I have attached an example that can model this. The strategy can then keep running until the connection re-established and then your logic will continue processing. (This assumes Connection Loss Handling is set to Keep Running with a high enough value for Disconnect Delay Seconds to withstand the connection loss.)

      You could also consider attempting to disconnect from your broker feed similar to how you have described, but this would effectively disable the strategy as it would if you manually disconnected from the Control Center. I would instead suggest to just disable the strategy with CloseStrategy() then, since we would not be able to programmatically re-enable the strategy after you have programmatically reconnected to the broker after the price feed has been re-established.

      CloseStrategy() - https://ninjatrader.com/support/help...sestrategy.htm

      If you want to look further into programmatically disconnecting and reconnecting to a connection, you can see the documentation below and the the AddOn Framework example also linked, but I would not see this as away forward.

      Connection - https://ninjatrader.com/support/help...tion_class.htm

      AddOn Framework example - https://ninjatrader.com/support/help...t_overview.htm

      You could also consider using a Virtual Private Server to have NinjaTrader run on a dedicated server if your home internet connection is not stable. If something like that interests you, please let me know and we can have a colleague give more information about VPS providers in our EcoSystem.

      We look forward to assisting.
      Attached Files
      JimNinjaTrader Customer Service

      Comment


        #4
        Thanks for the reply Jim.

        Yes, I could use a bool to stop trading but that doesn;t solve my real problem. I want to stop NT8 from switching to connection2 to get chart data when connection 1 fails. I do not want there to be a mix of data on the chart from 2 different sources. I have connection 1 as my data source and connection 2 as my broker, if connection 1 disconnects I do not want NT8 to start pulling data from my broker which is connection 2.

        Comment


          #5
          Hello b16_aln,

          Then you could consider forcibly disconnecting from the other connection when you see the price feed for your preferred connection is lost.

          You consider trying something like:

          Code:
          private bool AllowTrading = false;
          private Connection con1, con2;
          
          protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
          {
              if (connectionStatusUpdate.Connection.Options.Name == "My Kinetick")
                  con1 = connectionStatusUpdate.Connection;
              if (connectionStatusUpdate.Connection.Options.Name == "My Interactive Brokers")
                  con2 = connectionStatusUpdate.Connection;
          
          
              if(connectionStatusUpdate.PriceStatus == ConnectionStatus.Connected && connectionStatusUpdate.Connection.Options.Name == "My Kinetick")
              {
                  AllowTrading = true;
              }
              else if(connectionStatusUpdate.PriceStatus == ConnectionStatus.ConnectionLost && connectionStatusUpdate.Connection.Options.Name == "My Kinetick")
              {
                  AllowTrading = false;
                  con2.Disconnect();
              }
          }
          JimNinjaTrader Customer Service

          Comment


            #6
            Thanks Jim, yes thats also what I came up with. As mentioned, the problem with that though is how do I get the broker account to re-connect again after connection 1 re-connects?

            Comment


              #7
              You can see the AddOn Framework example and associated Connection code for how to programmatically reconnect, but if the strategy programmatically disconnects a connection it uses, the strategy will get disabled and there wouldn't be any further code execution to get to that reconnection, and there is no means to programmatically re-enable the strategy.

              I suppose you could build an AddOn that incorporates a timer and then attempts to connect when that timer is triggered, but we still come to a crossroads with the strategy needing to be re-enabled by the user. I have prepared an example to test that out and we can see that the strategy remains disabled.

              Example usage:

              NinjaTrader.NinjaScript.AddOns.MyTimerReconnectAdd On myTimerReconnectAddOn = new NinjaTrader.NinjaScript.AddOns.MyTimerReconnectAdd On(con2.Options.Name);

              We look forward to assisting.
              Attached Files
              JimNinjaTrader Customer Service

              Comment


                #8
                Thank you Jim, that's very helpful.

                Comment


                  #9
                  Hi Jim,

                  So here's what i've done;

                  I've focussed on the Connection.Connections List.

                  When my data feed is manually connected first and then broker account conected second, all is good; Connection.Connection[0] is the data feed and Connection.Connections[1] is the broker feed.

                  When I lose connection to datafeed and then reconnect, Connection.Connections[0] becomes the broker and Connection.Connection[1] becomes the data feed so my understanding is that data is now being pulled from broker feed as it is the first connection in the connection list.

                  So, i've used code within connectionstatusupdate to check that data feed is [0] and broker feed is [1] and if not I edit the list and move data feed back to [0]. When I print the edited list it appears to have been successful, data feed is back at [0]. However, On the NT8 control panel the connection tab is still showing my broker feed as the principal connection so i assume that NT8 is still treating the broker feed as [0] and using that for data. My guess is that there is some sort of refresh i need to do so that NT8 recognises the re-organisation of Connection.Connections list because the the Control panel still seems to show broker as [0] when it is not.

                  Thanks

                  Comment


                    #10
                    I've double checked wihtout using any code and it seems that the NT8 default is that it always reverts to data provider over broker no matter combination they are connected. I tried connecting to broker first then data provider and the chart always refreshes with data provider data and overwrites the broker data even when broker is connected frist. I was always under the impression that NT8 will take data from whichever account connected first so if I connect broker first and then data, the chart will populate from the broker and vice versa. Can you confirm that this is no longer the case and that data provider always takes priority now? Thanks

                    EDIT: OK, done further testing and for anyone that's interested here is what is happening. The default position is as originally thought, whatever is connected first is preffered data feed. What was confusing is when i refreshed, historical data was populating from connection2 then going back to connection 1 for live so my chart was a mismatch of historical data from one provider and live data from a seperate provider. The option in setting to select your preference of live and historical data does work though so that overcomes the issueeven when your data feed connects second, as long as it is selected in settings as prefferred it will remain the preferredlive feed.

                    You can also use the option above to always set data feed as [0] in Connection.Connections but it appears all i've really done is spent the day working out the logic behind using the NT8 settings for prefered feeds, lol!
                    Last edited by b16_aln; 10-30-2020, 07:45 AM.

                    Comment


                      #11
                      Hello b16_aln,

                      Do you have preferred connections configured in the Control Center's Tools > Options > Market data menu, or have you tried setting preferred connections?

                      If preferred connections are not set, I would expect the connection order to be respected for where the data for the asset types are being fetched.

                      Are you stating you are finding that when you do not have preferred connections set, the market data provider is taking precedence over the broker when you connect to your broker first? I would not expect this.

                      Our expectations on on using Multiple Connections would be reflective of what is documented in the Help guide. (Link below.)

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

                      If you are finding something contrary to this, could you let us know the following?

                      Who is your broker and market data provider?

                      What specific steps are you taking to reproduce and observe this behavior? (Please be explicit as I will want to test the same on my end.)

                      Can you confirm if you see this behavior on the latest version of the platform?

                      Download - https://ninjatrader.com/PlatformDirect

                      I look forward to hearing back.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Jim,

                        It's behaving as expected, what threw me off was that when I reconnected to datafeed (now in conection 2) the chart auto reloads the historical data and was still being taken from data feed on connection 2. This caused the brokerfeed (now connection1 data) data on the chart to be overwritten with datafeed data which made it look like datafeed was taking priority again, however if i let it run on again the live data was coming from connection 1, so there was a mix of connection2 historic and then live connection 1.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by bill2023, Yesterday, 08:51 AM
                        6 responses
                        27 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by yertle, Today, 08:38 AM
                        4 responses
                        10 views
                        0 likes
                        Last Post yertle
                        by yertle
                         
                        Started by NinjaTrader_ChelseaB, 01-08-2017, 06:59 PM
                        80 responses
                        19,667 views
                        5 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by adeelshahzad, Today, 03:54 AM
                        2 responses
                        16 views
                        0 likes
                        Last Post adeelshahzad  
                        Started by dappa, Today, 09:18 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Working...
                        X