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

Determining Connection Status from within a Script

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

    Determining Connection Status from within a Script

    I have a script that creates and writes to a file. When I start NT8 (before connection) the script will execute by default - and hence, the file will be written.

    Is there a way I can somehow check for a state of 'Connected' within the script so's I can choose whether or not to execute?

    Thank you,

    G.

    #2
    Hello G,

    Thank you for your post.

    You can utilize the method OnConnectionStatusUpdate() in your script:

    This is an event-driven method that is called for each change in connection status. You can also get connection information, such as ConnectionStatus, by looping through the Connections (you could even loop through to find a specific connection by name with Options.Name). For more information and snippets:You could set a condition in your script to only create the file to write to if the connection status is ConnectionStatus.Connected.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you Emily, looks promising.

      So, based on the info you gave, what I plan to use is (a form of) the code:
      //Get the status of the order system

      protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
      {
      if(connectionStatusUpdate.Status==ConnectionStatus.C onnected)
      {
      isConnected = true;
      }

      elseif(connectionStatusUpdate.Status==ConnectionStatus.C onnectionLost)
      {
      isConnected = false;
      }
      }

      To avoid major rabbit-holing could you please give me the code I need to "call" the "function" OnConnectionStatusUpdate in State.Configure.

      Many thanks,

      G.

      Comment


        #4
        Hello G,

        Thank you for your reply.

        As I previously mentioned, OnConnectionStatusUpdate() is an event-driven method that is called for each change in connection status. It may not be called manually in State.Configure; it is only called each time a connection status changes. I suggest adding this method to your script along with print statements, such as those in the help guide examples. Save a NinjaScript Output window in your workspace and observe the prints as you launch the platform, connect/disconnect manually a few times, and this should help to better understand how this method is called for updates of connection status. To see where to add this in your script, start a new script such as an indicator and select this method from the "Additional event methods" page of the NinjaScript Wizard:Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you Emily C.

          Checked out the options you suggested. Sadly, no actual resolution to my problem. You see, I only get notification that I am Connected when State == State.Realtime, which I could have done already. What I really want to know is "am I connected" on first (historical) bar loaded (i.e. 1000+ bars earlier).

          I thought that ReloadAllHistoricalData(); might be what I needed, but this only succeeds in telling me that I have corrupted historical data. Sorting out this particular rabbit hole is not where I'd have hoped to go.

          Also, by way of explanation, I am (very) old school and fluent in COBOL, FORTRAN, Basic, Algol, VB, and proficient in Java Script. For me C# is proving to be quite challenging - despite having already successfully created 2000+ lines of (working) code So, please take that into account.

          That having been said, I think I learned something new - i.e. event-driven method​s exist. So I'm wondering if there is any sort of list of these I might be able to peruse to discover what other goodies have eluded me.

          Well, I still would like resolution to my problem - are there any other ideas?

          Thanks,

          G.

          Comment


            #6
            Hello G,

            Thank you for your reply.

            Based on the information you have described, I don't think I fully understand the problem you are describing. You originally mentioned that the script executes when you start NinjaTrader. You also mention that "What I really want to know is "am I connected" on first (historical) bar loaded (i.e. 1000+ bars earlier)." as well as you are only getting notified that you are connected when State == State.Realtime. If you were to create a test script, such as an indicator, that prints the state as well as if you are connected or not, what are the results if you connect to data before applying the indicator to a chart vs. applying the indicator to a chart prior to connecting to data?

            What are the results if you print the connection status in each state of OnStateChange()?The NinjaScript Lifecycle could also be informational in this scenario, as some states are hit multiple times throughout the life of a script:While I do not have a list of all event-driven methods, you could use the search feature in the help guide and search the terms "event driven" for some relevant pages. Methods such as OnBarUpdate(), OnMarketData(), OnStateChange(), etc are all event driven methods:The idea of event driven methods starts to cross into the topic of NinjaTrader being multi-threaded. More information about multi-threading may be found here:


            I appreciate your patience and look forward to assisting you further.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Emily C,

              Here are the 3 sections of code I have implemented (as suggected), followed by the NinjaScript Output.

              In the output, the first half (up until ## Connection Status = Connecting) represents what I get when not connected but have just done an F5 refresh on the indicator.

              The second half shows what then happened when I actually connect.

              The ## OnStateChange = Terminated - Connection ON ## seems to be spurious - I suspect this is executed immediately before defaults as an indicator reset.

              All the ## Connection not tested ## outputs represent new bars forming. The first half I would wish to ignore (as I am not connected), the second half are after I have requested Connection - I want to process these as I am creating a new file based on them.

              I hope you see my dilema - I do not know when I am actually connected/connecting until after all the Non-Realtime bars I want to use, have already been loaded.

              Further advice please.

              G.


              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

              // CONNECTION TEST METHOD //

              private int nConnectionActive = -1;
              protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
              {
              if(connectionStatusUpdate.Status == ConnectionStatus.Connected)
              {
              nConnectionActive = 1;
              Print("Connected for orders at " + DateTime.Now);
              }

              else if(connectionStatusUpdate.Status == ConnectionStatus.ConnectionLost)
              {
              nConnectionActive = 0;
              Print("Connection for orders lost at: " + DateTime.Now);
              }
              Print("## Connection Status = " + connectionStatusUpdate.Status);

              if(connectionStatusUpdate.PriceStatus == ConnectionStatus.Connected)
              {
              Print("Connected to price feed at " + DateTime.Now);
              }

              else if(connectionStatusUpdate.PriceStatus == ConnectionStatus.ConnectionLost)
              {
              Print("Connection to price feed lost at: " + DateTime.Now);
              }
              }



              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

              // ON STATE CHANGE - CONNECTION STATUS //

              protected override void OnStateChange()
              {
              if ( nConnectionActive == -1)
              {
              Print ("## OnStateChange = " + State + " - Connection not tested ##");
              } else if (nConnectionActive == 0)
              {
              Print ("## OnStateChange = " + State + " - Connection OFF ##");
              } else if (nConnectionActive == +1)
              {
              Print ("## OnStateChange = " + State + " - Connection ON ##");
              } else {
              Print ("## OnStateChange = " + State + " - ASSUMED? Connection ON ##");
              }

              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

              // ON BAR UPDATE - CONNECTION STATUS //

              private int nNotTestedCount = 0;
              protected override void OnBarUpdate()
              {
              if (nConnectionActive == -1)
              {
              if (nNotTestedCount < 10 ) Print ("## Connection not tested ##");
              nNotTestedCount++;
              return;
              } else if (nConnectionActive == 0)
              {
              Print ("## Connection OFF ##");
              return;
              } else {
              Print ("## Connection ON ##");
              return; // ######################################## Remove after tesing
              }

              ~~~~~~~~~~~~~~~~ OUTPUT ~~~~~~~~~~~~~~~~~~~~~~~~


              ## OnStateChange = SetDefaults - Connection not tested ##
              ## OnStateChange = Terminated - Connection ON ##
              ## OnStateChange = Configure - Connection not tested ##
              ## OnStateChange = DataLoaded - Connection not tested ##
              ## OnStateChange = Historical - Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## OnStateChange = Transition - Connection not tested ##
              ## OnStateChange = Realtime - Connection not tested ##
              ## Connection Status = Connecting
              Connected for orders at 12/10/2023 11:34:36
              ## Connection Status = Connected
              Connected to price feed at 12/10/2023 11:34:36
              ## OnStateChange = Terminated - Connection ON ##
              ## OnStateChange = SetDefaults - Connection not tested ##
              ## OnStateChange = Configure - Connection not tested ##
              ## OnStateChange = DataLoaded - Connection not tested ##
              ## OnStateChange = Historical - Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## Connection not tested ##
              ## OnStateChange = Transition - Connection not tested ##
              ## OnStateChange = Realtime - Connection not tested ##
              ## Connection Status = Connecting
              Connected for orders at 12/10/2023 11:35:18
              ## Connection Status = Connected
              Connected to price feed at 12/10/2023 11:35:18
              ## Connection ON ##
              ## Connection ON ##
              ## Connection ON ##
              ## Connection ON ##​


              Comment


                #8
                Hello G,

                Thank you for your patience.

                In this case, I suggest assigning an account to a variable, then using the <Account>.Conneciton.Status information. The snippet on the following page shows how to assign the Sim101 account to the myAccount object in State.SetDefaults:


                Although the snippet on this page prints the <Account>Connection.Status in OnAccountItemUpdate(), you should be able to access this information as long as the account object is not null:


                Here is an example:
                Code:
                private Account myAccount;
                
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
                }
                if (myAccount != null)
                Print("State: " + State + " myAccount connection status: " + myAccount.Connection.Status);
                }
                
                protected override void OnBarUpdate()
                {
                if (myAccount.Connection.Status != ConnectionStatus.Connected)
                return;​
                }​
                Please let me know if I may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by JoMoon2024, Today, 06:56 AM
                0 responses
                6 views
                0 likes
                Last Post JoMoon2024  
                Started by Haiasi, 04-25-2024, 06:53 PM
                2 responses
                17 views
                0 likes
                Last Post Massinisa  
                Started by Creamers, Today, 05:32 AM
                0 responses
                5 views
                0 likes
                Last Post Creamers  
                Started by Segwin, 05-07-2018, 02:15 PM
                12 responses
                1,786 views
                0 likes
                Last Post Leafcutter  
                Started by poplagelu, Today, 05:00 AM
                0 responses
                3 views
                0 likes
                Last Post poplagelu  
                Working...
                X