Lean  $LEAN_TAG$
Data.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.Linq;
17 using Newtonsoft.Json;
18 using System.Collections.Generic;
19 using System.Text.RegularExpressions;
20 
21 // Collection of response objects for Quantconnect Data/ endpoints
22 namespace QuantConnect.Api
23 {
24  /// <summary>
25  /// Data/Read response wrapper, contains link to requested data
26  /// </summary>
27  public class DataLink : RestResponse
28  {
29  /// <summary>
30  /// Url to the data requested
31  /// </summary>
32  [JsonProperty(PropertyName = "link")]
33  public string Url { get; set; }
34 
35  /// <summary>
36  /// Remaining QCC balance on account after this transaction
37  /// </summary>
38  [JsonProperty(PropertyName = "balance")]
39  public double Balance { get; set; }
40 
41  /// <summary>
42  /// QCC Cost for this data link
43  /// </summary>
44  [JsonProperty(PropertyName = "cost")]
45  public double Cost { get; set; }
46  }
47 
48  /// <summary>
49  /// Data/List response wrapper for available data
50  /// </summary>
51  public class DataList : RestResponse
52  {
53  /// <summary>
54  /// List of all available data from this request
55  /// </summary>
56  [JsonProperty(PropertyName = "objects")]
57  public List<string> AvailableData { get; set; }
58  }
59 
60  /// <summary>
61  /// Data/Prices response wrapper for prices by vendor
62  /// </summary>
64  {
65  /// <summary>
66  /// Collection of prices objects
67  /// </summary>
68  [JsonProperty(PropertyName = "prices")]
69  public List<PriceEntry> Prices { get; set; }
70 
71  /// <summary>
72  /// The Agreement URL for this Organization
73  /// </summary>
74  [JsonProperty(PropertyName = "agreement")]
75  public string AgreementUrl { get; set; }
76 
77  /// <summary>
78  /// Get the price in QCC for a given data file
79  /// </summary>
80  /// <param name="path">Lean data path of the file</param>
81  /// <returns>QCC price for data, -1 if no entry found</returns>
82  public int GetPrice(string path)
83  {
84  if (path == null)
85  {
86  return -1;
87  }
88 
89  var entry = Prices.FirstOrDefault(x => x.RegEx.IsMatch(path));
90  return entry?.Price ?? -1;
91  }
92  }
93 
94  /// <summary>
95  /// Prices entry for Data/Prices response
96  /// </summary>
97  public class PriceEntry
98  {
99  private Regex _regex;
100 
101  /// <summary>
102  /// Vendor for this price
103  /// </summary>
104  [JsonProperty(PropertyName = "vendorName")]
105  public string Vendor { get; set; }
106 
107  /// <summary>
108  /// Regex for this data price entry
109  /// Trims regex open, close, and multiline flag
110  /// because it won't match otherwise
111  /// </summary>
112  public Regex RegEx
113  {
114  get
115  {
116  if (_regex == null && RawRegEx != null)
117  {
118  _regex = new Regex(RawRegEx.TrimStart('/').TrimEnd('m').TrimEnd('/'), RegexOptions.Compiled);
119  }
120  return _regex;
121  }
122  }
123 
124  /// <summary>
125  /// RegEx directly from response
126  /// </summary>
127  [JsonProperty(PropertyName = "regex")]
128  public string RawRegEx { get; set; }
129 
130  /// <summary>
131  /// The price for this entry in QCC
132  /// </summary>
133  [JsonProperty(PropertyName = "price")]
134  public int? Price { get; set; }
135 
136  /// <summary>
137  /// The type associated to this price entry if any
138  /// </summary>
139  [JsonProperty(PropertyName = "type")]
140  public string Type { get; set; }
141 
142  /// <summary>
143  /// True if the user is subscribed
144  /// </summary>
145  [JsonProperty(PropertyName = "subscribed")]
146  public bool? Subscribed { get; set; }
147 
148  /// <summary>
149  /// The associated product id
150  /// </summary>
151  [JsonProperty(PropertyName = "productId")]
152  public int ProductId { get; set; }
153 
154  /// <summary>
155  /// The associated data paths
156  /// </summary>
157  [JsonProperty(PropertyName = "paths")]
158  public HashSet<string> Paths { get; set; }
159  }
160 }