Lean  $LEAN_TAG$
RuntimeDaysReportElement.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.Linq;
18 using QuantConnect.Packets;
19 
21 {
22  internal class RuntimeDaysReportElement : ReportElement
23  {
24  private BacktestResult _backtest;
25  private LiveResult _live;
26 
27  /// <summary>
28  /// Create a new metric describing the number of days an algorithm ran for.
29  /// </summary>
30  /// <param name="name">Name of the widget</param>
31  /// <param name="key">Location of injection</param>
32  /// <param name="backtest">Backtest result object</param>
33  /// <param name="live">Live result object</param>
34  public RuntimeDaysReportElement(string name, string key, BacktestResult backtest, LiveResult live)
35  {
36  _backtest = backtest;
37  _live = live;
38  Name = name;
39  Key = key;
40  }
41 
42  /// <summary>
43  /// The generated output string to be injected
44  /// </summary>
45  public override string Render()
46  {
47  var result = (Result) _live ?? _backtest;
48  if (result == null)
49  {
50  return "-";
51  }
52 
53  var equityPoints = ResultsUtil.EquityPoints(result);
54  if (equityPoints.Count == 0)
55  {
56  Result = 0;
57  return "0";
58  }
59 
60  var days = (equityPoints.Last().Key - equityPoints.First().Key).Days;
61  Result = days;
62 
63  return days.ToStringInvariant();
64  }
65  }
66 }