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

Single Initialize()-like event called only once for a backtest of many products?

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

    Single Initialize()-like event called only once for a backtest of many products?

    Hello,

    I am running backtests on lots of products at once and writing the results to a database.

    I would like some C# code to execute one time per backtest. If I test 30 contracts at once, the Initialize() event of the Strategy gets called 30 times. Is there an event handler like "Initialize()" that only gets called once per backtest, as opposed to once per contract per backtest?

    Thanks in advance!

    ChiTrader2000

    #2
    Yes, that is possible. In this case I would create a base class your strategy class is inheriting from. And this base class needs to contain a static constructor, which finally is called once for all class instances before these class instances are generated. So, this static constructor would be the place to open the external database.

    Regards
    Ralph

    edit: Maybe you even don't need the base class and place the static constructor in your strategy class. But you could test what works best for you

    Comment


      #3
      Hello Ralph,

      Thanks for the novel idea. I hadn't thought about using static constructors, and will surely find this idea useful for something soon.

      I tried it, but noticed that the static constructor gets called the first time I backtest a bunch of contracts at once, but when I backtest a bunch of contracts again, it doesn't get called again.

      What I need is an event (or constructor, etc.) that gets called once per backtest. So, if I backtest 30 products 10 different times (once for each of 10 different parameter sets, such as different "Time frame: To" and "From" values and different user-changed values), I need to call some code 10 times, not 10*30=300 times, as the initializer does, and not 1 time as the static constructor does.

      Any other ideas?

      Here is the only idea I have so far:

      1. If in strategy code there exists some unique Guid per backtest run, I could call the code once in the first Initialize() event, and set a member variable like:

      _guidOfCurrentMultiProductBacktest = this.PropertyThatIsUniquePerMultiProductBacktest;

      ... then, if the Guid in an Initialize() call is still the same as my set Guid member variable "_guidOfCurrentMultiProductBacktest" I just wouldn't call my database INSERT code again (I am inserting details about each multi-product basktest's set of user-chosen parameters).

      Could maybe this approach work? I don't know if a property exists that uniquely identifies a backtest.

      Thanks in advance for any other ideas!

      ChiTrader2000

      Comment


        #4
        Hello ChiTrader2000,

        the static constructor is called once for the lifetime of the class similar like a static variable is available for the class-lifetime. If something (base class) keeps your strategy class in the active context, then this approach is of little value for you. I don't think we could obtain any information about the status of the base class which is kicking off the backtests.
        We need to find something different.

        Would be nice to detect somehow which run is the first of the series and which one is the last. But the strategy itself doesn't know anything about this. I agree with you that it is possible to identify a test series by analysing the used parameters-set. This way you still couldn't detect which one is the last one of the series, but with a detected change of the parameters-set you would know, the current one it the first one in a new series. Would this point of change be a proper one to execute the desired database access?

        Regards
        Ralph
        Last edited by Ralph; 01-24-2009, 11:02 AM.

        Comment


          #5
          >> What I need is an event (or constructor, etc.) that gets called once per backtest
          Unfortunately this is not supported.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Wilmarobyi, Today, 12:48 AM
          0 responses
          3 views
          0 likes
          Last Post Wilmarobyi  
          Started by BackToTheFutures, 08-17-2021, 03:15 PM
          8 responses
          732 views
          0 likes
          Last Post joehanus  
          Started by elirion, Yesterday, 09:32 PM
          0 responses
          8 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by cre8able, Yesterday, 09:15 PM
          1 response
          8 views
          0 likes
          Last Post bltdavid  
          Started by cummish, Yesterday, 08:43 PM
          0 responses
          16 views
          0 likes
          Last Post cummish
          by cummish
           
          Working...
          X