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