Lean  $LEAN_TAG$
SharpeRatioReportElement.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using Deedle;
17 using QuantConnect.Packets;
18 using System;
19 using System.Collections.Generic;
20 using System.Globalization;
21 using System.Linq;
22 
24 {
25  /// <summary>
26  /// Class for render the Sharpe Ratio statistic for a report
27  /// </summary>
29  {
30  /// <summary>
31  /// The number of trading days per year to get better result of statistics
32  /// </summary>
33  private double _tradingDaysPerYear;
34 
35  /// <summary>
36  /// Live result object
37  /// </summary>
38  protected LiveResult LiveResult { get; }
39 
40  /// <summary>
41  /// Backtest result object
42  /// </summary>
43  protected BacktestResult BacktestResult { get; }
44 
45  /// <summary>
46  /// Sharpe Ratio from a backtest
47  /// </summary>
48  public virtual decimal? BacktestResultValue => BacktestResult?.TotalPerformance?.PortfolioStatistics?.SharpeRatio;
49 
50  /// <summary>
51  /// Estimate the sharpe ratio of the strategy.
52  /// </summary>
53  /// <param name="name">Name of the widget</param>
54  /// <param name="key">Location of injection</param>
55  /// <param name="backtest">Backtest result object</param>
56  /// <param name="live">Live result object</param>
57  /// <param name="tradingDaysPerYear">The number of trading days per year to get better result of statistics</param>
58  public SharpeRatioReportElement(string name, string key, BacktestResult backtest, LiveResult live, int tradingDaysPerYear)
59  {
60  LiveResult = live;
61  BacktestResult = backtest;
62  Name = name;
63  Key = key;
64  _tradingDaysPerYear = Convert.ToDouble(tradingDaysPerYear, CultureInfo.InvariantCulture);
65  }
66 
67  /// <summary>
68  /// The generated output string to be injected
69  /// </summary>
70  public override string Render()
71  {
72  if (LiveResult == null)
73  {
75  return BacktestResultValue?.ToString("F1") ?? "-";
76  }
77 
78  var equityPoints = ResultsUtil.EquityPoints(LiveResult);
79  var performance = DeedleUtil.PercentChange(new Series<DateTime, double>(equityPoints).ResampleEquivalence(date => date.Date, s => s.LastValue()));
80  if (performance.ValueCount == 0)
81  {
82  return "-";
83  }
84 
85  var sixMonthsAgo = performance.LastKey().AddDays(-180);
86  var trailingPerformance = performance.Where(series => series.Key >= sixMonthsAgo && series.Key.DayOfWeek != DayOfWeek.Saturday && series.Key.DayOfWeek != DayOfWeek.Sunday)
87  .Values
88  .ToList();
89 
90  var annualStandardDeviation = trailingPerformance.Count < 7 ? 0 : GetAnnualStandardDeviation(trailingPerformance, _tradingDaysPerYear);
91  if (annualStandardDeviation <= 0)
92  {
93  return "-";
94  }
95 
96  var annualPerformance = Statistics.Statistics.AnnualPerformance(trailingPerformance, _tradingDaysPerYear);
97  var liveResultValue = Statistics.Statistics.SharpeRatio(annualPerformance, annualStandardDeviation, 0.0);
98  Result = liveResultValue;
99  return liveResultValue.ToString("F2");
100  }
101 
102  /// <summary>
103  /// Get annual standard deviation
104  /// </summary>
105  /// <param name="trailingPerformance">The performance for the last period</param>
106  /// <param name="tradingDaysPerYear">The number of trading days per year to get better result of statistics</param>
107  /// <returns>Annual standard deviation.</returns>
108  public virtual double GetAnnualStandardDeviation(List<double> trailingPerformance, double tradingDaysPerYear)
109  {
110  return Statistics.Statistics.AnnualStandardDeviation(trailingPerformance, tradingDaysPerYear);
111  }
112  }
113 }