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

Object reference not set to an instance of an object

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

    Object reference not set to an instance of an object

    I have developed a "Forecast1" Indicator that forecasts something about the market and a "Forecast2" Indicator that creates an array of "Forecast1" instances used to forecast more about the market. All that works great when "Forecast2" is attached directly to a chart.

    Now I am trying to use "Forecast2" forecasts in an automatic trading Strategy. Even though "Forecast2" runs without error when attached directly to a chart, merely setting a reference to it during (State == State.DataLoaded) in the Strategy results in the following NT log and trace file error records:
    Code:
     "Indicator 'Forecast2' : Error on calling 'OnStateChange' method: Object reference not set to an instance of an object"
    I understand what that means, have carefully read NinjaScript Best Practices in the Help, have thoroughly studied my code looking for possible issues, and have experimentally tried many things without being able to avoid that error. Calling "Forecast1" from the Strategy in exactly the same way works fine with no error.

    1) The fact that "Forecast1" can be used by the Strategy and "Forecast2" cannot suggests that the problem is related to "Forecast2" using an array of "Forecast1" indicators. Is that a known limitation?

    2) Do you know a technical difference between attaching an Indicator to a chart and referencing it in a Strategy that could cause it to work one way and not the other (such as maybe a processing thread difference)?

    "Forecast2" code obviously could be embedded in the Strategy, but I would rather not do that, because it is complex and I would like to avoid having to synchronize and test future changes in multiple applications.

    #2
    Hello caveat_lector,


    1) The fact that "Forecast1" can be used by the Strategy and "Forecast2" cannot suggests that the problem is related to "Forecast2" using an array of "Forecast1" indicators. Is that a known limitation?
    From the given details I really couldnt comment, I would need to see what code was used to know what was null. Object reference not set to an instance of an object simply means something was null when it was used.
    2) Do you know a technical difference between attaching an Indicator to a chart and referencing it in a Strategy that could cause it to work one way and not the other (such as maybe a processing thread difference)?
    A strategy hosts an indicator without a chart so it would not have any Chart related variables like ChartControl for instance.

    The most simple way to debug this is to add prints into the affected indicators OnStateChange throughout your code to see where it hits the error when processing your code. Once you have the general idea of the syntax if you are not sure what the problem is you could post that part and I can take a look to give some ideas.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I am using ChartControl properties like this during (State == State.Historical):
      Code:
      Draw.TextFixed(this,
           "myStdDevdollarValue", "Std Dev $" + StdDevDollarValue.ToString("0.00"),
           TextPosition.BottomRight, ChartControl.Properties.ChartText,
           ChartControl.Properties.LabelFont, Brushes.Green, Brushes.Transparent, 0);
      However, I use essentially the same thing in other indicators to display the values of various other variables without having errors when hosting them from Strategies. Furthermore, commenting-out the code above doesn't eliminate the Object reference error that I have now.

      Comment


        #4
        Hello caveat_lector,

        If you commented the code out and the re applied the strategy fresh but it still has an error that would likely not be the specific code causing the error.

        The code you have shown should be surrounded by a null check because there are situations where ChartControl is null:

        Code:
        if(ChartControl != null)
        {
            //code here
        }

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks, I will add the null check.

          Comment


            #6
            I found the problem. I have ChartBars.Count in a while-loop conditional test during (State == State.Historical). So, now I need an alternative way to test against the number of primary data series historical closing prices. Of course I can easily count them myself, but is there built-in access to that count that doesn't depend on having a chart?

            Comment


              #7
              Hello caveat_lector,

              An indicator would generally just use the Count property for knowing its bar Count are you able to use that in your logic in this situation?

              it would also not be expected to access bar data before bar 0 in OnBarUpdate, OnStateChange is not an area where that type of logic would generally occur. Can this potentially be moved to OnBarUpdate and if so do you still see the error?

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I haven't known about the Count property and have always used ChartBars.Count. Anyway, Count works fine, fixed the error, and my Forecast2 indicator now works as expected when hosted by the Strategy.

                I am not accessing data before bar 0 in OnBarUpdate. I am using a conditional while-loop during State.Historical to populate an array with "Forecast1" instances where the number of instances depends on the amount of historic data available. It probably would be more appropriate to do that in State.Transition, but the code seems to work fine as it is now.

                Incidentally, despite the fact that the Draw.TextFixed( ... ) code above uses ChartControl.Properties, ChartControl isn't set to null when the indicator is hosted by the Strategy and text displays on the screen with everything exactly the same as when the indicator is attached to a chart.

                Thanks very much for your help.

                Comment


                  #9
                  Jesse, as stated above, using the Count property instead of ChartBars.Count fixed the error I was having when hosting the indicator in a Strategy, but today I wanted to know some details about Count before using it differently for another purpose and wasn't able to find anything about it in the Help. Can you point me to where it is documented?
                  Last edited by caveat_lector; 09-05-2020, 11:51 PM.

                  Comment


                    #10
                    Hello caveat_lector,

                    You can find that documented here: https://ninjatrader.com/support/help.../nt8/count.htm


                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by GussJ, 03-04-2020, 03:11 PM
                    11 responses
                    3,221 views
                    0 likes
                    Last Post xiinteractive  
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    438 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post FAQtrader  
                    Working...
                    X