• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnStateChange' method

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

    Error on calling 'OnStateChange' method

    Hi,

    I built a simple strategy using the wizard but when I test the strategy, I get an error in the output saying
    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    The only code in that block was generated by the wizard. This is where the profit target is being set. Any ideas on what is causing this?

    For information, I am using Version 8.0.5.2 64-bit on Windows 10

    Code:
    else if (State == State.Configure)
    			{
    				SetProfitTarget(@"L1", CalculationMode.Price, DonchianChannel1.Mean[1]);
    				
    				SetProfitTarget(@"S1", CalculationMode.Price, DonchianChannel1.Mean[1]);
    				SetStopLoss(@"L1", CalculationMode.Price, (DonchianChannel1.Lower[1] - (ATR(14)[0])) , false);
    				SetStopLoss(@"S1", CalculationMode.Price, (DonchianChannel1.Upper[1] + (ATR(14)[0])) , false);
    			}
    When I debug it through Visual Studio, I get this message

    An exception of type 'System.NullReferenceException' occurred in NinjaTrader.Core.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.
    Can anyone help? I don't know where to start with this one

    Thanks

    #2
    Hello kdw2493,

    The error is occurring due to DonchianChannel1 (which I am assuming is assigned the indicator from a call to DonchianChannel() elsewhere in the script) in State.Configure before any bars have been processed in OnBarUpdate.

    The Strategy Builder calls SetProfitTarget() and SetStopLoss() once before the script starts with hard set values. It does not call these dynamically with new values once the strategy has started.

    (edit)
    Instead, if you would like to use the current bar's DonchianChannel values for the exit and continue using the Strategy Builder, you would need to use exit orders in the Actions section of the Conditions and Actions.
    (Like ExitLongLimit, ExitLongStopLimit, ExitLongStop)

    Alternatively, you could unlock the code and move the call for SetStopLoss/SetProfitTarget to OnBarUpdate one line before the entry order is called. (It is important to call the Set method before calling the entry.)
    Last edited by NinjaTrader_ChelseaB; 04-09-2017, 01:31 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Solved

      Hi Chelsea,

      Thanks for coming back to me. I managed to solve the problem about an hour ago by moving the instructions into the OnBarUpdate section

      Thanks a lot
      Kevin

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NinjaCustomer, Today, 12:29 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_ChrisSch  
      Started by smgrvd, Today, 12:08 AM
      0 responses
      5 views
      0 likes
      Last Post smgrvd
      by smgrvd
       
      Started by ronaldgreene828, Yesterday, 07:50 PM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_EricB  
      Started by nailz420, 07-17-2019, 08:38 AM
      2 responses
      13 views
      0 likes
      Last Post nailz420  
      Started by bouncetherubble, Yesterday, 06:56 PM
      0 responses
      6 views
      0 likes
      Last Post bouncetherubble  
      Working...
      X