Hi,
I'm trying to expose and access the default statistics generated by `PortfolioStatistics` at the end of my backtest, but I'm not finding an easy way to do so without inserting code into the library itself. I can see it's being called by `Engine.Run()` and stored in a local variable, so there's no way for me to access it from my algorithm.
Is there a recommended way to expose these statistics or, alternatively, to calculate them myself? I haven't been able to find any documentation on this. I suppose I could store the `Portfolio.TotalPortfolioValue` at each time point myself and then work back from there, but I'd like to know if there was a recommended approach.
Thanks in advance!
Douglas Stridsberg
The trick is to create a new class interfacing with IResultHandler. In particular, you want to override the SendFinalResult() method where the information you need is passed.
Example follows below, taken from inside an override of the SendFinalResult() method.
Take a look at Engine/Results/BacktestingResultHandler.cs for more information.
You'll need to shadow override a bunch of the other functions unfortunately, I used Engine/Results/BacktestingResultHandler as my shadow.
// Port to BacktestNodePacket var jobnode = job as BacktestNodePacket; // Save results var stats = statisticsResults.TotalPerformance.PortfolioStatistics; // Write to CSV (to highlight some of the various statistics that are available to you) StringBuilder builder = new StringBuilder(); builder.Append( jobnode.PeriodStart.ToUniversalTime().ToString() + "," ); builder.Append( jobnode.PeriodFinish.ToUniversalTime().ToString() + "," ); builder.Append( stats.TotalNetProfit.ToString() + "," ); builder.Append( stats.Drawdown.ToString() + "," ); builder.Append( stats.WinRate.ToString() + "," ); builder.Append( "\n" ); File.AppendAllText( @"C:\output\results.csv", builder.ToString() );
Douglas Stridsberg
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!