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

Using OnStartUp() and Plot() with an empty Chart

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

    Using OnStartUp() and Plot() with an empty Chart

    Hello,

    I created an indicator and instantiated some classes in OnStartUp(). These instantiatons are used within public override Plot().

    And here is a special situation when that causes a problem:

    - I opened this indicator on a 5 minute chart with a data history (Days to load) set to 10 days.
    -There is no January data available in the data repository for the selected instrument. That means, the chart window is blank, no bars painted.

    I observed that in this situation OnStartUp() is not executed and Plot() throws an exception because my classes are not instantiated.

    Question: Is this intended behavior, that OnStartUp() is not executed if there is no data availabe for the concerning instrument?

    Regards
    Ralph

    #2
    Hi Ralph,

    Thank you for your observations. Unfortunately this is expected. OnStartup() requires at least one bar.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Observations:

      1) Presumably OnStartUp() is not called because there is nothing to plot. Would that not be a bug if using live data and then something to plot does arrive?

      2) If there is nothing to plot, then Plot() should not be called.

      Please ask Development:
      • Is it really correct to not call OnStartUp(), considering that later data could arrive if this is a live plot?

      • Other functions such as OnBarUpDate() and Plot() should never be called if OnStartUp() has not been called. Doing so is an invitation to bugs -- they will be running without the needed initialization having been done.

      --EV

      Comment


        #4
        EV,

        OnStartup() is not called if there are no bars. This is expected. If there are bars, then it is correctly called, and before OnBarUpdate()

        If you have a scenario where NinjaTrader is not acting the way you expect, please share a simple script and the steps needed to see the issue. Thanks.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          No, I was just curious what happens if you open a chart with a security with no bars, and subsequently bars do arrive. Wouldn't that scenario cause a problem?

          My other point is that is seems as if later methods, such as OnBarStartUp() and Plot() should be called if, and only if, OnStartUp() has been called. The reason for that suggestion is that they depend on OnStartUp() to do initialization for them. It is wrong to call them when you know that initialization has not had a chance to happen. That is what happened in the original poster's case, and why Plot() threw an exception. I am suggesting it is a system bug that Plot() ever got called in that case.

          --EV

          Comment


            #6
            We are not aware of issues in this area. If you or Ralph have something you'd like us to look into, please share the script and the steps and we can run on our end.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I'm not sure why, but we do not seem to be communicating.

              The original poster's code threw an exception because Plot() was called even though OnStartUp() had not been.

              I am asserting the NT is wrong -- has a bug -- if it calls Plot() without first calling OnStartUp().

              --EV

              Comment


                #8
                Hello Ryan,

                I too think it is an consistancy issue. If OnStartUp() is not called (and OnBarUpdate too) because there is no bar data on the chart, then Plot() shouldn't be executed either.

                I appended a little test case as you requested. Please note, to reproduce the exception,
                - one needs to open a chart for some instrument where no data is stored in the data repository.
                - Then one applies the indicator attached (PlotExceptionTest) and you will get an exception, because a not instantiated class object is used in Plot().

                Regards
                Ralph
                Attached Files

                Comment


                  #9
                  Ryan,

                  Here is what the Help page for OnStartUp() says:
                  You can override the OnStartUp() method to initialize any variables or resources you wish to use in the custom indicator or strategy. This method is called only once immediately prior to the start of your script, but after the Initialize() method.
                  That says to me that OnStartUp() will be called exactly once:
                  • It will be called after Initialize() has been called
                  • It will be called prior to calling any other script code

                  From the developer's point of view, that works just fine. Note that all other code depends on (a) Initalize() being called first and (b) OnStartUp() being called after that, but before any other code gets called. All other code should be able to depend on initialization being done by OnStartUp().

                  In the case cited by the original poster, Plot() got called without OnStartUp() being called first. That is wrong, it is an NT bug, and it must be fixed.

                  --EV

                  Comment


                    #10
                    Thanks for the file Ralph.

                    OnStartup() will be called before OnBarUpdate(), even if no bars are loaded initially but then start arriving.

                    This is not the case for overriding the plot method. If you base any critical logic with OnStartUp, the script requires bars to process this logic.

                    Thanks to EV and Ralph for your suggestions on this.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_RyanM View Post
                      Thanks for the file Ralph.

                      OnStartup() will be called before OnBarUpdate(), even if no bars are loaded initially but then start arriving.
                      Good. That is what I was hoping to hear. That answers my first point.

                      Now just make that happen for Plot() as well.

                      --EV

                      Comment


                        #12
                        Originally posted by NinjaTrader_RyanM View Post
                        OnStartup() will be called before OnBarUpdate(), even if no bars are loaded initially but then start arriving.
                        This is not the case for overriding the plot method. If you base any critical logic with OnStartUp, the script requires bars to process this logic.
                        Yes Ryan, that describes exactly the current situation: OnStartUp() amd OnBarUpdate() are syncronized by design. Plot() is not included in this concept.

                        Now there are 2 solutions possible to fix the inconsistancy:
                        1) NT includes Plot() in this syncronisation cycle (I would prefer this scenario).
                        2) NT changes the Help document for OnStartUp() and includes the exceptions to be considered before using it.

                        Regards
                        Ralph
                        Last edited by Ralph; 01-21-2011, 11:09 AM.

                        Comment


                          #13
                          The next question is whether there are any other exceptions.

                          As the Help documentation stands today, nothing other than Initialize() and Dispose() should be called with first calling OnStartUp().

                          I could see qualifying that for good reason, but I agree with Ralph that the cleaner way would be to just not call anything (other than Dispose())

                          --EV

                          Comment


                            #14
                            I think again that that was an architecture decision, which while it is possible could have been made another way, is handled by their API providing means to limit drawing if there are no bars to paint. Hence the BarsRequired directive, which if not specified, it seems the chart only starts drawing after 20 bars. Of course, one can always use a bars limiter escape block.

                            Personally, I prefer to see the exception telling me that I do not have sufficient bars, rather than a silent failure because there are not enough bars to paint.

                            Comment


                              #15
                              Thanks all for your observations and suggestions. I will forward to development and documentation.
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Torontobluejays, Today, 08:43 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Torontobluejays  
                              Started by sastrades, 01-31-2024, 10:19 PM
                              12 responses
                              190 views
                              0 likes
                              Last Post sastrades  
                              Started by Jimmyk, 01-26-2018, 05:19 AM
                              8 responses
                              864 views
                              0 likes
                              Last Post emuns
                              by emuns
                               
                              Started by giulyko00, 04-24-2024, 12:03 PM
                              6 responses
                              29 views
                              0 likes
                              Last Post giulyko00  
                              Started by 00nevest, 04-27-2024, 02:27 PM
                              3 responses
                              27 views
                              0 likes
                              Last Post 00nevest  
                              Working...
                              X