Lean  $LEAN_TAG$
RollingPortfolioBetaReportElement.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 Python.Runtime;
19 using QuantConnect.Packets;
20 using System.Collections.Generic;
22 
24 {
25  internal sealed class RollingPortfolioBetaReportElement : ChartReportElement
26  {
27  private LiveResult _live;
28  private BacktestResult _backtest;
29 
30  /// <summary>
31  /// The number of trading days per year to get better result of statistics
32  /// </summary>
33  private int _tradingDaysPerYear;
34 
35  /// <summary>
36  /// Create a new plot of the rolling portfolio beta to equities
37  /// </summary>
38  /// <param name="name">Name of the widget</param>
39  /// <param name="key">Location of injection</param>
40  /// <param name="backtest">Backtest result object</param>
41  /// <param name="live">Live result object</param>
42  /// <param name="tradingDaysPerYear">The number of trading days per year to get better result of statistics</param>
43  public RollingPortfolioBetaReportElement(string name, string key, BacktestResult backtest, LiveResult live, int tradingDaysPerYear)
44  {
45  _live = live;
46  _backtest = backtest;
47  Name = name;
48  Key = key;
49  _tradingDaysPerYear = tradingDaysPerYear;
50  }
51 
52  /// <summary>
53  /// Generate the rolling portfolio beta to equities plot using the python libraries.
54  /// </summary>
55  public override string Render()
56  {
57  var backtestPoints = GetReturnSeries(_backtest);
58  var backtestBenchmarkPoints = ResultsUtil.BenchmarkPoints(_backtest);
59  var livePoints = GetReturnSeries(_live);
60  var liveBenchmarkPoints = ResultsUtil.BenchmarkPoints(_live);
61 
62  var base64 = "";
63  using (Py.GIL())
64  {
65  var backtestList = new PyList();
66  var liveList = new PyList();
67 
68  var backtestRollingBetaSixMonths = Rolling.Beta(backtestPoints, backtestBenchmarkPoints, windowSize: 22 * 6);
69  var backtestRollingBetaTwelveMonths = Rolling.Beta(backtestPoints, backtestBenchmarkPoints, windowSize: _tradingDaysPerYear);
70 
71  backtestList.Append(backtestRollingBetaSixMonths.Keys.ToList().ToPython());
72  backtestList.Append(backtestRollingBetaSixMonths.Values.ToList().ToPython());
73  backtestList.Append(backtestRollingBetaTwelveMonths.Keys.ToList().ToPython());
74  backtestList.Append(backtestRollingBetaTwelveMonths.Values.ToList().ToPython());
75 
76  var liveRollingBetaSixMonths = Rolling.Beta(livePoints, liveBenchmarkPoints, windowSize: 22 * 6);
77  var liveRollingBetaTwelveMonths = Rolling.Beta(livePoints, liveBenchmarkPoints, windowSize: _tradingDaysPerYear);
78 
79  liveList.Append(liveRollingBetaSixMonths.Keys.ToList().ToPython());
80  liveList.Append(liveRollingBetaSixMonths.Values.ToList().ToPython());
81  liveList.Append(liveRollingBetaTwelveMonths.Keys.ToList().ToPython());
82  liveList.Append(liveRollingBetaTwelveMonths.Values.ToList().ToPython());
83 
84  base64 = Charting.GetRollingBeta(backtestList, liveList);
85  }
86 
87  return base64;
88  }
89 
90  private static SortedList<DateTime, double> GetReturnSeries(Result leanResult)
91  {
92  var returnSeries = ResultsUtil.EquityPoints(leanResult, BaseResultsHandler.ReturnKey);
93  if (returnSeries == null || returnSeries.Count == 0)
94  {
95  // for backwards compatibility
96  returnSeries = ResultsUtil.EquityPoints(leanResult, "Daily Performance");
97  }
98  return returnSeries;
99  }
100  }
101 }