Lean  $LEAN_TAG$
IApi.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 
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel.Composition;
20 using QuantConnect.Api;
25 
27 {
28  /// <summary>
29  /// API for QuantConnect.com
30  /// </summary>
31  [InheritedExport(typeof(IApi))]
32  public interface IApi : IDisposable
33  {
34  /// <summary>
35  /// Initialize the control system
36  /// </summary>
37  void Initialize(int userId, string token, string dataFolder);
38 
39  /// <summary>
40  /// Create a project with the specified name and language via QuantConnect.com API
41  /// </summary>
42  /// <param name="name">Project name</param>
43  /// <param name="language">Programming language to use</param>
44  /// <param name="organizationId">Organization to create this project under</param>
45  /// <returns><see cref="ProjectResponse"/> that includes information about the newly created project</returns>
46  ProjectResponse CreateProject(string name, Language language, string organizationId = null);
47 
48  /// <summary>
49  /// Read in a project from the QuantConnect.com API.
50  /// </summary>
51  /// <param name="projectId">Project id you own</param>
52  /// <returns><see cref="ProjectResponse"/> about a specific project</returns>
53  ProjectResponse ReadProject(int projectId);
54 
55  /// <summary>
56  /// Add a file to a project
57  /// </summary>
58  /// <param name="projectId">The project to which the file should be added</param>
59  /// <param name="name">The name of the new file</param>
60  /// <param name="content">The content of the new file</param>
61  /// <returns><see cref="ProjectFilesResponse"/> that includes information about the newly created file</returns>
62  RestResponse AddProjectFile(int projectId, string name, string content);
63 
64  /// <summary>
65  /// Update the name of a file
66  /// </summary>
67  /// <param name="projectId">Project id to which the file belongs</param>
68  /// <param name="oldFileName">The current name of the file</param>
69  /// <param name="newFileName">The new name for the file</param>
70  /// <returns><see cref="RestResponse"/> indicating success</returns>
71  RestResponse UpdateProjectFileName(int projectId, string oldFileName, string newFileName);
72 
73  /// <summary>
74  /// Update the contents of a file
75  /// </summary>
76  /// <param name="projectId">Project id to which the file belongs</param>
77  /// <param name="fileName">The name of the file that should be updated</param>
78  /// <param name="newFileContents">The new contents of the file</param>
79  /// <returns><see cref="RestResponse"/> indicating success</returns>
80  RestResponse UpdateProjectFileContent(int projectId, string fileName, string newFileContents);
81 
82  /// <summary>
83  /// Read a file in a project
84  /// </summary>
85  /// <param name="projectId">Project id to which the file belongs</param>
86  /// <param name="fileName">The name of the file</param>
87  /// <returns><see cref="ProjectFilesResponse"/> that includes the file information</returns>
88  ProjectFilesResponse ReadProjectFile(int projectId, string fileName);
89 
90  /// <summary>
91  /// Read all files in a project
92  /// </summary>
93  /// <param name="projectId">Project id to which the file belongs</param>
94  /// <returns><see cref="ProjectFilesResponse"/> that includes the information about all files in the project</returns>
96 
97  /// <summary>
98  /// Read all nodes in a project.
99  /// </summary>
100  /// <param name="projectId">Project id to which the nodes refer</param>
101  /// <returns><see cref="ProjectNodesResponse"/> that includes the information about all nodes in the project</returns>
102  ProjectNodesResponse ReadProjectNodes(int projectId);
103 
104  /// <summary>
105  /// Update the active state of some nodes to true.
106  /// If you don't provide any nodes, all the nodes become inactive and AutoSelectNode is true.
107  /// </summary>
108  /// <param name="projectId">Project id to which the nodes refer</param>
109  /// <param name="nodes">List of node ids to update</param>
110  /// <returns><see cref="ProjectNodesResponse"/> that includes the information about all nodes in the project</returns>
111  ProjectNodesResponse UpdateProjectNodes(int projectId, string[] nodes);
112 
113  /// <summary>
114  /// Delete a file in a project
115  /// </summary>
116  /// <param name="projectId">Project id to which the file belongs</param>
117  /// <param name="name">The name of the file that should be deleted</param>
118  /// <returns><see cref="ProjectFilesResponse"/> that includes the information about all files in the project</returns>
119  RestResponse DeleteProjectFile(int projectId, string name);
120 
121  /// <summary>
122  /// Delete a specific project owned by the user from QuantConnect.com
123  /// </summary>
124  /// <param name="projectId">Project id we own and wish to delete</param>
125  /// <returns>RestResponse indicating success</returns>
126  RestResponse DeleteProject(int projectId);
127 
128  /// <summary>
129  /// Read back a list of all projects on the account for a user.
130  /// </summary>
131  /// <returns>Container for list of projects</returns>
133 
134  /// <summary>
135  /// Create a new compile job request for this project id.
136  /// </summary>
137  /// <param name="projectId">Project id we wish to compile.</param>
138  /// <returns>Compile object result</returns>
139  Compile CreateCompile(int projectId);
140 
141  /// <summary>
142  /// Read a compile packet job result.
143  /// </summary>
144  /// <param name="projectId">Project id we sent for compile</param>
145  /// <param name="compileId">Compile id return from the creation request</param>
146  /// <returns>Compile object result</returns>
147  Compile ReadCompile(int projectId, string compileId);
148 
149  /// <summary>
150  /// Create a new backtest from a specified projectId and compileId
151  /// </summary>
152  /// <param name="projectId"></param>
153  /// <param name="compileId"></param>
154  /// <param name="backtestName"></param>
155  /// <returns></returns>
156  Backtest CreateBacktest(int projectId, string compileId, string backtestName);
157 
158  /// <summary>
159  /// Read out the full result of a specific backtest
160  /// </summary>
161  /// <param name="projectId">Project id for the backtest we'd like to read</param>
162  /// <param name="backtestId">Backtest id for the backtest we'd like to read</param>
163  /// <param name="getCharts">True will return backtest charts</param>
164  /// <returns>Backtest result object</returns>
165  Backtest ReadBacktest(int projectId, string backtestId, bool getCharts = true);
166 
167  /// <summary>
168  /// Update the backtest name
169  /// </summary>
170  /// <param name="projectId">Project id to update</param>
171  /// <param name="backtestId">Backtest id to update</param>
172  /// <param name="name">New backtest name to set</param>
173  /// <param name="note">Note attached to the backtest</param>
174  /// <returns>Rest response on success</returns>
175  RestResponse UpdateBacktest(int projectId, string backtestId, string name = "", string note = "");
176 
177  /// <summary>
178  /// Delete a backtest from the specified project and backtestId.
179  /// </summary>
180  /// <param name="projectId">Project for the backtest we want to delete</param>
181  /// <param name="backtestId">Backtest id we want to delete</param>
182  /// <returns>RestResponse on success</returns>
183  RestResponse DeleteBacktest(int projectId, string backtestId);
184 
185  /// <summary>
186  /// Get a list of backtest summaries for a specific project id
187  /// </summary>
188  /// <param name="projectId">Project id to search</param>
189  /// <param name="includeStatistics">True for include statistics in the response, false otherwise</param>
190  /// <returns>BacktestList container for list of backtests</returns>
191  BacktestSummaryList ListBacktests(int projectId, bool includeStatistics = false);
192 
193  /// <summary>
194  /// Read out the insights of a backtest
195  /// </summary>
196  /// <param name="projectId">Id of the project from which to read the backtest</param>
197  /// <param name="backtestId">Backtest id from which we want to get the insights</param>
198  /// <param name="start">Starting index of the insights to be fetched</param>
199  /// <param name="end">Last index of the insights to be fetched. Note that end - start must be less than 100</param>
200  /// <returns><see cref="InsightResponse"/></returns>
201  /// <exception cref="ArgumentException"></exception>
202  public InsightResponse ReadBacktestInsights(int projectId, string backtestId, int start = 0, int end = 0);
203 
204  #pragma warning disable CS1574
205  /// <summary>
206  /// Estimate optimization with the specified parameters via QuantConnect.com API
207  /// </summary>
208  /// <param name="projectId">Project ID of the project the optimization belongs to</param>
209  /// <param name="name">Name of the optimization</param>
210  /// <param name="target">Target of the optimization, see examples in <see cref="PortfolioStatistics"/></param>
211  /// <param name="targetTo">Target extremum of the optimization, for example "max" or "min"</param>
212  /// <param name="targetValue">Optimization target value</param>
213  /// <param name="strategy">Optimization strategy, <see cref="GridSearchOptimizationStrategy"/></param>
214  /// <param name="compileId">Optimization compile ID</param>
215  /// <param name="parameters">Optimization parameters</param>
216  /// <param name="constraints">Optimization constraints</param>
217  /// <returns>Estimate object from the API.</returns>
218  #pragma warning restore CS1574
220  int projectId,
221  string name,
222  string target,
223  string targetTo,
224  decimal? targetValue,
225  string strategy,
226  string compileId,
227  HashSet<OptimizationParameter> parameters,
228  IReadOnlyList<Constraint> constraints);
229 
230  #pragma warning disable CS1574
231  /// <summary>
232  /// Create an optimization with the specified parameters via QuantConnect.com API
233  /// </summary>
234  /// <param name="projectId">Project ID of the project the optimization belongs to</param>
235  /// <param name="name">Name of the optimization</param>
236  /// <param name="target">Target of the optimization, see examples in <see cref="PortfolioStatistics"/></param>
237  /// <param name="targetTo">Target extremum of the optimization, for example "max" or "min"</param>
238  /// <param name="targetValue">Optimization target value</param>
239  /// <param name="strategy">Optimization strategy, <see cref="GridSearchOptimizationStrategy"/></param>
240  /// <param name="compileId">Optimization compile ID</param>
241  /// <param name="parameters">Optimization parameters</param>
242  /// <param name="constraints">Optimization constraints</param>
243  /// <param name="estimatedCost">Estimated cost for optimization</param>
244  /// <param name="nodeType">Optimization node type</param>
245  /// <param name="parallelNodes">Number of parallel nodes for optimization</param>
246  /// <returns>BaseOptimization object from the API.</returns>
247  #pragma warning restore CS1574
249  int projectId,
250  string name,
251  string target,
252  string targetTo,
253  decimal? targetValue,
254  string strategy,
255  string compileId,
256  HashSet<OptimizationParameter> parameters,
257  IReadOnlyList<Constraint> constraints,
258  decimal estimatedCost,
259  string nodeType,
260  int parallelNodes);
261 
262  /// <summary>
263  /// List all the optimizations for a project
264  /// </summary>
265  /// <param name="projectId">Project id we'd like to get a list of optimizations for</param>
266  /// <returns>A list of BaseOptimization objects, <see cref="BaseOptimization"/></returns>
267  public List<OptimizationSummary> ListOptimizations(int projectId);
268 
269  /// <summary>
270  /// Read an optimization
271  /// </summary>
272  /// <param name="optimizationId">Optimization id for the optimization we want to read</param>
273  /// <returns><see cref="Optimization"/></returns>
274  public Optimization ReadOptimization(string optimizationId);
275 
276  /// <summary>
277  /// Abort an optimization
278  /// </summary>
279  /// <param name="optimizationId">Optimization id for the optimization we want to abort</param>
280  /// <returns><see cref="RestResponse"/></returns>
281  public RestResponse AbortOptimization(string optimizationId);
282 
283  /// <summary>
284  /// Update an optimization
285  /// </summary>
286  /// <param name="optimizationId">Optimization id we want to update</param>
287  /// <param name="name">Name we'd like to assign to the optimization</param>
288  /// <returns><see cref="RestResponse"/></returns>
289  public RestResponse UpdateOptimization(string optimizationId, string name = null);
290 
291  /// <summary>
292  /// Delete an optimization
293  /// </summary>
294  /// <param name="optimizationId">Optimization id for the optimization we want to delete</param>
295  /// <returns><see cref="RestResponse"/></returns>
296  public RestResponse DeleteOptimization(string optimizationId);
297 
298  /// <summary>
299  /// Gets the logs of a specific live algorithm
300  /// </summary>
301  /// <param name="projectId">Project Id of the live running algorithm</param>
302  /// <param name="algorithmId">Algorithm Id of the live running algorithm</param>
303  /// <param name="startLine">Start line of logs to read</param>
304  /// <param name="endLine">End line of logs to read</param>
305  /// <returns>List of strings that represent the logs of the algorithm</returns>
306  LiveLog ReadLiveLogs(int projectId, string algorithmId, int startLine, int endLine);
307 
308  /// <summary>
309  /// Returns a chart object from a live algorithm
310  /// </summary>
311  /// <param name="projectId">Project ID of the request</param>
312  /// <param name="name">The requested chart name</param>
313  /// <param name="start">The Utc start seconds timestamp of the request</param>
314  /// <param name="end">The Utc end seconds timestamp of the request</param>
315  /// <param name="count">The number of data points to request</param>
316  /// <returns></returns>
317  public ReadChartResponse ReadLiveChart(int projectId, string name, int start, int end, uint count);
318 
319  /// <summary>
320  /// Read out the portfolio state of a live algorithm
321  /// </summary>
322  /// <param name="projectId">Id of the project from which to read the live algorithm</param>
323  /// <returns><see cref="PortfolioResponse"/></returns>
324  public PortfolioResponse ReadLivePortfolio(int projectId);
325 
326  /// <summary>
327  /// Read out the insights of a live algorithm
328  /// </summary>
329  /// <param name="projectId">Id of the project from which to read the live algorithm</param>
330  /// <param name="start">Starting index of the insights to be fetched</param>
331  /// <param name="end">Last index of the insights to be fetched. Note that end - start must be less than 100</param>
332  /// <returns><see cref="InsightResponse"/></returns>
333  /// <exception cref="ArgumentException"></exception>
334  public InsightResponse ReadLiveInsights(int projectId, int start = 0, int end = 0);
335 
336  /// <summary>
337  /// Gets the link to the downloadable data.
338  /// </summary>
339  /// <param name="filePath">File path representing the data requested</param>
340  /// <param name="organizationId">Organization to purchase this data with</param>
341  /// <returns>Link to the downloadable data.</returns>
342  DataLink ReadDataLink(string filePath, string organizationId);
343 
344  /// <summary>
345  /// Get valid data entries for a given filepath from data/list
346  /// </summary>
347  /// <returns></returns>
348  DataList ReadDataDirectory(string filePath);
349 
350  /// <summary>
351  /// Gets data prices from data/prices
352  /// </summary>
353  public DataPricesList ReadDataPrices(string organizationId);
354 
355  /// <summary>
356  /// Read out the report of a backtest in the project id specified.
357  /// </summary>
358  /// <param name="projectId">Project id to read</param>
359  /// <param name="backtestId">Specific backtest id to read</param>
360  /// <returns><see cref="BacktestReport"/></returns>
361  public BacktestReport ReadBacktestReport(int projectId, string backtestId);
362 
363  /// <summary>
364  /// Returns a requested chart object from a backtest
365  /// </summary>
366  /// <param name="projectId">Project ID of the request</param>
367  /// <param name="name">The requested chart name</param>
368  /// <param name="start">The Utc start seconds timestamp of the request</param>
369  /// <param name="end">The Utc end seconds timestamp of the request</param>
370  /// <param name="count">The number of data points to request</param>
371  /// <param name="backtestId">Associated Backtest ID for this chart request</param>
372  /// <returns></returns>
373  public ReadChartResponse ReadBacktestChart(int projectId, string name, int start, int end, uint count, string backtestId);
374 
375  /// <summary>
376  /// Method to download and save the data purchased through QuantConnect
377  /// </summary>
378  /// <param name="filePath">File path representing the data requested</param>
379  /// <returns>A bool indicating whether the data was successfully downloaded or not.</returns>
380  bool DownloadData(string filePath, string organizationId);
381 
382  /// <summary>
383  /// Will read the organization account status
384  /// </summary>
385  /// <param name="organizationId">The target organization id, if null will return default organization</param>
386  public Account ReadAccount(string organizationId = null);
387 
388  /// <summary>
389  /// Fetch organization data from web API
390  /// </summary>
391  /// <param name="organizationId"></param>
392  /// <returns></returns>
393  public Organization ReadOrganization(string organizationId = null);
394 
395  /// <summary>
396  /// Create a new live algorithm for a logged in user.
397  /// </summary>
398  /// <param name="projectId">Id of the project on QuantConnect</param>
399  /// <param name="compileId">Id of the compilation on QuantConnect</param>
400  /// <param name="nodeId">Id of the node that will run the algorithm</param>
401  /// <param name="brokerageSettings">Dictionary with Brokerage specific settings</param>
402  /// <param name="versionId">The version identifier</param>
403  /// <param name="dataProviders">Dictionary with data providers and their corresponding credentials</param>
404  /// <returns>Information regarding the new algorithm <see cref="CreateLiveAlgorithmResponse"/></returns>
405  CreateLiveAlgorithmResponse CreateLiveAlgorithm(int projectId, string compileId, string nodeId, Dictionary<string, object> brokerageSettings, string versionId = "-1", Dictionary<string, object> dataProviders = null);
406 
407  /// <summary>
408  /// Get a list of live running algorithms for a logged in user.
409  /// </summary>
410  /// <param name="status">Filter the statuses of the algorithms returned from the api</param>
411  /// <param name="startTime">Earliest launched time of the algorithms returned by the Api</param>
412  /// <param name="endTime">Latest launched time of the algorithms returned by the Api</param>
413  /// <returns>List of live algorithm instances</returns>
414  LiveList ListLiveAlgorithms(AlgorithmStatus? status = null, DateTime? startTime = null, DateTime? endTime = null);
415 
416  /// <summary>
417  /// Read out a live algorithm in the project id specified.
418  /// </summary>
419  /// <param name="projectId">Project id to read</param>
420  /// <param name="deployId">Specific instance id to read</param>
421  /// <returns>Live object with the results</returns>
422  LiveAlgorithmResults ReadLiveAlgorithm(int projectId, string deployId);
423 
424  /// <summary>
425  /// Liquidate a live algorithm from the specified project.
426  /// </summary>
427  /// <param name="projectId">Project for the live instance we want to stop</param>
428  /// <returns></returns>
429  RestResponse LiquidateLiveAlgorithm(int projectId);
430 
431  /// <summary>
432  /// Stop a live algorithm from the specified project.
433  /// </summary>
434  /// <param name="projectId">Project for the live algo we want to delete</param>
435  /// <returns></returns>
436  RestResponse StopLiveAlgorithm(int projectId);
437 
438  /// <summary>
439  /// Sends a notification
440  /// </summary>
441  /// <param name="notification">The notification to send</param>
442  /// <param name="projectId">The project id</param>
443  /// <returns><see cref="RestResponse"/> containing success response and errors</returns>
444  RestResponse SendNotification(Notification notification, int projectId);
445 
446  /// <summary>
447  /// Get the algorithm current status, active or cancelled from the user
448  /// </summary>
449  /// <param name="algorithmId"></param>
450  /// <returns></returns>
451  AlgorithmControl GetAlgorithmStatus(string algorithmId);
452 
453  /// <summary>
454  /// Set the algorithm status from the worker to update the UX e.g. if there was an error.
455  /// </summary>
456  /// <param name="algorithmId">Algorithm id we're setting.</param>
457  /// <param name="status">Status enum of the current worker</param>
458  /// <param name="message">Message for the algorithm status event</param>
459  void SetAlgorithmStatus(string algorithmId, AlgorithmStatus status, string message = "");
460 
461  /// <summary>
462  /// Send the statistics to storage for performance tracking.
463  /// </summary>
464  /// <param name="algorithmId">Identifier for algorithm</param>
465  /// <param name="unrealized">Unrealized gainloss</param>
466  /// <param name="fees">Total fees</param>
467  /// <param name="netProfit">Net profi</param>
468  /// <param name="holdings">Algorithm holdings</param>
469  /// <param name="equity">Total equity</param>
470  /// <param name="netReturn">Algorithm return</param>
471  /// <param name="volume">Volume traded</param>
472  /// <param name="trades">Total trades since inception</param>
473  /// <param name="sharpe">Sharpe ratio since inception</param>
474  void SendStatistics(string algorithmId, decimal unrealized, decimal fees, decimal netProfit, decimal holdings, decimal equity, decimal netReturn, decimal volume, int trades, double sharpe);
475 
476  /// <summary>
477  /// Send an email to the user associated with the specified algorithm id
478  /// </summary>
479  /// <param name="algorithmId">The algorithm id</param>
480  /// <param name="subject">The email subject</param>
481  /// <param name="body">The email message body</param>
482  void SendUserEmail(string algorithmId, string subject, string body);
483 
484  /// <summary>
485  /// Local implementation for downloading data to algorithms
486  /// </summary>
487  /// <param name="address">URL to download</param>
488  /// <param name="headers">KVP headers</param>
489  /// <param name="userName">Username for basic authentication</param>
490  /// <param name="password">Password for basic authentication</param>
491  /// <returns></returns>
492  string Download(string address, IEnumerable<KeyValuePair<string, string>> headers, string userName, string password);
493 
494  /// <summary>
495  /// Local implementation for downloading data to algorithms
496  /// </summary>
497  /// <param name="address">URL to download</param>
498  /// <param name="headers">KVP headers</param>
499  /// <param name="userName">Username for basic authentication</param>
500  /// <param name="password">Password for basic authentication</param>
501  /// <returns></returns>
502  byte[] DownloadBytes(string address, IEnumerable<KeyValuePair<string, string>> headers, string userName, string password);
503 
504  /// <summary>
505  /// Download the object store associated with the given organization ID and key
506  /// </summary>
507  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
508  /// <param name="keys">Keys for the Object Store files</param>
509  /// <param name="destinationFolder">Folder in which the object will be stored</param>
510  /// <returns>True if the object was retrieved correctly, false otherwise</returns>
511  public bool GetObjectStore(string organizationId, List<string> keys, string destinationFolder = null);
512 
513  /// <summary>
514  /// Get Object Store properties given the organization ID and the Object Store key
515  /// </summary>
516  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
517  /// <param name="key">Key for the Object Store file</param>
518  /// <returns><see cref="PropertiesObjectStoreResponse"/></returns>
519  /// <remarks>It does not work when the object store is a directory</remarks>
520  public PropertiesObjectStoreResponse GetObjectStoreProperties(string organizationId, string key);
521 
522  /// <summary>
523  /// Upload files to the Object Store
524  /// </summary>
525  /// <param name="organizationId">Organization ID we would like to upload the file to</param>
526  /// <param name="key">Key to the Object Store file</param>
527  /// <param name="objectData">File to be uploaded</param>
528  /// <returns><see cref="RestResponse"/></returns>
529  public RestResponse SetObjectStore(string organizationId, string key, byte[] objectData);
530 
531  /// <summary>
532  /// Request to delete Object Store metadata of a specific organization and key
533  /// </summary>
534  /// <param name="organizationId">Organization ID we would like to delete the Object Store file from</param>
535  /// <param name="key">Key to the Object Store file</param>
536  /// <returns><see cref="RestResponse"/></returns>
537  public RestResponse DeleteObjectStore(string organizationId, string key);
538 
539  /// <summary>
540  /// Gets a list of LEAN versions with their corresponding basic descriptions
541  /// </summary>
543  }
544 }