Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 (B6) : Strategy 'OnStateChange' Object reference error

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

    NT8 (B6) : Strategy 'OnStateChange' Object reference error

    NT Team,

    Please find attached two scripts TestIndicator and TestStrategy.

    TestIndicator identifies the BarPeriodType in State.Configure. Adding TestIndicator to a chart works as expected.
    Code:
    public class TestIndicator : Indicator
    {
     protected override void OnStateChange()
     {
     if (State == State.SetDefaults)
      {
       ...
      }
      else if (State == State.Configure)
      {
       BarsPeriodType _barsPeriodType = BarsPeriod.BarsPeriodType;
       Print(_barsPeriodType);
      }
     }
    
     protected override void OnBarUpdate() { }
    }
    TestStrategy creates an instance of TestIndicator: Adding TestIndicator to a chart (with or without TestIndicator removed from the chart) results in the error message
    Indicator 'TestIndicator': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    Code:
    public class TestStrategy : Strategy
    {
     private TestIndicator _testIndicator;
    
     protected override void OnStateChange()
     {
      if (State == State.SetDefaults)
      {
       ...
      }
      else if (State == State.Configure)
      {
       _testIndicator = TestIndicator();
      }
     }
    
     protected override void OnBarUpdate() { }
    }
    I believe the above code is in line with the NT Guide - BarsPeriod, please confirm or correct my understanding. Please advise if this is expected behaviour.

    As always thanks and regards
    Shannon
    Attached Files
    Last edited by Shansen; 10-30-2015, 04:19 AM.

    #2
    Shannon,

    Are you using a custom bar type?

    Comment


      #3
      Hello,

      Thank you for posting the sample.

      I tried this on my end and did see the error, I am currently looking into what is happening there.

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

      Comment


        #4
        Hello,

        it appears that when an indicator is being hosted by a strategy, the BarsPeriod is null at this point causing the error.

        I wanted to ask, is this part of a larger file where this variable was required to be used in State.Configure or was this just based on the information you had found?

        I was going to run through the states to verify at what point this is no longer null, but wanted to first get a better idea of how you would be using the variable.

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

        Comment


          #5
          Jesse,

          Thanks for the quick reply.

          The information is required as part of a larger file. The example provided is purely for ease of understanding.
          I look forward to any solution/s you may offer.

          For completeness, the bar type is simply OHLC.

          Thanks again
          Shannon

          Comment


            #6
            Shansen,

            OHLC is technically a chart style, not a bar type. A bar type would be tick, volume, range, minute, etc.

            Comment


              #7
              strategesis,

              Apologies. If I understand correctly, the BarsPeriod.BarsPeriodType = Day.

              Shannon

              Comment


                #8
                Shansen,

                Ok. That means my suspicion as to what might be the problem can't be correct.

                Comment


                  #9
                  Hello,

                  I just wanted to update this thread with my findings.

                  This would be expected in this case because BarsPeriod can be null in State.Configure depending on the use case, it would not be suggested to use BarsPeriod for any type of series adding logic or in State.Configure because of this. The valid state in which BarsPeriod can be accessed in and after would be State.DataLoaded. The documentation will also be updated in the near future to reflect this.

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

                  Comment


                    #10
                    Jesse & NT Forum,

                    Thank you for your responses.

                    While incorporating State.DataLoaded naviages the problem of " 'OnStateChange' : Object reference not set to an instance of an object", the larger issue remains.
                    To further examine this issue, please find attached three scripts CalculationPeriod, TestIndicator and TestStrategy.

                    TestIndicator instantiates CalculationPeriod (an Addon) in State.DataLoaded. Adding TestIndicator to a chart works as expected; each NewPeriod is printed to the Output window.
                    Code:
                    public class TestIndicator : Indicator
                    {
                     private CalculationPeriod _calculationPeriod;
                     protected override void OnStateChange()
                     {
                     if (State == State.SetDefaults)
                      { ... }
                      else if (State == State.Configure)
                      { ... }
                      else if (State == State.DataLoaded)
                      {
                       _calculationPeriod = new CalculationPeriod(this);
                      }
                     }
                    
                     protected override void OnBarUpdate()
                     {
                      if (CurrentBar < 10) return;
                      if (_calculationPeriod.IsNewPeriod())
                       Print(string.Format("{0} is new period", Time[0].ToString("g")));
                     }
                    }
                    TestStrategy instantiates TestIndicator. Adding TestStrategy to a chart (with or without removing TestIndicator from the chart) results in IsNewPeriod never being true.
                    Code:
                    public class TestStrategy : Strategy
                    {
                     private TestIndicator _testIndicator;
                     protected override void OnStateChange()
                     {
                      if (State == State.SetDefaults)
                      { ... }
                      else if (State == State.Configure)
                      {
                       _testIndicator = TestIndicator();
                      }
                     }
                    
                     protected override void OnBarUpdate()
                     { ... }
                    }
                    The addon CalculationPeriod works as expected as part of an Indicator. However, as part of an Indicator in a Strategy, it does not. Any suggestions as to where I am going wrong would be greatly appreciated.

                    As always thanks and regards
                    Shannon
                    Attached Files
                    Last edited by Shansen; 11-06-2015, 04:07 AM.

                    Comment


                      #11
                      Hello,

                      I tried the sample but have been unable to reproduce the error you are.

                      Could you outline the steps you are taking to have this occur from the start?

                      While testing the strategy, I see the output:
                      11/2/2015 12:00 AM is new period
                      11/3/2015 12:00 AM is new period
                      11/4/2015 12:00 AM is new period
                      11/5/2015 12:00 AM is new period
                      11/6/2015 12:00 AM is new period

                      This was the same output the indicator provided by its self so I would assume this is doing what you have programmed the addon to do.

                      I created a new ES 12-15 chart, applied the strategy to the chart and enabled it. The default chart settings of 1 minute and 3 days were used. Can you provide any additional information from your tests?

                      Also have you removed all instances of the strategy from the control center along with all instances of the indicator from any charts?

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

                      Comment


                        #12
                        Originally posted by Shansen View Post
                        Jesse & NT Forum,

                        Thank you for your responses.

                        While incorporating State.DataLoaded naviages the problem of " 'OnStateChange' : Object reference not set to an instance of an object", the larger issue remains.
                        To further examine this issue, please find attached three scripts CalculationPeriod, TestIndicator and TestStrategy.

                        TestIndicator instantiates CalculationPeriod (an Addon) in State.DataLoaded. Adding TestIndicator to a chart works as expected; each NewPeriod is printed to the Output window.
                        Code:
                        public class TestIndicator : Indicator
                        {
                         private CalculationPeriod _calculationPeriod;
                         protected override void OnStateChange()
                         {
                         if (State == State.SetDefaults)
                          { ... }
                          else if (State == State.Configure)
                          { ... }
                          else if (State == State.DataLoaded)
                          {
                           _calculationPeriod = new CalculationPeriod(this);
                          }
                         }
                        
                         protected override void OnBarUpdate()
                         {
                          if (CurrentBar < 10) return;
                          if (_calculationPeriod.IsNewPeriod())
                           Print(string.Format("{0} is new period", Time[0].ToString("g")));
                         }
                        }
                        TestStrategy instantiates TestIndicator. Adding TestStrategy to a chart (with or without removing TestIndicator from the chart) results in IsNewPeriod never being true.
                        Code:
                        public class TestStrategy : Strategy
                        {
                         private TestIndicator _testIndicator;
                         protected override void OnStateChange()
                         {
                          if (State == State.SetDefaults)
                          { ... }
                          else if (State == State.Configure)
                          {
                           _testIndicator = TestIndicator();
                          }
                         }
                        
                         protected override void OnBarUpdate()
                         { ... }
                        }
                        The addon CalculationPeriod works as expected as part of an Indicator. However, as part of an Indicator in a Strategy, it does not. Any suggestions as to where I am going wrong would be greatly appreciated.

                        As always thanks and regards
                        Shannon
                        Can you debug in Visual Studio and tell us exactly which object is null?
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          Jesse and Matthew,

                          Apologies, the simplified scripts failed to replicate what I am seeing in my actual code.
                          I will investigate further.

                          Again, apologies
                          Shannon

                          Comment


                            #14
                            Ok, well just to be clear:

                            There are objects such as Instruments, Bars, BarsPeriod, TradingHours, etc. which cannot reliably be accessed in State.Configure... if you see them there in one scenario but not in another, it's just by chance they were available and should not be relied on any behavior you're trying to accomplish under that state.

                            You should make sure your scripts are ONLY trying to access data related to Instruments/Bars etc once you've reached State.DataLoaded, or possible null reference exceptions can be encountered in some scenarios but not others...
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              Jesse, Matthew & the NT Forum,

                              The issue persists. Please find an short video of what I am experiencing on this side at http://screencast.com/t/X2JJxILnZ9v.

                              Apologies for the confusion, TestStrategy showed expected behaviour yesterday... once. I have not been able to replicate expected behaviour since.

                              Any ideas are welcome.

                              Regards
                              Shannon

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Perr0Grande, Today, 08:16 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Perr0Grande  
                              Started by elderan, Today, 08:03 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post elderan
                              by elderan
                               
                              Started by algospoke, Today, 06:40 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post algospoke  
                              Started by maybeimnotrader, Today, 05:46 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post maybeimnotrader  
                              Started by quantismo, Today, 05:13 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post quantismo  
                              Working...
                              X