22 using System.Collections.Generic;
26 internal sealed
class CumulativeReturnsReportElement : ChartReportElement
50 public override string Render()
52 var backtestReturns = ResultsUtil.EquityPoints(_backtest);
53 var benchmark = ResultsUtil.BenchmarkPoints(_backtest);
54 var liveReturns = ResultsUtil.EquityPoints(_live);
55 var liveBenchmark = ResultsUtil.BenchmarkPoints(_live);
57 var backtestTime = backtestReturns.Keys.ToList();
58 var backtestStrategy = backtestReturns.Values.ToList();
59 var benchmarkTime = benchmark.Keys.ToList();
60 var benchmarkPoints = benchmark.Values.ToList();
62 var liveTime = liveReturns.Keys.ToList();
63 var liveStrategy = liveReturns.Values.ToList();
64 var liveBenchmarkTime = liveBenchmark.Keys.ToList();
65 var liveBenchmarkStrategy = liveBenchmark.Values.ToList();
70 var backtestList =
new PyList();
71 var liveList =
new PyList();
73 var backtestSeries =
new Series<DateTime, double>(backtestTime, backtestStrategy);
74 var liveSeries =
new Series<DateTime, double>(liveTime, liveStrategy);
75 var backtestBenchmarkSeries =
new Series<DateTime, double>(benchmarkTime, benchmarkPoints);
76 var liveBenchmarkSeries =
new Series<DateTime, double>(liveBenchmarkTime, liveBenchmarkStrategy);
91 var backtestLastValue = backtestSeries.ValueCount == 0 ? 0 : backtestSeries.LastValue();
92 var backtestBenchmarkLastValue = backtestBenchmarkSeries.ValueCount == 0 ? 0 : backtestBenchmarkSeries.LastValue();
94 var liveContinuousEquity = liveSeries;
95 var liveBenchContinuousEquity = liveBenchmarkSeries;
97 if (liveSeries.ValueCount != 0)
99 liveContinuousEquity = (liveSeries * (backtestLastValue / liveSeries.FirstValue()))
103 if (liveBenchmarkSeries.ValueCount != 0)
105 liveBenchContinuousEquity = (liveBenchmarkSeries * (backtestBenchmarkLastValue / liveBenchmarkSeries.FirstValue()))
110 var liveStart = liveContinuousEquity.ValueCount == 0 ? DateTime.MaxValue : liveContinuousEquity.DropMissing().FirstKey();
111 var liveBenchStart = liveBenchContinuousEquity.ValueCount == 0 ? DateTime.MaxValue : liveBenchContinuousEquity.DropMissing().FirstKey();
113 var finalEquity = backtestSeries.Where(kvp => kvp.Key < liveStart).Observations.ToList();
114 var finalBenchEquity = backtestBenchmarkSeries.Where(kvp => kvp.Key < liveBenchStart).Observations.ToList();
116 finalEquity.AddRange(liveContinuousEquity.Observations);
117 finalBenchEquity.AddRange(liveBenchContinuousEquity.Observations);
119 var finalSeries = (
new Series<DateTime, double>(finalEquity).CumulativeReturns() * 100)
123 var finalBenchSeries = (
new Series<DateTime, double>(finalBenchEquity).CumulativeReturns() * 100)
127 var backtestCumulativePercent = finalSeries.Where(kvp => kvp.Key < liveStart);
128 var backtestBenchmarkCumulativePercent = finalBenchSeries.Where(kvp => kvp.Key < liveBenchStart);
130 var liveCumulativePercent = finalSeries.Where(kvp => kvp.Key >= liveStart);
131 var liveBenchmarkCumulativePercent = finalBenchSeries.Where(kvp => kvp.Key >= liveBenchStart);
133 backtestList.Append(backtestCumulativePercent.Keys.ToList().ToPython());
134 backtestList.Append(backtestCumulativePercent.Values.ToList().ToPython());
135 backtestList.Append(backtestBenchmarkCumulativePercent.Keys.ToList().ToPython());
136 backtestList.Append(backtestBenchmarkCumulativePercent.Values.ToList().ToPython());
138 liveList.Append(liveCumulativePercent.Keys.ToList().ToPython());
139 liveList.Append(liveCumulativePercent.Values.ToList().ToPython());
140 liveList.Append(liveBenchmarkCumulativePercent.Keys.ToList().ToPython());
141 liveList.Append(liveBenchmarkCumulativePercent.Values.ToList().ToPython());
143 base64 = Charting.GetCumulativeReturns(backtestList, liveList);