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 Error

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

    Object reference not set to an instance of an object Error

    Hi,

    I am clearly doing something stupid and even though it is a simple thing, for the world I cannot see where the error is. Any insight would be greatly appreciated. Thank you.

    In my strategy I define a variable for my custom indicator:
    Code:
    private NinjaTrader.NinjaScript.Indicators.MyCustomIndicator myCustomIndicator;
    In State == State.DataLoaded I have
    Code:
    myCustomIndicator = NinjaTrader.NinjaScript.Indicators.MyCustomIndicator();
    This fails to compile with the error:
    NinjaTrader.NinjaScript.Indicators.MyCustomIndicat or' is a 'type', which is not valid in the given context.
    Weird.

    So I change this line to
    Code:
    myCustomIndicator = MyCustomIndicator();
    And add
    Code:
    AddChartIndicator(myCustomIndicator);
    The script compiles fine but when the strategy is run, the line
    Code:
    myCustomIndicator = MyCustomIndicator();
    Generates the error:
    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    I'm sure the solution is simple, I just can't see it.

    #2
    Hello.

    Thanks for the post.

    There is something going wrong when instantiating the custom indicator, If your strategy is doing the following then the initialization of the custom indicator is failing and you would have to debug the indicator's OnStateChange method.

    Code:
    public class MyCustomStrategy : Strategy
    	{
    		private NinjaTrader.NinjaScript.Indicators.MyCustomIndicator myCustomIndicator;
                    ...
    Code:
    else if (State == State.DataLoaded)
    			{
    				myCustomIndicator = MyCustomIndicator();
    				AddChartIndicator(myCustomIndicator);
    			}
    Are you able to load the custom indicator by itself on a chart?

    Could you post the OnStateChange method of the custom indicator?

    I look forward to your reply.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thank you for your reply. I do believe that there is an issue with instantiating the custom indicator. I will check the indicator OnState method as suggested.

      As an aside, the indicator compiles and runs fine by itself. It does, however, have expandable property classes that are not decorated with the [NinjaScriptProperty] attribute and I wonder whether this is causing a constructor issue.

      EDIT: So I have disected my indicator in detail and I think I have found the problem:
      The indicator uses ChartBars and ChartControl for various things. This is not an issue when the indicator runs on the chart. It is an issue when called from a strategy as ChartBars and ChartControl are null. How do I fix this?
      Last edited by Zeos6; 03-28-2018, 11:22 AM.

      Comment


        #4
        Hello.

        Thanks for the reply.

        I am able to use ChartBars and ChartControl in my testing. Are you trying to load this strategy from a chart or from the Strategies tab of the Control Center?

        I look forward to your reply.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris,

          Figured it out - finally! Was not checking ChartBars and ChartControl for null in the indicator. Worked as an indicator but created issues in Strategy testing. Finally figured it out from NinjaScript Best Practices and the section on accessing elements on the UI. Thank you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Christopher_R, Today, 12:29 AM
          0 responses
          9 views
          0 likes
          Last Post Christopher_R  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          166 responses
          2,235 views
          0 likes
          Last Post sidlercom80  
          Started by thread, Yesterday, 11:58 PM
          0 responses
          3 views
          0 likes
          Last Post thread
          by thread
           
          Started by jclose, Yesterday, 09:37 PM
          0 responses
          8 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,415 views
          0 likes
          Last Post Traderontheroad  
          Working...
          X