Lean  $LEAN_TAG$
ResultsUtil.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;
19 
20 namespace QuantConnect.Report
21 {
22  /// <summary>
23  /// Utility methods for dealing with the <see cref="Result"/> objects
24  /// </summary>
25  public static class ResultsUtil
26  {
27  /// <summary>
28  /// Get the points, from the Series name given, in Strategy Equity chart
29  /// </summary>
30  /// <param name="result">Result object to extract the chart points</param>
31  /// <param name="seriesName">Series name from which the points will be extracted. By default is Equity series</param>
32  /// <returns></returns>
33  public static SortedList<DateTime, double> EquityPoints(Result result, string seriesName = null)
34  {
35  var points = new SortedList<DateTime, double>();
36 
37  seriesName ??= BaseResultsHandler.EquityKey;
38  if (result == null || result.Charts == null ||
39  !result.Charts.ContainsKey(BaseResultsHandler.StrategyEquityKey) ||
40  result.Charts[BaseResultsHandler.StrategyEquityKey].Series == null ||
41  !result.Charts[BaseResultsHandler.StrategyEquityKey].Series.ContainsKey(seriesName))
42  {
43  return points;
44  }
45 
46  var series = result.Charts[BaseResultsHandler.StrategyEquityKey].Series[seriesName];
47  switch (series)
48  {
49  case Series s:
50  foreach (ChartPoint point in s.Values)
51  {
52  points[point.Time] = Convert.ToDouble(point.y);
53  }
54  break;
55 
56  case CandlestickSeries candlestickSeries:
57  foreach (Candlestick candlestick in candlestickSeries.Values)
58  {
59  points[candlestick.Time] = Convert.ToDouble(candlestick.Close);
60  }
61  break;
62  }
63 
64  return points;
65  }
66 
67  /// <summary>
68  /// Gets the points of the benchmark
69  /// </summary>
70  /// <param name="result">Backtesting or live results</param>
71  /// <returns>Sorted list keyed by date and value</returns>
72  public static SortedList<DateTime, double> BenchmarkPoints(Result result)
73  {
74  var points = new SortedList<DateTime, double>();
75 
76  if (result == null || result.Charts == null ||
77  !result.Charts.ContainsKey(BaseResultsHandler.BenchmarkKey) ||
78  result.Charts[BaseResultsHandler.BenchmarkKey].Series == null ||
80  {
81  return points;
82  }
83 
84  if (!result.Charts.ContainsKey(BaseResultsHandler.BenchmarkKey))
85  {
86  return new SortedList<DateTime, double>();
87  }
88  if (!result.Charts[BaseResultsHandler.BenchmarkKey].Series.ContainsKey(BaseResultsHandler.BenchmarkKey))
89  {
90  return new SortedList<DateTime, double>();
91  }
92 
93  // Benchmark should be a Series, so we cast the points directly to ChartPoint
94  foreach (ChartPoint point in result.Charts[BaseResultsHandler.BenchmarkKey].Series[BaseResultsHandler.BenchmarkKey].Values)
95  {
96  points[Time.UnixTimeStampToDateTime(point.x)] = Convert.ToDouble(point.y);
97  }
98 
99  return points;
100  }
101  }
102 }