Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to re-instantiate NT indicator for reuse with Optimization

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

    Unable to re-instantiate NT indicator for reuse with Optimization

    I am trying to increase speed of Optimization of my strategy. I already have set IsInstantiatedOnEachOptimizationIteration=false and I reset all my class variables inside State.DataLoaded .My strategy uses a custom indicator I have built. This custom indicator instantiates other native indicators that NT has (e.g. SMA, EMA, Swing, etc...).

    To improve speed, I am trying to avoid re-instantiation my custom indicator. I built a method inside my custom indicator called ResetMyIndicator() which my strategy calls during State.DataLoaded​. Inside the ResetMyIndicator()​ I reset various variables and I also instantiate that native indicators. However, when I run my strategy in Optimization, I get the following error:

    Code:
    Strategy 'MyStragetyName': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    Using the Visual Studio debugger, I was able to find the error is coming from the Swing.cs indicator on line 409 (bold text in excerpt of the Swing.cs code).

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    public Swing Swing(ISeries<double> input, int strength)
    {
    if (cacheSwing != null)
    for (int idx = 0; idx < cacheSwing.Length; idx++)
    if (cacheSwing[idx] != null && cacheSwing[idx].Strength == strength && cacheSwing[idx].EqualsInput(input))
    return cacheSwing[idx];
    return CacheIndicator<Swing>(new Swing(){ Strength = strength }, input, ref cacheSwing);
    }​
    The above error happens when I do the following inside my ResetMyIndicator()​ method of custom indicator and when my custom strategy is object is reused during Optimization.

    Code:
    public void ResetMyIndicator(){
    
    mySwing = Swing(strength: 10);
    // other variable resets not shown
    }

    #2
    Hello wzgy0920,

    The error you are seeing means the an object was null when you tried to use it, if that is happening from the strategy you would need to check the strategies code and not use the visual studio debugger to try and peer into other indicator instances. The visual studio debugger is not something we can assist with for this use case but we can suggest using prints for this type of error. In your strategy you can add prints to find which specific line is called when the error happens, based on which line is having an error we could try to provide an alternate way of doing that task.

    Keep in mind that IsInstantiatedOnEachOptimizationIteration is only for your strategies class level variables, this does not affect indicators being used. To instantiate an indicator the indicator needs to be a class level variable and the strategy then calls the indicator in State.DataLoaded to get an instance. If nothing changed with the indicators properties then a cached instance would be used, calling a reset inside the indicator will likely prevent it from working correctly.

    The strategy code would look like the following to use IsInstantiatedOnEachOptimizationIteration with any indicator.
    Code:
    private SMA mySMAIndicator;
    
    protected override void OnStateChange()
    {
       if (State == State.SetDefaults)
       {
           IsInstantiatedOnEachOptimizationIteration = false;
       }
       else if (State == State.DataLoaded)
       {
           mySMAIndicator = SMA(14);
        }
    }

    Comment


      #3
      Hi Jesse,

      I have tried all troubleshooting but still can't figure out what is causing the issue. I am sent a very simplified example code via email. Can you please look at that?

      Comment


        #4
        Hello wzgy0920,

        I had replied to your email, in the future please use only one means of communication so we can keep all the details in one place. You can attach scripts to the forum for future reference.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        50 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X