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

Instantiated on each optimization false problem

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

    Instantiated on each optimization false problem

    Hi!
    In the past I used IsInstantiatedOnEachOptimizationIteration = false with several strategies where I reset all variables in State.DataLoaded. It always worked great. In these strategies I had the full logic in the code of the strategy with no custom indicator, so all variables created were in the strategy which I could reset.

    Now, I want to use a custom indicator that I created and use it as buy and sell signal in a strategy: Basically if CustomIndicator.BoolValue[0] == true, then buy and if CustomIndicator.BoolValue[0] == false then sell. Everything works perfect with optimization if IsInstantiatedOnEachOptimizationIteration is set to true. However, it doesn't work if I set it to false. I guess, the problem is that with the custom indicator the full logic is not in the strategy code but in the indicator code.

    I tried to reset all variables within the indicator(!) code in State.DataLoaded. But it still doesn't work. I can't reset the variables in the strategy code as these variables only exist within the indicator code. The error is "accessing an index with a value out of range".

    How can I use IsInstantiatedOnEachOptimizationIteration = false with a custom indicator that has variables? Is this even possible?

    Thanks in advance!!

    #2
    Hi, thanks for your post.

    In the case of a series object, instantiating on each optimization will create a new custom Series object so with this set to false the values of the previous series are still within the array when the next optimization iteration occurs. Either you would just keep the initialization set to true or revert your series object.

    e.g.
    Guest:
    Code:
        
    public class TestGuest : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    ...
    
                }
                else if (State == State.Configure)
                {    
                    _boolList = new Series<bool>(this, MaximumBarsLookBack.Infinite);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(CurrentBar % 2 == 0)
                {
                    _boolList[0] = true; 
                }
                else
                {
                    _boolList[0] = false;
                }
    
            }
    
    
            public Series<bool> _boolList
            { get; set; }
        }
    Host:

    Code:
    public class TestHost : Indicator
        {
            private TestGuest myGuest;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    ...
    
                }
                else if (State == State.Configure)
                {
                    myGuest = TestGuest();
                }
            }
    
            protected override void OnBarUpdate()
            {
                myGuest.Update();
                Print(myGuest._boolList[0]);
            }
    Please let me know if I can assist any further.

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the answer Chris! I want to use it when set to false to make faster optimizations.

      I just didn't understand your answer. I played around and also recreated your example but still couldn't figure it out. I understand that I can create another object myGuest of the type TestGuest in the TestHost Script and then access myGuest_boolList. But how do I reset myGuest._boolList series on each optimization iteration within the TestHost script?

      Why do you need the myGuest.Update() in your example and why do you call it during onbarupdate? I guess it has to do with resetting myGuest. However, I thought a reset of an object should be made during State.DataLoaded?
      I read the Update() section in the help guide but I still don't understand exactly what it does in your example code.

      While trying to solve this problem i came up with another one:

      I am getting strange results when calling Print() on each bar (OnBarupdate) when optimizing with the strategy analyzer with your TestGuest and TestHost (which is a strategy in my case). I just added 1 property in TestHost to be able to optimize . I just have one Print() statement containing Time[0] and myGuest._boolList[0] and i am getting the following results:

      17.01.2011 00:00:00; False
      17.01.2011 01:00:00; False
      17.01.2011 02:00:00; True
      17.01.2011 03:00:00; False
      17.01.2011 04:00:00; True
      17.01.2011 05:00:00; False
      17.01.2011 06:00:00; True
      17.01.2011 07:00:00; False
      17.01.2011 08:00:00; True
      17.01.2011 09:00:00; False
      17.01.2011 10:00:00; True
      17.01.2011 11:00:00; False
      17.01.2011 12:00:00; True
      17.01.2011 13:00:00; False
      17.01.2011 14:00:00; True
      17.01.2011 15:00:00; False
      17.01.2011 16:00:00; True
      17.01.2011 17:00:00; False
      17.01.2011 18:00:00; True
      17.01.2011 19:00:00; False
      17.01.2011 20:00:00; True
      17.01.2011 21:00:00; False
      17.01.2011 22:00:00; True
      17.01.2011 23:00:00; False
      18.01.2011 00:00:00; True
      18.01.2011 01:00:00; False
      18.01.2011 02:00:00; True
      18.01.2011 03:00:00; False
      18.01.2011 04:00:00; True
      18.01.2011 05:00:00; False
      18.01.2011 06:00:00; True
      18.01.2011 07:00:00; False
      18.01.2011 08:00:00; True
      18.01.2011 09:00:00; False
      18.01.2011 10:00:00; True
      18.01.2011 11:00:00; False
      18.01.2011 12:00:00; True
      18.01.2011 13:00:00; False
      18.01.2011 14:00:00; True
      18.01.2011 15:00:00; False
      18.01.2011 16:00:00; True
      18.01.2011 17:00:00; False
      18.01.2011 18:00:00; True
      18.01.2011 19:00:00; False
      18.01.2011 20:00:00; True
      18.01.2011 21:00:00; False
      18.01.2011 22:00:00; True
      18.01.2011 23:00:00; False
      19.01.2011 00:00:00; True
      19.01.2011 01:00:00; False
      19.01.2011 02:00:00; True
      19.01.2011 03:00:00; False
      19.01.2011 04:00:00; True
      19.01.2011 05:00:00; False
      19.01.2011 06:00:00; True
      19.01.2011 07:00:00; False
      19.01.2011 08:00:00; True
      19.01.2011 09:00:00; False
      19.01.2011 10:00:00; True
      19.01.2011 11:00:00; False
      19.01.2011 12:00:00; True
      19.01.2011 13:00:00; False
      19.01.2011 14:00:00; True
      19.01.2011 15:00:00; False
      19.01.2011 16:00:00; True
      19.01.2011 17:00:00; False
      17.01.2011 00:00:00; False
      19.01.2011 18:00:00; True
      19.01.2011 19:00:00; False
      19.01.2011 20:00:00; True
      17.01.2011 01:00:00; False
      19.01.2011 21:00:00; False
      19.01.2011 22:00:00; True
      17.01.2011 02:00:00; False
      19.01.2011 23:00:00; False
      17.01.2011 03:00:00; False
      20.01.2011 00:00:00; True
      20.01.2011 01:00:00; False
      17.01.2011 04:00:00; True
      20.01.2011 02:00:00; True
      17.01.2011 05:00:00; False
      20.01.2011 03:00:00; False
      20.01.2011 04:00:00; True
      17.01.2011 06:00:00; True
      ...

      So basically, after 19.01.2011 it additionally prints all values from the beginning starting from 17.01.2011. How does the Print() statement work in OnBarUpdate when optimizing?

      Thanks for your help!

      Comment


        #4
        Hi Kirk, thanks for your reply.

        On this page we have an example that goes through re-initializing variables like indicators, Series<T> objects, etc:



        The Strategy Analyzer is simply running your code with the given inputs over the given time frame so the Print() statement will still work as expected. The Update() method forces the OnBarUpdate method to be called for the indicator so it's guaranteed you will get up to date info on the extra series.

        If you re-instantiate the indicator the lists, series within that indicator will also be reset. My example can also be applied to a strategy and tested through the analyzer.

        Kind regards.


        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much, your example worked out for me now. There must be something else in my indicator code that doesn't work properly, I will need to check it.

          What I am still a bit confused is how the optimizer makes the calculation per iteration. Why does it calculate the second iteration after 66 bars in my example above (see 17.01.2011 00:00:00; False coming after 19.01.2011 17:00:00; False) although the first iteration is not finished calculating? I always thought it caluclates one iteration from start to end date and then goes to the second iteration and so on. But this seems not to be the case. Can you briefly explain how the calculation per iteration works? I couldn't find anything in the help guide.
          Last edited by KirkHammett; 04-14-2020, 02:04 PM.

          Comment


            #6
            Hello Kirk,

            Optimization backtests will take advantage of a multi core CPU. Multiple backtests are all run at once and are not performed in a consecutive order. This can make following prints in an optimization confusing since multiple instances of the strategy are being run at once. (I would recommend using prints with standard backtests and to leave them out of optimizations.)

            We look forward to assisting.
            JimNinjaTrader Customer Service

            Comment


              #7
              Ok, so the Print statement really doesn't make any sense while optimizing. Thanks a lot for your help!

              Comment


                #8
                Originally posted by KirkHammett View Post
                Thank you very much, your example worked out for me now. There must be something else in my indicator code that doesn't work properly, I will need to check it.

                What I am still a bit confused is how the optimizer makes the calculation per iteration. Why does it calculate the second iteration after 66 bars in my example above (see 17.01.2011 00:00:00; False coming after 19.01.2011 17:00:00; False) although the first iteration is not finished calculating? I always thought it caluclates one iteration from start to end date and then goes to the second iteration and so on. But this seems not to be the case. Can you briefly explain how the calculation per iteration works? I couldn't find anything in the help guide.
                I think the issue you're facing is related to a bug in NT. Please see my post here: https://forum.ninjatrader.com/forum/...18#post1271518

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by yertle, Yesterday, 08:38 AM
                7 responses
                28 views
                0 likes
                Last Post yertle
                by yertle
                 
                Started by bmartz, 03-12-2024, 06:12 AM
                2 responses
                21 views
                0 likes
                Last Post bmartz
                by bmartz
                 
                Started by funk10101, Today, 12:02 AM
                0 responses
                4 views
                0 likes
                Last Post funk10101  
                Started by gravdigaz6, Yesterday, 11:40 PM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by MarianApalaghiei, Yesterday, 10:49 PM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X