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

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 Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      4 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      40 views
      0 likes
      Last Post alifarahani  
      Started by Waxavi, Today, 02:10 AM
      1 response
      19 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Working...
      X