Lean  $LEAN_TAG$
TradesPerDayReportElement.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.Collections.Generic;
17 using System.Linq;
18 using QuantConnect.Orders;
19 using QuantConnect.Packets;
20 
22 {
23  internal sealed class TradesPerDayReportElement : ReportElement
24  {
25  private LiveResult _live;
26  private BacktestResult _backtest;
27 
28  /// <summary>
29  /// Estimate the trades per day of the strategy.
30  /// </summary>
31  /// <param name="name">Name of the widget</param>
32  /// <param name="key">Location of injection</param>
33  /// <param name="backtest">Backtest result object</param>
34  /// <param name="live">Live result object</param>
35  public TradesPerDayReportElement(string name, string key, BacktestResult backtest, LiveResult live)
36  {
37  _live = live;
38  _backtest = backtest;
39  Name = name;
40  Key = key;
41  }
42 
43  /// <summary>
44  /// Generate trades per day
45  /// </summary>
46  public override string Render()
47  {
48  var liveOrders = _live?.Orders?.Values.ToList();
49  if (liveOrders == null)
50  {
51  liveOrders = new List<Order>();
52  }
53 
54  var orders = _backtest?.Orders?.Values.Concat(liveOrders).OrderBy(x => x.Time);
55  if (orders == null)
56  {
57  return "-";
58  }
59 
60  if (!orders.Any())
61  {
62  return "-";
63  }
64 
65  var days = orders.Last().Time
66  .Subtract(orders.First().Time)
67  .TotalDays;
68 
69  if (days == 0)
70  {
71  days = 1;
72  }
73 
74  var tradesPerDay = orders.Count() / days;
75  Result = tradesPerDay;
76 
77  if (tradesPerDay > 9)
78  {
79  return $"{tradesPerDay:F0}";
80  }
81 
82  return $"{tradesPerDay:F1}";
83  }
84  }
85 }