Lean  $LEAN_TAG$
ReturnsPerTradeReportElement.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;
18 using Python.Runtime;
19 using QuantConnect.Logging;
20 using QuantConnect.Packets;
22 
24 {
25  internal sealed class ReturnsPerTradeReportElement : ChartReportElement
26  {
27  private LiveResult _live;
28  private BacktestResult _backtest;
29 
30  /// <summary>
31  /// Create a new distribution plot of returns per trade
32  /// </summary>
33  /// <param name="name">Name of the widget</param>
34  /// <param name="key">Location of injection</param>
35  /// <param name="backtest">Backtest result object</param>
36  /// <param name="live">Live result object</param>
37  public ReturnsPerTradeReportElement(string name, string key, BacktestResult backtest, LiveResult live)
38  {
39  _live = live;
40  _backtest = backtest;
41  Name = name;
42  Key = key;
43  }
44 
45  /// <summary>
46  /// Generate the returns per trade plot using the python libraries.
47  /// </summary>
48  public override string Render()
49  {
50  var backtestPercentagePerTrade = new List<double>();
51  if (_backtest?.TotalPerformance?.ClosedTrades != null)
52  {
53  foreach (var trade in _backtest.TotalPerformance.ClosedTrades)
54  {
55  if (trade.EntryPrice == 0m)
56  {
57  Log.Error($"ReturnsPerTradeReportElement.Render(): Encountered entry price of 0 in trade with entry time: {trade.EntryTime:yyyy-MM-dd HH:mm:ss} - Exit time: {trade.ExitTime:yyyy-MM-dd HH::mm:ss}");
58  continue;
59  }
60 
61  var sideMultiplier = trade.Direction == TradeDirection.Long ? 1 : -1;
62  backtestPercentagePerTrade.Add(sideMultiplier * (Convert.ToDouble(trade.ExitPrice) - Convert.ToDouble(trade.EntryPrice)) / Convert.ToDouble(trade.EntryPrice));
63  }
64  }
65 
66  // TODO: LiveResult does not contain a TotalPerformance field, so skip live mode for now
67 
68  var base64 = "";
69  using (Py.GIL())
70  {
71  // Charting library does not expect values to be in whole percentage values (i.e. not 1% == 1.0, but rather 1% == 0.01),
72  base64 = Charting.GetReturnsPerTrade(backtestPercentagePerTrade.ToPython());
73  }
74 
75  return base64;
76  }
77  }
78 }