Lean  $LEAN_TAG$
DrawdownReportElement.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.Packets;
22 using QuantConnect.Util;
23 
25 {
26  internal sealed class DrawdownReportElement : ChartReportElement
27  {
28  private LiveResult _live;
29  private BacktestResult _backtest;
30 
31  /// <summary>
32  /// Create a new plot of the top N worst drawdown durations
33  /// </summary>
34  /// <param name="name">Name of the widget</param>
35  /// <param name="key">Location of injection</param>
36  /// <param name="backtest">Backtest result object</param>
37  /// <param name="live">Live result object</param>
38  public DrawdownReportElement(string name, string key, BacktestResult backtest, LiveResult live)
39  {
40  _live = live;
41  _backtest = backtest;
42  Name = name;
43  Key = key;
44  }
45 
46  /// <summary>
47  /// Generate the top N drawdown plot using the python libraries.
48  /// </summary>
49  public override string Render()
50  {
51  var backtestPoints = ResultsUtil.EquityPoints(_backtest);
52  var livePoints = ResultsUtil.EquityPoints(_live);
53 
54  var liveSeries = new Series<DateTime, double>(livePoints.Keys, livePoints.Values);
55  var strategySeries = DrawdownCollection.NormalizeResults(_backtest, _live);
56 
57  var seriesUnderwaterPlot = DrawdownCollection.GetUnderwater(strategySeries).DropMissing();
58  var liveUnderwaterPlot = backtestPoints.Count == 0 ? seriesUnderwaterPlot : seriesUnderwaterPlot.After(backtestPoints.Last().Key);
59  var drawdownCollection = DrawdownCollection.FromResult(_backtest, _live, periods: 5);
60 
61  var base64 = "";
62  using (Py.GIL())
63  {
64  var backtestList = new PyList();
65 
66  if (liveUnderwaterPlot.IsEmpty)
67  {
68  backtestList.Append(seriesUnderwaterPlot.Keys.ToList().ToPython());
69  backtestList.Append(seriesUnderwaterPlot.Values.ToList().ToPython());
70  }
71  else
72  {
73  backtestList.Append(seriesUnderwaterPlot.Before(liveUnderwaterPlot.FirstKey()).Keys.ToList().ToPython());
74  backtestList.Append(seriesUnderwaterPlot.Before(liveUnderwaterPlot.FirstKey()).Values.ToList().ToPython());
75  }
76 
77  var liveList = new PyList();
78  liveList.Append(liveUnderwaterPlot.Keys.ToList().ToPython());
79  liveList.Append(liveUnderwaterPlot.Values.ToList().ToPython());
80 
81  var worstList = new PyList();
82  var previousDrawdownPeriods = new List<KeyValuePair<DateTime, DateTime>>();
83 
84  foreach (var group in drawdownCollection.Drawdowns)
85  {
86  // Skip drawdown periods that are overlapping
87  if (previousDrawdownPeriods.Where(kvp => (group.Start >= kvp.Key && group.Start <= kvp.Value) || (group.End >= kvp.Key && group.End <= kvp.Value)).Any())
88  {
89  continue;
90  }
91 
92  var worst = new PyDict();
93  worst.SetItem("Begin", group.Start.ToPython());
94  worst.SetItem("End", group.End.ToPython());
95  worst.SetItem("Total", group.PeakToTrough.ToPython());
96 
97  worstList.Append(worst);
98  previousDrawdownPeriods.Add(new KeyValuePair<DateTime, DateTime>(group.Start, group.End));
99  }
100 
101  base64 = Charting.GetDrawdown(backtestList, liveList, worstList);
102  }
103 
104  return base64;
105  }
106  }
107 }