Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error when loading a strategy onto a chart (object reference)

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

    Error when loading a strategy onto a chart (object reference)

    Everything has been going along fine developing and testing and then all of a sudden I get this message when I try to load a strategy onto a chart:

    Strategy 'Trender': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    I've looked through the code in case maybe I made an editing error. It compiles fine. If I pull the Strategy out of the folder and put it back there is no error messages. When I open the Strategies panel it prints this.

    After the failure my strategy name is no longer in the Available list in the Strategies panel.


    Update 1:
    I have taken all the strategies out of the strategy folder so only a few stock files exist. So there is no indicator or strategy file anywhere that references this. When I start and open the Strategies panel on a chart it still displays that message. And there are strategy names in the Available list that no longer exist in the folder. I have repaired the database and reset the database but nothing is clearing it.

    Whatever is going on is stuck in the Strategies panel. It won't let go of something there.


    Update 2:
    I removed every file in NT8 that looked like it could be safely removed and recreated. Something triggered a compile of the assemblies during bootup and I got this message:

    ERROR: Unable to compile custom assembly. Original version has been restored. You need to recompile your custom NinjaScript files.

    I saw a bunch of trace errors around a 3rd party tool. It looks like something triggered a compile, something failed and something got restored. I am not seeing the original strategy issues and the Available list is now correct. So it looks like this issue got resolved but now I'm not sure about the 3rd party tool.

    I think I'll remove the assemblies and re-import that tool. Then I can put my strategy back and see if it applies to the chart.


    Update 3:
    I uninstalled NT8 and installed from scratch. The only thing I'm adding are a couple 3rd party assemblies, a minimal workspace and my strategy code. I've been writing code in this environment for a week now, so I know the 3rd party tools are not the problem. It is something in my strategy code.

    I'm getting the same message "Object reference not set to an instance of an object.". So maybe the object reference is something like this?
    in State.DataLoaded: myFastMA = ZLEMA(Sensitivity);

    I don't have much in the State.* areas, all pretty normal stuff.


    Update 4:
    I narrowed this down to a single Print statement. I was putting print statements all over to track the execution order. I had this:

    protected override void OnStateChange()
    {
    Print(string.Empty);
    Print("------------------------------------------------------------------------------------------------------------------");
    Print("Entering OnStateChange \t\tState = " + State + "\t\t\t\t" + Time[0].DayOfWeek + "\t\t" + Time[0].ToString()); // time from chart

    if (State == State.SetDefaults)
    {

    The culprit was the Print line with "Time[0].DayOfWeek". When I take this reference out it is ok.

    I looked up Trading Hours man page and now see this:
    Warning: The properties in this class should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded

    Wow! What a boondoggle this was. I burned a half day chasing this down.
    Last edited by cashinvestor; 06-27-2020, 04:07 AM.

    #2
    Hi cashinvestor, thanks for your question. Im glad you were able to get this solved.

    Next time you come across an initialization error like this, use Visual Studio debugging to attach to the NinjaTrader process. When you attach and run the problem script, Visual Studio will break at unhandled exceptions, showing the line where the exception occurred.



    Also, remember to never use any Indexed data arrays in the OnStateChanged method. If it takes an index e.g. Close[0], Time[0], it must be accessed in OnBarUpdate.

    Kind regards.
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NRITV, Today, 01:15 PM
    2 responses
    6 views
    0 likes
    Last Post NRITV
    by NRITV
     
    Started by frankthearm, Today, 09:08 AM
    7 responses
    30 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by maybeimnotrader, Yesterday, 05:46 PM
    5 responses
    25 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by quantismo, Yesterday, 05:13 PM
    2 responses
    18 views
    0 likes
    Last Post quantismo  
    Started by adeelshahzad, Today, 03:54 AM
    5 responses
    33 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Working...
    X