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