Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          566 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          547 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X