Lean  $LEAN_TAG$
RollingSharpeReportElement.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 System;
17 using System.Linq;
18 using Deedle;
19 using Python.Runtime;
20 using QuantConnect.Packets;
21 
23 {
24  internal sealed class RollingSharpeReportElement : ChartReportElement
25  {
26  private LiveResult _live;
27  private BacktestResult _backtest;
28 
29  /// <summary>
30  /// The number of trading days per year to get better result of statistics
31  /// </summary>
32  private int _tradingDaysPerYear;
33 
34  /// <summary>
35  /// Create a new plot of the rolling sharpe ratio
36  /// </summary>
37  /// <param name="name">Name of the widget</param>
38  /// <param name="key">Location of injection</param>
39  /// <param name="backtest">Backtest result object</param>
40  /// <param name="live">Live result object</param>
41  /// <param name="tradingDaysPerYear">The number of trading days per year to get better result of statistics</param>
42  public RollingSharpeReportElement(string name, string key, BacktestResult backtest, LiveResult live, int tradingDaysPerYear)
43  {
44  _live = live;
45  _backtest = backtest;
46  Name = name;
47  Key = key;
48  _tradingDaysPerYear = tradingDaysPerYear;
49  }
50 
51  /// <summary>
52  /// Generate the rolling sharpe using the python libraries.
53  /// </summary>
54  public override string Render()
55  {
56  var backtestPoints = ResultsUtil.EquityPoints(_backtest);
57  var livePoints = ResultsUtil.EquityPoints(_live);
58 
59  var backtestSeries = new Series<DateTime, double>(backtestPoints);
60  var liveSeries = new Series<DateTime, double>(livePoints);
61 
62  var backtestRollingSharpeSixMonths = Rolling.Sharpe(backtestSeries, 6, _tradingDaysPerYear).DropMissing();
63  var backtestRollingSharpeTwelveMonths = Rolling.Sharpe(backtestSeries, 12, _tradingDaysPerYear).DropMissing();
64  var liveRollingSharpeSixMonths = Rolling.Sharpe(liveSeries, 6, _tradingDaysPerYear).DropMissing();
65  var liveRollingSharpeTwelveMonths = Rolling.Sharpe(liveSeries, 12, _tradingDaysPerYear).DropMissing();
66 
67  var base64 = "";
68  using (Py.GIL())
69  {
70  var backtestList = new PyList();
71  var liveList = new PyList();
72 
73  backtestList.Append(backtestRollingSharpeSixMonths.Keys.ToList().ToPython());
74  backtestList.Append(backtestRollingSharpeSixMonths.Values.ToList().ToPython());
75  backtestList.Append(backtestRollingSharpeTwelveMonths.Keys.ToList().ToPython());
76  backtestList.Append(backtestRollingSharpeTwelveMonths.Values.ToList().ToPython());
77 
78  liveList.Append(liveRollingSharpeSixMonths.Keys.ToList().ToPython());
79  liveList.Append(liveRollingSharpeSixMonths.Values.ToList().ToPython());
80  liveList.Append(liveRollingSharpeTwelveMonths.Keys.ToList().ToPython());
81  liveList.Append(liveRollingSharpeTwelveMonths.Values.ToList().ToPython());
82 
83  base64 = Charting.GetRollingSharpeRatio(backtestList, liveList);
84  }
85 
86  return base64;
87  }
88  }
89 }