Lean  $LEAN_TAG$
ExposureReportElement.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 ExposureReportElement : ChartReportElement
27  {
28  private LiveResult _live;
29  private BacktestResult _backtest;
30  private List<PointInTimePortfolio> _backtestPortfolios;
31  private List<PointInTimePortfolio> _livePortfolios;
32 
33  /// <summary>
34  /// Create a new plot of the exposure
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 ExposureReportElement(
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 exposure plot using the python libraries.
60  /// </summary>
61  public override string Render()
62  {
63  var longBacktestFrame = Metrics.Exposure(_backtestPortfolios, OrderDirection.Buy);
64  var shortBacktestFrame = Metrics.Exposure(_backtestPortfolios, OrderDirection.Sell);
65  var longLiveFrame = Metrics.Exposure(_livePortfolios, OrderDirection.Buy);
66  var shortLiveFrame = Metrics.Exposure(_livePortfolios, OrderDirection.Sell);
67 
68  var backtestFrame = longBacktestFrame.Join(shortBacktestFrame)
69  .FillMissing(Direction.Forward)
70  .FillMissing(0.0);
71 
72  var liveFrame = longLiveFrame.Join(shortLiveFrame)
73  .FillMissing(Direction.Forward)
74  .FillMissing(0.0);
75 
76  longBacktestFrame = Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
77  shortBacktestFrame = Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
78  longLiveFrame = Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
79  shortLiveFrame = Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
80 
81  foreach (var key in backtestFrame.ColumnKeys)
82  {
83  longBacktestFrame[key] = backtestFrame[key].SelectValues(x => x < 0 ? 0 : x);
84  shortBacktestFrame[key] = backtestFrame[key].SelectValues(x => x > 0 ? 0 : x);
85  }
86 
87  foreach (var key in liveFrame.ColumnKeys)
88  {
89  longLiveFrame[key] = liveFrame[key].SelectValues(x => x < 0 ? 0 : x);
90  shortLiveFrame[key] = liveFrame[key].SelectValues(x => x > 0 ? 0 : x);
91  }
92 
93  longBacktestFrame = longBacktestFrame.DropSparseColumnsAll();
94  shortBacktestFrame = shortBacktestFrame.DropSparseColumnsAll();
95  longLiveFrame = longLiveFrame.DropSparseColumnsAll();
96  shortLiveFrame = shortLiveFrame.DropSparseColumnsAll();
97 
98  var base64 = "";
99  using (Py.GIL())
100  {
101  var time = backtestFrame.RowKeys.ToList().ToPython();
102  var longSecurities = longBacktestFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
103  var shortSecurities = shortBacktestFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
104  var longData = longBacktestFrame.ColumnKeys.Select(x => longBacktestFrame[x].Values.ToList().ToPython()).ToPython();
105  var shortData = shortBacktestFrame.ColumnKeys.Select(x => shortBacktestFrame[x].Values.ToList().ToPython()).ToPython();
106  var liveTime = liveFrame.RowKeys.ToList().ToPython();
107  var liveLongSecurities = longLiveFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
108  var liveShortSecurities = shortLiveFrame.ColumnKeys.Select(x => x.Item1.ToStringInvariant()).ToList().ToPython();
109  var liveLongData = longLiveFrame.ColumnKeys.Select(x => longLiveFrame[x].Values.ToList().ToPython()).ToPython();
110  var liveShortData = shortLiveFrame.ColumnKeys.Select(x => shortLiveFrame[x].Values.ToList().ToPython()).ToPython();
111 
112  base64 = Charting.GetExposure(
113  time,
114  longSecurities,
115  shortSecurities,
116  longData,
117  shortData,
118  liveTime,
119  liveLongSecurities,
120  liveShortSecurities,
121  liveLongData,
122  liveShortData
123  );
124  }
125 
126  return base64;
127  }
128  }
129 }