Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnStateChange behavior when there is no Connection

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

    OnStateChange behavior when there is no Connection

    Hello,

    I need to clarify this issue...

    1) what happens in the OnStateChange when Ninja starts up with No Connection?

    2) Does it make sense to never do any custom code in OnStateChange when there is no Connection?

    3) Are there guidlines published somewhere?

    4) What happens in State=State.Terminated when there is no Connection?

    Thanks

    #2
    Hello llanqui,

    This depends on the script type.

    "what happens in the OnStateChange when Ninja starts up with No Connection?"

    For an indicator, OnStateChange() will progress through State.SetDefaults (if a new instance), State.Configure, State.DataLoaded, and State.Historical, then process all available historical data in OnBarUpdate(), even if there is no connection.

    However, a strategy will not enable so will not progress through State.Configure.

    "Does it make sense to never do any custom code in OnStateChange when there is no Connection?​"

    There could be times that you want an indicator to make changes to a chart based on loaded historical data. Depends on what you want the script to do.

    "Are there guidlines published somewhere?"

    On what specifically?
    There is some information on the forum about how the State progresses.


    "What happens in State=State.Terminated when there is no Connection?"

    Depends on what the script type is and what code is in State.Terminated.
    If you loaded an indicator that created some objects with no connection, and then you decided to remove the indicator from the chart, even without connecting, the objects may still need to be disposed of in State.Terminated because the indicator is being removed from the chart.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      this is only for an Indicator

      it seems best then to execute no code at all when there is no connection

      Comment


        #4
        Originally posted by llanqui View Post
        it seems best then to execute no code at all when there is no connection
        My experience tends to say you shouldn't, generally speaking,
        have to care about the status of a connection.

        Much of what you're asking very much depends on context,
        and what you're wanting your script to do.

        Can you explain any of your plans?
        Have you found a specific problem?

        Unless your working on something pretty exotic, you're
        probably wasting your time.

        I mean, by design, NinjaTrader programming is an event
        driven endeavor, and connection/disconnection could occur
        at any time, and with standard NinjaScript programming,
        this is handled internally rather gracefully for most all of
        the cases I know.

        What special situation have you found where the status
        of the connection is so important?

        What problem are you trying to solve?



        Comment


          #5
          I wrote a complete custom ninjascript system designed for fast scalping

          I get object not instantiated and index errors when there is no connection, on some of the scripts

          I can review the earlier Ninja comments and see what I can do


          Comment


            #6
            Hello llanqui,

            That may or may not be related to a connection. I would think most likely a connection is not the cause.

            What is the specific line of code causing the error?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              another question is....

              what happens in the State system in an Indicator when


              1) Ninja starts up with no connection

              2) A connection is made

              3) All connections are closed

              Comment


                #8
                Hello Chelsea

                I'll have to research this a bit....it does not happen when there is a connection...it either is an index out of range or an unistantiated object

                one example I had to fix was the AccountSelector object is null if there is no connection, I was subscribing to the change event and the object didn't exist when there is no connection



                Comment


                  #9
                  it is really time consuming to track these errors down with Print statements..

                  cause I often don't get the name of the indicator causing the problem (I don't use the Indicator Label on the charts)

                  any better using Visual Studio debugger?

                  and, it would be good if the error messages in the Log told me what Instrument and what Indicator caused the error. If I don't show the Label on the Chart, then the Log text does not tell me what indicator cause the problem

                  Comment


                    #10
                    Hello llanqui,

                    "what happens in the State system in an Indicator when
                    1) Ninja starts up with no connection"

                    State.Configure > State.DataLoaded > State.Historical > OnBarUpdate() with historical

                    "2) A connection is made"

                    Historical data will be reloaded and the script will be reloaded

                    State.Terminated on the old instance > State.Configure on the new clone > State.DataLoaded > State.Historical > OnBarUpdate() with State.Historical > State.Real-time > OnBarUpdate() with State.Realtime.

                    ​3) All connections are closed

                    No change


                    "one example I had to fix was the AccountSelector object is null if there is no connection, I was subscribing to the change event and the object didn't exist when there is no connection"

                    Your code should always check that a variable/property is not null before attempting to use it, if the object type is a nullable type.

                    This includes selectors or other UIElements, Account objects, Atm objects, order objects, execution objects, custom classes, etc.


                    "it is really time consuming to track these errors down with Print statements..

                    cause I often don't get the name of the indicator causing the problem (I don't use the Indicator Label on the charts)

                    any better using Visual Studio debugger?​"

                    You can use 3rd party editors such as Visual Studio, but this would be out of the realm of what our support teams will advise or assist with.
                    We would recommend you use prints and really understand the code you have written.


                    "and, it would be good if the error messages in the Log told me what Instrument and what Indicator caused the error. If I don't show the Label on the Chart, then the Log text does not tell me what indicator cause the problem"

                    There is a feature request for keeping the indicator name in the error message tracked with ID # SFT-4093. I will add your vote to this request.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Visual Studio is not so hard, but it takes a little setup...

                      some of the objects/indexes are my code...and may require a connection....I will have to look closer...would love to have the code base produce no exceptions, no matter what

                      thanks!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      580 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      335 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      102 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X