Lean  $LEAN_TAG$
Backtest.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 Newtonsoft.Json;
19 using System.Collections.Generic;
21 using QuantConnect.Util;
22 
23 namespace QuantConnect.Api
24 {
25  /// <summary>
26  /// A power gauge for backtests, time and parameters to estimate the overfitting risk
27  /// </summary>
28  public class ResearchGuide
29  {
30  /// <summary>
31  /// Number of minutes used in developing the current backtest
32  /// </summary>
33  public int Minutes { get; set; }
34 
35  /// <summary>
36  /// The quantity of backtests run in the project
37  /// </summary>
38  public int BacktestCount { get; set; }
39 
40  /// <summary>
41  /// Number of parameters detected
42  /// </summary>
43  public int Parameters { get; set; }
44 
45  /// <summary>
46  /// Project ID
47  /// </summary>
48  public int ProjectId { get; set; }
49  }
50 
51  /// <summary>
52  /// Base class for backtest result object response
53  /// </summary>
54  public class BasicBacktest : RestResponse
55  {
56  /// <summary>
57  /// Backtest error message
58  /// </summary>
59  public string Error { get; set; }
60 
61  /// <summary>
62  /// Backtest error stacktrace
63  /// </summary>
64  public string Stacktrace { get; set; }
65 
66  /// <summary>
67  /// Assigned backtest Id
68  /// </summary>
69  public string BacktestId { get; set; }
70 
71  /// <summary>
72  /// Status of the backtest
73  /// </summary>
74  public string Status { get; set; }
75 
76  /// <summary>
77  /// Name of the backtest
78  /// </summary>
79  public string Name { get; set; }
80 
81  /// <summary>
82  /// Backtest creation date and time
83  /// </summary>
84  [JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
85  public DateTime Created { get; set; }
86 
87  /// <summary>
88  /// Progress of the backtest in percent 0-1.
89  /// </summary>
90  public decimal Progress { get; set; }
91 
92  /// <summary>
93  /// Optimization task ID, if the backtest is part of an optimization
94  /// </summary>
95  public string OptimizationId { get; set; }
96 
97  /// <summary>
98  /// Number of tradeable days
99  /// </summary>
100  public int TradeableDates { get; set; }
101 
102  /// <summary>
103  /// Optimization parameters
104  /// </summary>
105  public ParameterSet ParameterSet { get; set; }
106 
107  /// <summary>
108  /// Snapshot id of this backtest result
109  /// </summary>
110  public int SnapShotId { get; set; }
111  }
112 
113  /// <summary>
114  /// Results object class. Results are exhaust from backtest or live algorithms running in LEAN
115  /// </summary>
116  public class Backtest : BasicBacktest
117  {
118  /// <summary>
119  /// Note on the backtest attached by the user
120  /// </summary>
121  public string Note { get; set; }
122 
123  /// <summary>
124  /// Boolean true when the backtest is completed.
125  /// </summary>
126  public bool Completed { get; set; }
127 
128  /// <summary>
129  /// Organization ID
130  /// </summary>
131  public int OrganizationId { get; set; }
132 
133  /// <summary>
134  /// Rolling window detailed statistics.
135  /// </summary>
136  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
137  public Dictionary<string, AlgorithmPerformance> RollingWindow { get; set; }
138 
139  /// <summary>
140  /// Total algorithm performance statistics.
141  /// </summary>
142  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
144 
145  /// <summary>
146  /// Charts updates for the live algorithm since the last result packet
147  /// </summary>
148  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
149  public IDictionary<string, Chart> Charts { get; set; }
150 
151  /// <summary>
152  /// Statistics information sent during the algorithm operations.
153  /// </summary>
154  /// <remarks>Intended for update mode -- send updates to the existing statistics in the result GUI. If statistic key does not exist in GUI, create it</remarks>
155  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
156  public IDictionary<string, string> Statistics { get; set; }
157 
158  /// <summary>
159  /// Runtime banner/updating statistics in the title banner of the live algorithm GUI.
160  /// </summary>
161  [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
162  public IDictionary<string, string> RuntimeStatistics { get; set; }
163 
164  /// <summary>
165  /// A power gauge for backtests, time and parameters to estimate the overfitting risk
166  /// </summary>
167  public ResearchGuide ResearchGuide { get; set; }
168 
169  /// <summary>
170  /// The starting time of the backtest
171  /// </summary>
172  public DateTime? BacktestStart { get; set; }
173 
174  /// <summary>
175  /// The ending time of the backtest
176  /// </summary>
177  public DateTime? BacktestEnd { get; set; }
178 
179  /// <summary>
180  /// Indicates if the backtest has error during initialization
181  /// </summary>
182  public bool HasInitializeError { get; set; }
183 
184  /// <summary>
185  /// The backtest node name
186  /// </summary>
187  public string NodeName { get; set; }
188 
189  /// <summary>
190  /// The associated project id
191  /// </summary>
192  public int ProjectId { get; set; }
193 
194  /// <summary>
195  /// End date of out of sample data
196  /// </summary>
197  public DateTime? OutOfSampleMaxEndDate { get; set; }
198 
199  /// <summary>
200  /// Number of days of out of sample days
201  /// </summary>
202  public int? OutOfSampleDays { get; set; }
203  }
204 
205  /// <summary>
206  /// Result object class for the List Backtest response from the API
207  /// </summary>
209  {
210  /// <summary>
211  /// Sharpe ratio with respect to risk free rate: measures excess of return per unit of risk
212  /// </summary>
213  public decimal? SharpeRatio { get; set; }
214 
215  /// <summary>
216  /// Algorithm "Alpha" statistic - abnormal returns over the risk free rate and the relationshio (beta) with the benchmark returns
217  /// </summary>
218  public decimal? Alpha { get; set; }
219 
220  /// <summary>
221  /// Algorithm "beta" statistic - the covariance between the algorithm and benchmark performance, divided by benchmark's variance
222  /// </summary>
223  public decimal? Beta { get; set; }
224 
225  /// <summary>
226  /// Annual compounded returns statistic based on the final-starting capital and years
227  /// </summary>
228  public decimal? CompoundingAnnualReturn { get; set; }
229 
230  /// <summary>
231  /// Drawdown maximum percentage
232  /// </summary>
233  public decimal? Drawdown { get; set; }
234 
235  /// <summary>
236  /// The ratio of the number of losing trades to the total number of trades
237  /// </summary>
238  public decimal? LossRate { get; set; }
239 
240  /// <summary>
241  /// Net profit percentage
242  /// </summary>
243  public decimal? NetProfit { get; set; }
244 
245  /// <summary>
246  /// Number of parameters in the backtest
247  /// </summary>
248  public int? Parameters { get; set; }
249 
250  /// <summary>
251  /// Price-to-sales ratio
252  /// </summary>
253  public decimal? Psr { get; set; }
254 
255  /// <summary>
256  /// SecurityTypes present in the backtest
257  /// </summary>
258  public string? SecurityTypes { get; set; }
259 
260  /// <summary>
261  /// Sortino ratio with respect to risk free rate: measures excess of return per unit of downside risk
262  /// </summary>
263  public decimal? SortinoRatio { get; set; }
264 
265  /// <summary>
266  /// Number of trades in the backtest
267  /// </summary>
268  public int? Trades { get; set; }
269 
270  /// <summary>
271  /// Treynor ratio statistic is a measurement of the returns earned in excess of that which could have been earned on an investment that has no diversifiable risk
272  /// </summary>
273  public decimal? TreynorRatio { get; set; }
274 
275  /// <summary>
276  /// The ratio of the number of winning trades to the total number of trades
277  /// </summary>
278  public decimal? WinRate { get; set; }
279 
280  /// <summary>
281  /// Collection of tags for the backtest
282  /// </summary>
283  public List<string> Tags { get; set; }
284  }
285 
286  /// <summary>
287  /// Wrapper class for Backtest/* endpoints JSON response
288  /// Currently used by Backtest/Read and Backtest/Create
289  /// </summary>
291  {
292  /// <summary>
293  /// Backtest Object
294  /// </summary>
295  public Backtest Backtest { get; set; }
296 
297  /// <summary>
298  /// Indicates if the backtest is run under debugging mode
299  /// </summary>
300  public bool Debugging { get; set; }
301  }
302 
303  /// <summary>
304  /// Collection container for a list of backtests for a project
305  /// </summary>
306  public class BacktestList : RestResponse
307  {
308  /// <summary>
309  /// Collection of summarized backtest objects
310  /// </summary>
311  public List<Backtest> Backtests { get; set; }
312  }
313 
314  /// <summary>
315  /// Collection container for a list of backtest summaries for a project
316  /// </summary>
318  {
319  /// <summary>
320  /// Collection of summarized backtest summary objects
321  /// </summary>
322  public List<BacktestSummary> Backtests { get; set; }
323 
324  /// <summary>
325  /// Number of backtest summaries retrieved in the response
326  /// </summary>
327  public int Count { get; set; }
328  }
329 
330  /// <summary>
331  /// Collection container for a list of backtest tags
332  /// </summary>
333  public class BacktestTags : RestResponse
334  {
335  /// <summary>
336  /// Collection of tags for a backtest
337  /// </summary>
338  public List<string> Tags { get; set; }
339  }
340 }