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  /// Fetch organization data from web API
335  /// </summary>
336  /// <param name="organizationId"></param>
337  /// <returns></returns>
338  public Organization ReadOrganization(string organizationId = null);
339 
340  /// <summary>
341  /// Create a new live algorithm for a logged in user.
342  /// </summary>
343  /// <param name="projectId">Id of the project on QuantConnect</param>
344  /// <param name="compileId">Id of the compilation on QuantConnect</param>
345  /// <param name="serverType">Type of server instance that will run the algorithm</param>
346  /// <param name="baseLiveAlgorithmSettings">Dictionary with Brokerage specific settings</param>
347  /// <param name="versionId">The version identifier</param>
348  /// <param name="dataProviders">Dictionary with data providers and their corresponding credentials</param>
349  /// <returns>Information regarding the new algorithm <see cref="LiveAlgorithm"/></returns>
350  CreateLiveAlgorithmResponse CreateLiveAlgorithm(int projectId, string compileId, string serverType, Dictionary<string, object> baseLiveAlgorithmSettings, string versionId = "-1", Dictionary<string, object> dataProviders = null);
351 
352  /// <summary>
353  /// Get a list of live running algorithms for a logged in user.
354  /// </summary>
355  /// <param name="status">Filter the statuses of the algorithms returned from the api</param>
356  /// <param name="startTime">Earliest launched time of the algorithms returned by the Api</param>
357  /// <param name="endTime">Latest launched time of the algorithms returned by the Api</param>
358  /// <returns>List of live algorithm instances</returns>
359  LiveList ListLiveAlgorithms(AlgorithmStatus? status = null, DateTime? startTime = null, DateTime? endTime = null);
360 
361  /// <summary>
362  /// Read out a live algorithm in the project id specified.
363  /// </summary>
364  /// <param name="projectId">Project id to read</param>
365  /// <param name="deployId">Specific instance id to read</param>
366  /// <returns>Live object with the results</returns>
367  LiveAlgorithmResults ReadLiveAlgorithm(int projectId, string deployId);
368 
369  /// <summary>
370  /// Liquidate a live algorithm from the specified project.
371  /// </summary>
372  /// <param name="projectId">Project for the live instance we want to stop</param>
373  /// <returns></returns>
374  RestResponse LiquidateLiveAlgorithm(int projectId);
375 
376  /// <summary>
377  /// Stop a live algorithm from the specified project.
378  /// </summary>
379  /// <param name="projectId">Project for the live algo we want to delete</param>
380  /// <returns></returns>
381  RestResponse StopLiveAlgorithm(int projectId);
382 
383  /// <summary>
384  /// Sends a notification
385  /// </summary>
386  /// <param name="notification">The notification to send</param>
387  /// <param name="projectId">The project id</param>
388  /// <returns><see cref="RestResponse"/> containing success response and errors</returns>
389  RestResponse SendNotification(Notification notification, int projectId);
390 
391  /// <summary>
392  /// Get the algorithm current status, active or cancelled from the user
393  /// </summary>
394  /// <param name="algorithmId"></param>
395  /// <returns></returns>
396  AlgorithmControl GetAlgorithmStatus(string algorithmId);
397 
398  /// <summary>
399  /// Set the algorithm status from the worker to update the UX e.g. if there was an error.
400  /// </summary>
401  /// <param name="algorithmId">Algorithm id we're setting.</param>
402  /// <param name="status">Status enum of the current worker</param>
403  /// <param name="message">Message for the algorithm status event</param>
404  void SetAlgorithmStatus(string algorithmId, AlgorithmStatus status, string message = "");
405 
406  /// <summary>
407  /// Send the statistics to storage for performance tracking.
408  /// </summary>
409  /// <param name="algorithmId">Identifier for algorithm</param>
410  /// <param name="unrealized">Unrealized gainloss</param>
411  /// <param name="fees">Total fees</param>
412  /// <param name="netProfit">Net profi</param>
413  /// <param name="holdings">Algorithm holdings</param>
414  /// <param name="equity">Total equity</param>
415  /// <param name="netReturn">Algorithm return</param>
416  /// <param name="volume">Volume traded</param>
417  /// <param name="trades">Total trades since inception</param>
418  /// <param name="sharpe">Sharpe ratio since inception</param>
419  void SendStatistics(string algorithmId, decimal unrealized, decimal fees, decimal netProfit, decimal holdings, decimal equity, decimal netReturn, decimal volume, int trades, double sharpe);
420 
421  /// <summary>
422  /// Send an email to the user associated with the specified algorithm id
423  /// </summary>
424  /// <param name="algorithmId">The algorithm id</param>
425  /// <param name="subject">The email subject</param>
426  /// <param name="body">The email message body</param>
427  void SendUserEmail(string algorithmId, string subject, string body);
428 
429  /// <summary>
430  /// Local implementation for downloading data to algorithms
431  /// </summary>
432  /// <param name="address">URL to download</param>
433  /// <param name="headers">KVP headers</param>
434  /// <param name="userName">Username for basic authentication</param>
435  /// <param name="password">Password for basic authentication</param>
436  /// <returns></returns>
437  string Download(string address, IEnumerable<KeyValuePair<string, string>> headers, string userName, string password);
438 
439  /// <summary>
440  /// Local implementation for downloading data to algorithms
441  /// </summary>
442  /// <param name="address">URL to download</param>
443  /// <param name="headers">KVP headers</param>
444  /// <param name="userName">Username for basic authentication</param>
445  /// <param name="password">Password for basic authentication</param>
446  /// <returns></returns>
447  byte[] DownloadBytes(string address, IEnumerable<KeyValuePair<string, string>> headers, string userName, string password);
448 
449  /// <summary>
450  /// Download the object store associated with the given organization ID and key
451  /// </summary>
452  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
453  /// <param name="keys">Keys for the Object Store files</param>
454  /// <param name="destinationFolder">Folder in which the object will be stored</param>
455  /// <returns>True if the object was retrieved correctly, false otherwise</returns>
456  public bool GetObjectStore(string organizationId, List<string> keys, string destinationFolder = null);
457 
458  /// <summary>
459  /// Get Object Store properties given the organization ID and the Object Store key
460  /// </summary>
461  /// <param name="organizationId">Organization ID we would like to get the Object Store from</param>
462  /// <param name="key">Key for the Object Store file</param>
463  /// <returns><see cref="PropertiesObjectStoreResponse"/></returns>
464  /// <remarks>It does not work when the object store is a directory</remarks>
465  public PropertiesObjectStoreResponse GetObjectStoreProperties(string organizationId, string key);
466 
467  /// <summary>
468  /// Upload files to the Object Store
469  /// </summary>
470  /// <param name="organizationId">Organization ID we would like to upload the file to</param>
471  /// <param name="key">Key to the Object Store file</param>
472  /// <param name="objectData">File to be uploaded</param>
473  /// <returns><see cref="RestResponse"/></returns>
474  public RestResponse SetObjectStore(string organizationId, string key, byte[] objectData);
475 
476  /// <summary>
477  /// Request to delete Object Store metadata of a specific organization and key
478  /// </summary>
479  /// <param name="organizationId">Organization ID we would like to delete the Object Store file from</param>
480  /// <param name="key">Key to the Object Store file</param>
481  /// <returns><see cref="RestResponse"/></returns>
482  public RestResponse DeleteObjectStore(string organizationId, string key);
483 
484  /// <summary>
485  /// Gets a list of LEAN versions with their corresponding basic descriptions
486  /// </summary>
488  }
489 }