17 using System.Collections.Generic;
26 internal sealed
class DrawdownReportElement : ChartReportElement
49 public override string Render()
51 var backtestPoints = ResultsUtil.EquityPoints(_backtest);
52 var livePoints = ResultsUtil.EquityPoints(_live);
54 var liveSeries =
new Series<DateTime, double>(livePoints.Keys, livePoints.Values);
55 var strategySeries = DrawdownCollection.NormalizeResults(_backtest, _live);
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);
64 var backtestList =
new PyList();
66 if (liveUnderwaterPlot.IsEmpty)
68 backtestList.Append(seriesUnderwaterPlot.Keys.ToList().ToPython());
69 backtestList.Append(seriesUnderwaterPlot.Values.ToList().ToPython());
73 backtestList.Append(seriesUnderwaterPlot.Before(liveUnderwaterPlot.FirstKey()).Keys.ToList().ToPython());
74 backtestList.Append(seriesUnderwaterPlot.Before(liveUnderwaterPlot.FirstKey()).Values.ToList().ToPython());
77 var liveList =
new PyList();
78 liveList.Append(liveUnderwaterPlot.Keys.ToList().ToPython());
79 liveList.Append(liveUnderwaterPlot.Values.ToList().ToPython());
81 var worstList =
new PyList();
82 var previousDrawdownPeriods =
new List<KeyValuePair<DateTime, DateTime>>();
84 foreach (var group
in drawdownCollection.Drawdowns)
87 if (previousDrawdownPeriods.Where(kvp => (group.Start >= kvp.Key && group.Start <= kvp.Value) || (group.End >= kvp.Key && group.End <= kvp.Value)).Any())
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());
97 worstList.Append(worst);
98 previousDrawdownPeriods.Add(
new KeyValuePair<DateTime, DateTime>(group.Start, group.End));
101 base64 = Charting.GetDrawdown(backtestList, liveList, worstList);