save backtesting results

Created at 21 Jun 2018, 00:04
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
lec0456's avatar

lec0456

Joined 14.11.2012

save backtesting results
21 Jun 2018, 00:04


It would be really helpful if we could save the results of a back test.  That way we can run for one year and then pull up a saved version of the test for the previous year, for example.


@lec0456
Replies

solark
03 Jul 2018, 21:16

Would be a great built in feature. In the meantime Id recommend a simple `dump` method in an external assembly you can just place into OnStop. With the `System.IO.Compression` namepace I actually dump the full marketseries/trade history/stats/cbotset parameters file to a zip to easily load up later. Only thing is to be careful when optimizing (disable it or make sure there's enough disk space)


@solark

lec0456
03 Jul 2018, 21:38

WWWWHAT??? You are a rockstar. I need more specifics. Can you walk me through the steps.  I didn't enevn know there was a cbotset parameters file.


@lec0456

solark
03 Jul 2018, 22:35

Man... wrote a long post hit submit and just lost it all... This forum is horrible. Ill try rewritting something


@solark

solark
03 Jul 2018, 22:45

This version might be a bit quicker this time around....

 

cbotfile is just an ini file (open one in a text editor).

So for example (sorry dont use C# often) you could do

            var l = new List<string>();
            l.Add("[ChartParameters]");
            l.Add("Symbol = " + this.Symbol.Code);
            l.Add("Timeframe = " + this.TimeFrame);

            l.Add("[cBotParameters]");
            foreach (var p in this.GetType().GetProperties().Where(x => x.GetCustomAttributes(typeof(ParameterAttribute), false).Length > 0).Select(x => string.Format("{0} = {1}", x.Name, x.GetValue(this))))
            {
                l.Add(p);
            }
            System.IO.File.WriteAllLines("mycbotsetfile.cbotset", l);

 

If you're creating a Dump method then one parameter would be `this`.

 

Some useful things to log:

  • this.History, I just save to a csv
  • StartTime: r.MarketSeries.OpenTime.[0].ToString("o")
  • EndTime: r.MarketSeries.OpenTime.LastValue.ToString("o")
  • ExperimentTime. DateTime.Not.ToString("o")
  • RunTime: this would require a method call OnStart and tracking a "Dump" object (maybe not worth it). I just use System.Diagnostics.StopWatch
  • Assembly Name/Location
    • var a = System.Reflection.Assembly.GetExecutingAssembly();
      a.AssemblyName;
      a.Location;
  • Using the assembly location you should beable to copy the cs file that created it
  • Market data history: This makes things take longer and really starts to eat up space but is useful for creating your own tools to load this dump files.

 

Turns out for the stats I had to recreate them from the trade history. It's possible the GetFitness method could be used but Id imagine thats only called for genetic optimization.

 

It's convienet to save the files to an archive. You need to add references

#r "System.IO.Compression"
#r "System.IO.Compression.FileSystem

Then the easiest thing is to create a temporary directory (Path.GetTempFileName and Path.GetTempPath are useful here) then write raw files as you normaly would and call `ZipFile.CreateFromDirectory(dir,outFileName)` then clean up.

 

 

 

 


@solark

driftingprogrammer
03 Jul 2020, 16:00

RE:

solark said:

This version might be a bit quicker this time around....

 

cbotfile is just an ini file (open one in a text editor).

So for example (sorry dont use C# often) you could do

            var l = new List<string>();
            l.Add("[ChartParameters]");
            l.Add("Symbol = " + this.Symbol.Code);
            l.Add("Timeframe = " + this.TimeFrame);

            l.Add("[cBotParameters]");
            foreach (var p in this.GetType().GetProperties().Where(x => x.GetCustomAttributes(typeof(ParameterAttribute), false).Length > 0).Select(x => string.Format("{0} = {1}", x.Name, x.GetValue(this))))
            {
                l.Add(p);
            }
            System.IO.File.WriteAllLines("mycbotsetfile.cbotset", l);

 

If you're creating a Dump method then one parameter would be `this`.

 

Some useful things to log:

  • this.History, I just save to a csv
  • StartTime: r.MarketSeries.OpenTime.[0].ToString("o")
  • EndTime: r.MarketSeries.OpenTime.LastValue.ToString("o")
  • ExperimentTime. DateTime.Not.ToString("o")
  • RunTime: this would require a method call OnStart and tracking a "Dump" object (maybe not worth it). I just use System.Diagnostics.StopWatch
  • Assembly Name/Location
    • var a = System.Reflection.Assembly.GetExecutingAssembly();
      a.AssemblyName;
      a.Location;
  • Using the assembly location you should beable to copy the cs file that created it
  • Market data history: This makes things take longer and really starts to eat up space but is useful for creating your own tools to load this dump files.

 

Turns out for the stats I had to recreate them from the trade history. It's possible the GetFitness method could be used but Id imagine thats only called for genetic optimization.

 

It's convienet to save the files to an archive. You need to add references

#r "System.IO.Compression"
#r "System.IO.Compression.FileSystem

Then the easiest thing is to create a temporary directory (Path.GetTempFileName and Path.GetTempPath are useful here) then write raw files as you normaly would and call `ZipFile.CreateFromDirectory(dir,outFileName)` then clean up.

 

 

 

 

Wow this is so good. Do you also know of a way of automating the running of backtest. Say I have a folder with 100 parmeter files and I want to backtest all of them for a particular time frame, i there any tool that can help me do that quickly instead of me sitting and manually backtesting each parameter one by one.

Thanks in advance.


@driftingprogrammer