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="backtestName">New backtest name to set</param>
173  /// <param name="backtestNote">Note attached to the backtest</param>
174  /// <returns>Rest response on success</returns>
175  RestResponse UpdateBacktest(int projectId, string backtestId, string backtestName = "", string backtestNote = "");
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  /// Estimate optimization with the specified parameters via QuantConnect.com API
195  /// </summary>
196  /// <param name="projectId">Project ID of the project the optimization belongs to</param>
197  /// <param name="name">Name of the optimization</param>
198  /// <param name="target">Target of the optimization, see examples in <see cref="PortfolioStatistics"/></param>
199  /// <param name="targetTo">Target extremum of the optimization, for example "max" or "min"</param>
200  /// <param name="targetValue">Optimization target value</param>
201  /// <param name="strategy">Optimization strategy, <see cref="GridSearchOptimizationStrategy"/></param>
202  /// <param name="compileId">Optimization compile ID</param>
203  /// <param name="parameters">Optimization parameters</param>
204  /// <param name="constraints">Optimization constraints</param>
205  /// <returns>Estimate object from the API.</returns>
207  int projectId,
208  string name,
209  string target,
210  string targetTo,
211  decimal? targetValue,
212  string strategy,
213  string compileId,
214  HashSet<OptimizationParameter> parameters,
215  IReadOnlyList<Constraint> constraints);
216 
217  /// <summary>
218  /// Create an optimization with the specified parameters via QuantConnect.com API
219  /// </summary>
220  /// <param name="projectId">Project ID of the project the optimization belongs to</param>
221  /// <param name="name">Name of the optimization</param>
222  /// <param name="target">Target of the optimization, see examples in <see cref="PortfolioStatistics"/></param>
223  /// <param name="targetTo">Target extremum of the optimization, for example "max" or "min"</param>
224  /// <param name="targetValue">Optimization target value</param>
225  /// <param name="strategy">Optimization strategy, <see cref="GridSearchOptimizationStrategy"/></param>
226  /// <param name="compileId">Optimization compile ID</param>
227  /// <param name="parameters">Optimization parameters</param>
228  /// <param name="constraints">Optimization constraints</param>
229  /// <param name="estimatedCost">Estimated cost for optimization</param>
230  /// <param name="nodeType">Optimization node type</param>
231  /// <param name="parallelNodes">Number of parallel nodes for optimization</param>
232  /// <returns>BaseOptimization object from the API.</returns>
234  int projectId,
235  string name,
236  string target,
237  string targetTo,
238  decimal? targetValue,
239  string strategy,
240  string compileId,
241  HashSet<OptimizationParameter> parameters,
242  IReadOnlyList<Constraint> constraints,
243  decimal estimatedCost,
244  string nodeType,
245  int parallelNodes);
246 
247  /// <summary>
248  /// List all the optimizations for a project
249  /// </summary>
250  /// <param name="projectId">Project id we'd like to get a list of optimizations for</param>
251  /// <returns>A list of BaseOptimization objects, <see cref="BaseOptimization"/></returns>
252  public List<BaseOptimization> ListOptimizations(int projectId);
253 
254  /// <summary>
255  /// Read an optimization
256  /// </summary>
257  /// <param name="optimizationId">Optimization id for the optimization we want to read</param>
258  /// <returns><see cref="Optimization"/></returns>
259  public Optimization ReadOptimization(string optimizationId);
260 
261  /// <summary>
262  /// Abort an optimization
263  /// </summary>
264  /// <param name="optimizationId">Optimization id for the optimization we want to abort</param>
265  /// <returns><see cref="RestResponse"/></returns>
266  public RestResponse AbortOptimization(string optimizationId);
267 
268  /// <summary>
269  /// Update an optimization
270  /// </summary>
271  /// <param name="optimizationId">Optimization id we want to update</param>
272  /// <param name="name">Name we'd like to assign to the optimization</param>
273  /// <returns><see cref="RestResponse"/></returns>
274  public RestResponse UpdateOptimization(string optimizationId, string name = null);
275 
276  /// <summary>
277  /// Delete an optimization
278  /// </summary>
279  /// <param name="optimizationId">Optimization id for the optimization we want to delete</param>
280  /// <returns><see cref="RestResponse"/></returns>
281  public RestResponse DeleteOptimization(string optimizationId);
282 
283  /// <summary>
284  /// Gets the logs of a specific live algorithm
285  /// </summary>
286  /// <param name="projectId">Project Id of the live running algorithm</param>
287  /// <param name="algorithmId">Algorithm Id of the live running algorithm</param>
288  /// <param name="startTime">No logs will be returned before this time. Should be in UTC</param>
289  /// <param name="endTime">No logs will be returned after this time. Should be in UTC</param>
290  /// <returns>List of strings that represent the logs of the algorithm</returns>
291  LiveLog ReadLiveLogs(int projectId, string algorithmId, DateTime? startTime = null, DateTime? endTime = null);
292 
293  /// <summary>
294  /// Gets the link to the downloadable data.
295  /// </summary>
296  /// <param name="filePath">File path representing the data requested</param>
297  /// <param name="organizationId">Organization to purchase this data with</param>
298  /// <returns>Link to the downloadable data.</returns>
299  DataLink ReadDataLink(string filePath, string organizationId);
300 
301  /// <summary>
302  /// Get valid data entries for a given filepath from data/list
303  /// </summary>
304  /// <returns></returns>
305  DataList ReadDataDirectory(string filePath);
306 
307  /// <summary>
308  /// Gets data prices from data/prices
309  /// </summary>
310  public DataPricesList ReadDataPrices(string organizationId);
311 
312  /// <summary>
313  /// Read out the report of a backtest in the project id specified.
314  /// </summary>
315  /// <param name="projectId">Project id to read</param>
316  /// <param name="backtestId">Specific backtest id to read</param>
317  /// <returns><see cref="BacktestReport"/></returns>
318  public BacktestReport ReadBacktestReport(int projectId, string backtestId);
319 
320  /// <summary>
321  /// Method to download and save the data purchased through QuantConnect
322  /// </summary>
323  /// <param name="filePath">File path representing the data requested</param>
324  /// <returns>A bool indicating whether the data was successfully downloaded or not.</returns>
325  bool DownloadData(string filePath, string organizationId);
326 
327  /// <summary>
328  /// Will read the organization account status
329  /// </summary>
330  /// <param name="organizationId">The target organization id, if null will return default organization</param>
331  public Account ReadAccount(string organizationId = null);
332 
333  /// <summary>
334  /// Get a list of organizations tied to this account
335  /// </summary>
336  /// <returns></returns>
337  public List<Organization> ListOrganizations();
338 
339  /// <summary>
340  /// Fetch organization data from web API
341  /// </summary>
342  /// <param name="organizationId"></param>
343  /// <returns></returns>
344  public Organization ReadOrganization(string organizationId = null);
345 
346  /// <summary>
347  /// Create a new live algorithm for a logged in user.
348  /// </summary>
349  /// <param name="projectId">Id of the project on QuantConnect</param>
350  /// <param name="compileId">Id of the compilation on QuantConnect</param>
351  /// <param name="serverType">Type of server instance that will run the algorithm</param>
352  /// <param name="baseLiveAlgorithmSettings">Dictionary with Brokerage specific settings</param>
353  /// <param name="versionId">The version identifier</param>
354  /// <param name="dataProviders">Dictionary with data providers and their corresponding credentials</param>
355  /// <returns>Information regarding the new algorithm <see cref="LiveAlgorithm"/></returns>
356  LiveAlgorithm CreateLiveAlgorithm(int projectId, string compileId, string serverType, Dictionary<string, object> baseLiveAlgorithmSettings, string versionId = "-1", Dictionary<string, object> dataProviders = null);
357 
358  /// <summary>
359  /// Get a list of live running algorithms for a logged in user.
360  /// </summary>
361  /// <param name="status">Filter the statuses of the algorithms returned from the api</param>
362  /// <param name="startTime">Earliest launched time of the algorithms returned by the Api</param>
363  /// <param name="endTime">Latest launched time of the algorithms returned by the Api</param>
364  /// <returns>List of live algorithm instances</returns>
365  LiveList ListLiveAlgorithms(AlgorithmStatus? status = null, DateTime? startTime = null, DateTime? endTime = null);
366 
367  /// <summary>
368  /// Read out a live algorithm in the project id specified.
369  /// </summary>
370  /// <param name="projectId">Project id to read</param>
371  /// <param name="deployId">Specific instance id to read</param>
372  /// <returns>Live object with the results</returns>
373  LiveAlgorithmResults ReadLiveAlgorithm(int projectId, string deployId);
374 
375  /// <summary>
376  /// Liquidate a live algorithm from the specified project.
377  /// </summary>
378  /// <param name="projectId">Project for the live instance we want to stop</param>
379  /// <returns></returns>
380  RestResponse LiquidateLiveAlgorithm(int projectId);
381 
382  /// <summary>
383  /// Stop a live algorithm from the specified project.
384  /// </summary>
385  /// <param name="projectId">Project for the live algo we want to delete</param>
386  /// <returns></returns>
387  RestResponse StopLiveAlgorithm(int projectId);
388 
389  /// <summary>
390  /// Sends a notification
391  /// </summary>
392  /// <param name="notification">The notification to send</param>
393  /// <param name="projectId">The project id</param>
394  /// <returns><see cref="RestResponse"/> containing success response and errors</returns>
395  RestResponse SendNotification(Notification notification, int projectId);
396 
397  /// <summary>
398  /// Get the algorithm current status, active or cancelled from the user
399  /// </summary>
400  /// <param name="algorithmId"></param>
401  /// <returns></returns>
402  AlgorithmControl GetAlgorithmStatus(string algorithmId);
403 
404  /// <summary>
405  /// Set the algorithm status from the worker to update the UX e.g. if there was an error.
406  /// </summary>
407  /// <param name="algorithmId">Algorithm id we're setting.</param>
408  /// <param name="status">Status enum of the current worker</param>
409  /// <param name="message">Message for the algorithm status event</param>
410  void SetAlgorithmStatus(string algorithmId, AlgorithmStatus status, string message = "");
411 
412  /// <summary>
413  /// Send the statistics to storage for performance tracking.
414  /// </summary>
415  /// <param name="algorithmId">Identifier for algorithm</param>
416  /// <param name="unrealized">Unrealized gainloss</param>
417  /// <param name="fees">Total fees</param>
418  /// <param name="netProfit">Net profi</param>
419  /// <param name="holdings">Algorithm holdings</param>
420  /// <param name="equity">Total equity</param>
421  /// <param name="netReturn">Algorithm return</param>
422  /// <param name="volume">Volume traded</param>
423  /// <param name="trades">Total trades since inception</param>
424  /// <param name="sharpe">Sharpe ratio since inception</param>
425  void SendStatistics(string algorithmId, decimal unrealized, decimal fees, decimal netProfit, decimal holdings, decimal equity, decimal netReturn, decimal volume, int trades, double sharpe);
426 
427  /// <summary>
428  /// Send an email to the user associated with the specified algorithm id
429  /// </summary>
430  /// <param name="algorithmId">The algorithm id</param>
431  /// <param name="subject">The email subject</param>
432  /// <param name="body">The email message body</param>
433  void SendUserEmail(string algorithmId, string subject, string body);
434 
435  /// <summary>
436  /// Local implementation for downloading data to algorithms
437  /// </summary>
438  /// <param name="address">URL to download</param>
439  /// <param name="headers">KVP headers</param>
440  /// <param name="userName">Username for basic authentication</param>
441  /// <param name="password">Password for basic authentication</param>
442  /// <returns></returns>
443  string Download(string address, IEnumerable<KeyValuePair<string, string>> headers, string userName, string password);
444 
445  /// <summary>
446  /// Download the object store associated with the given organization ID and key
447  /// </summary>
448  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
449  /// <param name="keys">Keys for the Object Store files</param>
450  /// <param name="destinationFolder">Folder in which the object will be stored</param>
451  /// <returns>True if the object was retrieved correctly, false otherwise</returns>
452  public bool GetObjectStore(string organizationId, List<string> keys, string destinationFolder = null);
453 
454  /// <summary>
455  /// Get Object Store properties given the organization ID and the Object Store key
456  /// </summary>
457  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
458  /// <param name="key">Key for the Object Store file</param>
459  /// <returns><see cref="PropertiesObjectStoreResponse"/></returns>
460  /// <remarks>It does not work when the object store is a directory</remarks>
461  public PropertiesObjectStoreResponse GetObjectStoreProperties(string organizationId, string key);
462 
463  /// <summary>
464  /// Upload files to the Object Store
465  /// </summary>
466  /// <param name="organizationId">Organization ID we would like to upload the file to</param>
467  /// <param name="key">Key to the Object Store file</param>
468  /// <param name="objectData">File to be uploaded</param>
469  /// <returns><see cref="RestResponse"/></returns>
470  public RestResponse SetObjectStore(string organizationId, string key, byte[] objectData);
471 
472  /// <summary>
473  /// Request to delete Object Store metadata of a specific organization and key
474  /// </summary>
475  /// <param name="organizationId">Organization ID we would like to delete the Object Store file from</param>
476  /// <param name="key">Key to the Object Store file</param>
477  /// <returns><see cref="RestResponse"/></returns>
478  public RestResponse DeleteObjectStore(string organizationId, string key);
479 
480  /// <summary>
481  /// Gets a list of LEAN versions with their corresponding basic descriptions
482  /// </summary>
484  }
485 }