Lean  $LEAN_TAG$
AssetAllocationReportElement.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.Collections.Generic;
18 using System.Linq;
19 using Deedle;
20 using Python.Runtime;
21 using QuantConnect.Orders;
22 using QuantConnect.Packets;
23 
25 {
26  internal sealed class AssetAllocationReportElement : ChartReportElement
27  {
28  private BacktestResult _backtest;
29  private List<PointInTimePortfolio> _backtestPortfolios;
30  private LiveResult _live;
31  private List<PointInTimePortfolio> _livePortfolios;
32 
33  /// <summary>
34  /// Create a new plot of the asset allocation over time
35  /// </summary>
36  /// <param name="name">Name of the widget</param>
37  /// <param name="key">Location of injection</param>
38  /// <param name="backtest">Backtest result object</param>
39  /// <param name="live">Live result object</param>
40  /// <param name="backtestPortfolios">Backtest point in time portfolios</param>
41  /// <param name="livePortfolios">Live point in time portfolios</param>
42  public AssetAllocationReportElement(
43  string name,
44  string key,
45  BacktestResult backtest,
46  LiveResult live,
47  List<PointInTimePortfolio> backtestPortfolios,
48  List<PointInTimePortfolio> livePortfolios)
49  {
50  _backtest = backtest;
51  _backtestPortfolios = backtestPortfolios;
52  _live = live;
53  _livePortfolios = livePortfolios;
54  Name = name;
55  Key = key;
56  }
57 
58  /// <summary>
59  /// Generate the asset allocation pie chart using the python libraries.
60  /// </summary>
61  public override string Render()
62  {
63  var backtestSeries = Metrics.AssetAllocations(_backtestPortfolios);
64  var liveSeries = Metrics.AssetAllocations(_livePortfolios);
65 
66  PyObject result;
67 
68  using (Py.GIL())
69  {
70  var data = new PyList();
71  var liveData = new PyList();
72 
73  data.Append(backtestSeries.SortBy(x => -x).Where(x => x.Value != 0).Keys.Select(x => x.Value).ToList().ToPython());
74  data.Append(backtestSeries.SortBy(x => -x).Where(x => x.Value != 0).Values.ToList().ToPython());
75 
76  liveData.Append(liveSeries.SortBy(x => -x).Where(x => x.Value != 0).Keys.Select(x => x.Value).ToList().ToPython());
77  liveData.Append(liveSeries.SortBy(x => -x).Where(x => x.Value != 0).Values.ToList().ToPython());
78 
79  result = Charting.GetAssetAllocation(data, liveData);
80  }
81 
82  var base64 = result.ConvertToDictionary<string, string>();
83  if (base64.ContainsKey("Live Asset Allocation"))
84  {
85  return base64["Live Asset Allocation"];
86  }
87 
88  return base64["Backtest Asset Allocation"];
89  }
90  }
91 }