@@ -64,12 +64,14 @@ public static StatisticsResults Generate(
6464 IRiskFreeInterestRateModel riskFreeInterestRateModel ,
6565 int tradingDaysPerYear )
6666 {
67- var firstDate = equityPoints . Count > 0 ? equityPoints . First ( ) . Time . Date : default ;
68- var lastDate = equityPoints . Count > 0 ? equityPoints . Last ( ) . Time . Date : default ;
67+ var equity = ChartPointToDictionary ( equityPoints ) ;
6968
70- var totalPerformance = GetAlgorithmPerformance ( firstDate , lastDate , trades , profitLoss , equityPoints , pointsPerformance , pointsBenchmark ,
69+ var firstDate = equity . Keys . FirstOrDefault ( ) . Date ;
70+ var lastDate = equity . Keys . LastOrDefault ( ) . Date ;
71+
72+ var totalPerformance = GetAlgorithmPerformance ( firstDate , lastDate , trades , profitLoss , equity , equityPoints , pointsPerformance , pointsBenchmark ,
7173 pointsPortfolioTurnover , startingCapital , transactions , riskFreeInterestRateModel , tradingDaysPerYear ) ;
72- var rollingPerformances = GetRollingPerformances ( firstDate , lastDate , trades , profitLoss , equityPoints , pointsPerformance , pointsBenchmark ,
74+ var rollingPerformances = GetRollingPerformances ( firstDate , lastDate , trades , profitLoss , equity , equityPoints , pointsPerformance , pointsBenchmark ,
7375 pointsPortfolioTurnover , startingCapital , transactions , riskFreeInterestRateModel , tradingDaysPerYear ) ;
7476 var summary = GetSummary ( totalPerformance , estimatedStrategyCapacity , totalFees , totalOrders , accountCurrencySymbol ) ;
7577
@@ -83,7 +85,8 @@ public static StatisticsResults Generate(
8385 /// <param name="toDate">The final date of the range</param>
8486 /// <param name="trades">The list of closed trades</param>
8587 /// <param name="profitLoss">Trade record of profits and losses</param>
86- /// <param name="equityPoints">The equity curve series points</param>
88+ /// <param name="equity">The list of daily equity values</param>
89+ /// <param name="equityPoints">The equity curve OHLC series points, used for drawdown calculation</param>
8790 /// <param name="pointsPerformance">The list of algorithm performance values</param>
8891 /// <param name="pointsBenchmark">The list of benchmark values</param>
8992 /// <param name="pointsPortfolioTurnover">The list of portfolio turnover daily samples</param>
@@ -99,6 +102,7 @@ private static AlgorithmPerformance GetAlgorithmPerformance(
99102 DateTime toDate ,
100103 List < Trade > trades ,
101104 SortedDictionary < DateTime , decimal > profitLoss ,
105+ SortedDictionary < DateTime , decimal > equity ,
102106 List < ISeriesPoint > equityPoints ,
103107 List < ISeriesPoint > pointsPerformance ,
104108 List < ISeriesPoint > pointsBenchmark ,
@@ -108,13 +112,15 @@ private static AlgorithmPerformance GetAlgorithmPerformance(
108112 IRiskFreeInterestRateModel riskFreeInterestRateModel ,
109113 int tradingDaysPerYear )
110114 {
115+ var periodEquity = new SortedDictionary < DateTime , decimal > ( equity . Where ( x => x . Key . Date >= fromDate && x . Key . Date < toDate . AddDays ( 1 ) ) . ToDictionary ( x => x . Key , y => y . Value ) ) ;
111116 var periodEquityPoints = equityPoints ? . Where ( x => x . Time . Date >= fromDate && x . Time . Date < toDate . AddDays ( 1 ) ) . ToList ( ) ;
112117
113118 // No portfolio equity for the period means that there is no performance to be computed
114- if ( periodEquityPoints . IsNullOrEmpty ( ) )
119+ if ( periodEquity . IsNullOrEmpty ( ) )
115120 {
116121 return new AlgorithmPerformance ( ) ;
117122 }
123+
118124 var periodTrades = trades . Where ( x => x . ExitTime . Date >= fromDate && x . ExitTime < toDate . AddDays ( 1 ) ) . ToList ( ) ;
119125 var periodProfitLoss = new SortedDictionary < DateTime , decimal > ( profitLoss . Where ( x => x . Key >= fromDate && x . Key . Date < toDate . AddDays ( 1 ) ) . ToDictionary ( x => x . Key , y => y . Value ) ) ;
120126 var periodWinCount = transactions . WinningTransactions . Count ( x => x . Key >= fromDate && x . Key . Date < toDate . AddDays ( 1 ) ) ;
@@ -139,9 +145,9 @@ private static AlgorithmPerformance GetAlgorithmPerformance(
139145 var listBenchmark = benchmarkEnumerable . Select ( x => x . Value ) . ToList ( ) ;
140146 var listPerformance = PreprocessPerformanceValues ( performance ) . Select ( x => x . Value ) . ToList ( ) ;
141147
142- var runningCapital = equityPoints . Count == periodEquityPoints . Count ? startingCapital : Statistics . GetClose ( periodEquityPoints . First ( ) ) ;
148+ var runningCapital = equity . Count == periodEquity . Count ? startingCapital : periodEquity . Values . FirstOrDefault ( ) ;
143149
144- return new AlgorithmPerformance ( periodTrades , periodProfitLoss , periodEquityPoints , portfolioTurnover , listPerformance , listBenchmark ,
150+ return new AlgorithmPerformance ( periodTrades , periodProfitLoss , periodEquity , periodEquityPoints , portfolioTurnover , listPerformance , listBenchmark ,
145151 runningCapital , periodWinCount , periodLossCount , riskFreeInterestRateModel , tradingDaysPerYear ) ;
146152 }
147153
@@ -152,7 +158,8 @@ private static AlgorithmPerformance GetAlgorithmPerformance(
152158 /// <param name="lastDate">The last date of the total period</param>
153159 /// <param name="trades">The list of closed trades</param>
154160 /// <param name="profitLoss">Trade record of profits and losses</param>
155- /// <param name="equityPoints">The equity curve series points</param>
161+ /// <param name="equity">The list of daily equity values</param>
162+ /// <param name="equityPoints">The equity curve OHLC series points, used for drawdown calculation</param>
156163 /// <param name="pointsPerformance">The list of algorithm performance values</param>
157164 /// <param name="pointsBenchmark">The list of benchmark values</param>
158165 /// <param name="pointsPortfolioTurnover">The list of portfolio turnover daily samples</param>
@@ -168,6 +175,7 @@ private static Dictionary<string, AlgorithmPerformance> GetRollingPerformances(
168175 DateTime lastDate ,
169176 List < Trade > trades ,
170177 SortedDictionary < DateTime , decimal > profitLoss ,
178+ SortedDictionary < DateTime , decimal > equity ,
171179 List < ISeriesPoint > equityPoints ,
172180 List < ISeriesPoint > pointsPerformance ,
173181 List < ISeriesPoint > pointsBenchmark ,
@@ -187,7 +195,7 @@ private static Dictionary<string, AlgorithmPerformance> GetRollingPerformances(
187195 foreach ( var period in ranges )
188196 {
189197 var key = $ "M{ monthPeriod } _{ period . EndDate . ToStringInvariant ( "yyyyMMdd" ) } ";
190- var periodPerformance = GetAlgorithmPerformance ( period . StartDate , period . EndDate , trades , profitLoss , equityPoints , pointsPerformance ,
198+ var periodPerformance = GetAlgorithmPerformance ( period . StartDate , period . EndDate , trades , profitLoss , equity , equityPoints , pointsPerformance ,
191199 pointsBenchmark , pointsPortfolioTurnover , startingCapital , transactions , riskFreeInterestRateModel , tradingDaysPerYear ) ;
192200 rollingPerformances [ key ] = periodPerformance ;
193201 }
0 commit comments