17 using System.Collections.Generic;
25 internal sealed
class MonthlyReturnsReportElement : ChartReportElement
48 public override string Render()
50 var backtestPoints = ResultsUtil.EquityPoints(_backtest);
51 var livePoints = ResultsUtil.EquityPoints(_live);
53 var backtestSeries =
new Series<DateTime, double>(backtestPoints.Keys, backtestPoints.Values);
54 var liveSeries =
new Series<DateTime, double>(livePoints.Keys, livePoints.Values);
57 var backtestMonthlyReturns = backtestSeries.ResampleEquivalence(date =>
new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1))
58 .Select(kvp => kvp.Value.TotalReturns());
60 var liveMonthlyReturns = liveSeries.ResampleEquivalence(date =>
new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1))
61 .Select(kvp => kvp.Value.TotalReturns());
66 var backtestResults =
new PyDict();
67 foreach (var kvp
in backtestMonthlyReturns.GroupBy(kvp => kvp.Key.Year).GetObservations())
69 var key = kvp.Key.ToStringInvariant();
70 var monthlyReturns = kvp.Value * 100;
72 var values =
new List<double>();
73 for (var i = 1; i <= 12; i++)
75 var returns = monthlyReturns.Where(row => row.Key.Month == i);
78 values.Add(returns.FirstValue());
82 values.Add(
double.NaN);
85 backtestResults.SetItem(key.ToPython(), values.ToPython());
88 var liveResults =
new PyDict();
89 foreach (var kvp
in liveMonthlyReturns.GroupBy(kvp => kvp.Key.Year).GetObservations())
91 var key = kvp.Key.ToStringInvariant();
92 var monthlyReturns = kvp.Value * 100;
94 var values =
new List<double>();
95 for (var i = 1; i <= 12; i++)
97 var returns = monthlyReturns.Where(row => row.Key.Month == i);
100 values.Add(returns.FirstValue());
104 values.Add(
double.NaN);
107 liveResults.SetItem(key.ToPython(), values.ToPython());
110 base64 = Charting.GetMonthlyReturns(backtestResults, liveResults);