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