Lean  $LEAN_TAG$
IntrinioEconomicData.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;
17 using System.Collections.Generic;
18 using System.Globalization;
19 using System.Text;
20 
22 {
23  /// <summary>
24  /// TRanformation available for the Economic data.
25  /// </summary>
27  {
28  /// <summary>
29  /// The rate of change
30  /// </summary>
31  Roc,
32 
33  /// <summary>
34  /// Rate of change from Year Ago
35  /// </summary>
36  AnnualyRoc,
37 
38  /// <summary>
39  /// The compounded annual rate of change
40  /// </summary>
42 
43  /// <summary>
44  /// The continuously compounded annual rate of change
45  /// </summary>
47 
48  /// <summary>
49  /// The continuously compounded rateof change
50  /// </summary>
51  CCRoc,
52 
53  /// <summary>
54  /// The level, no transformation.
55  /// </summary>
56  Level,
57 
58  /// <summary>
59  /// The natural log
60  /// </summary>
61  Ln,
62 
63  /// <summary>
64  /// The percent change
65  /// </summary>
66  Pc,
67 
68  /// <summary>
69  /// The percent change from year ago
70  /// </summary>
71  AnnualyPc
72  }
73 
74  /// <summary>
75  /// Access the massive repository of economic data from the Federal Reserve Economic Data system via the Intrinio API.
76  /// </summary>
77  /// <seealso cref="QuantConnect.Data.BaseData" />
79  {
80  private readonly string _baseUrl = @"https://api.intrinio.com/historical_data.csv?";
81 
82  private readonly IntrinioDataTransformation _dataTransformation;
83 
84 
85  private bool _backtestingFirstTimeCallOrLiveMode = true;
86 
87  /// <summary>
88  /// Initializes a new instance of the <see cref="IntrinioEconomicData" /> class.
89  /// </summary>
91  {
92  }
93 
94  /// <summary>
95  /// Initializes a new instance of the <see cref="IntrinioEconomicData" /> class.
96  /// </summary>
97  /// <param name="dataTransformation">The item.</param>
99  {
100  _dataTransformation = dataTransformation;
101 
102  // If the user and the password is not set then then throw error.
104  {
105  throw new
106  InvalidOperationException("Please set a valid Intrinio user and password using the 'IntrinioEconomicData.SetUserAndPassword' static method. " +
107  "For local backtesting, the user and password can be set in the 'parameters' fields from the 'config.json' file.");
108  }
109  }
110 
111 
112  /// <summary>
113  /// Return the URL string source of the file. This will be converted to a stream
114  /// </summary>
115  /// <param name="config">Configuration object</param>
116  /// <param name="date">Date of this source file</param>
117  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
118  /// <returns>
119  /// String URL of source file.
120  /// </returns>
121  /// <remarks>
122  /// Given Intrinio's API limits, we cannot make more than one CSV request per second. That's why in backtesting mode
123  /// we make sure we make just one call to retrieve all the data needed. Also, to avoid the problem of many sources
124  /// asking the data at the beginning of the algorithm, a pause of a second is added.
125  /// </remarks>
126  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
127  {
128  SubscriptionDataSource subscription;
129 
130  // We want to make just one call with all the data in backtesting mode.
131  // Also we want to make one call per second becasue of the API limit.
132  if (_backtestingFirstTimeCallOrLiveMode)
133  {
134  // Force the engine to wait at least 1000 ms between API calls.
135  IntrinioConfig.RateGate.WaitToProceed();
136 
137  // In backtesting mode, there is only one call at the beggining with all the data
138  _backtestingFirstTimeCallOrLiveMode = false || isLiveMode;
139  subscription = GetIntrinioSubscription(config, isLiveMode);
140  }
141  else
142  {
143  subscription = new SubscriptionDataSource("", SubscriptionTransportMedium.LocalFile);
144  }
145  return subscription;
146  }
147 
148  /// <summary>
149  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method,
150  /// and returns a new instance of the object
151  /// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
152  /// </summary>
153  /// <param name="config">Subscription data config setup object</param>
154  /// <param name="line">Line of the source document</param>
155  /// <param name="date">Date of the requested data</param>
156  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
157  /// <returns>
158  /// Instance of the T:BaseData object generated by this line of the CSV
159  /// </returns>
160  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
161  {
162  var obs = line.Split(',');
163  var time = DateTime.MinValue;
164  if (!DateTime.TryParseExact(obs[0], "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None,
165  out time)) return null;
166  var value = obs[1].ToDecimal();
167  return new IntrinioEconomicData
168  {
169  Symbol = config.Symbol,
170  Time = time,
171  EndTime = time + QuantConnect.Time.OneDay,
172  Value = value
173  };
174  }
175 
176  private static string GetStringForDataTransformation(IntrinioDataTransformation dataTransformation)
177  {
178  var item = "level";
179  switch (dataTransformation)
180  {
182  item = "change";
183  break;
184  case IntrinioDataTransformation.AnnualyRoc:
185  item = "yr_change";
186  break;
187  case IntrinioDataTransformation.CompoundedAnnualRoc:
188  item = "c_annual_roc";
189  break;
190  case IntrinioDataTransformation.AnnualyCCRoc:
191  item = "cc_annual_roc";
192  break;
193  case IntrinioDataTransformation.CCRoc:
194  item = "cc_roc";
195  break;
196  case IntrinioDataTransformation.Level:
197  item = "level";
198  break;
200  item = "log";
201  break;
203  item = "percent_change";
204  break;
205  case IntrinioDataTransformation.AnnualyPc:
206  item = "yr_percent_change";
207  break;
208  }
209  return item;
210  }
211 
212  private SubscriptionDataSource GetIntrinioSubscription(SubscriptionDataConfig config, bool isLiveMode)
213  {
214  // In Live mode, we only want the last observation, in backtesitng we need the data in ascending order.
215  var order = isLiveMode ? "desc" : "asc";
216  var item = GetStringForDataTransformation(_dataTransformation);
217  var url = $"{_baseUrl}identifier={config.Symbol.Value}&item={item}&sort_order={order}";
218  var byteKey = Encoding.ASCII.GetBytes($"{IntrinioConfig.User}:{IntrinioConfig.Password}");
219  var authorizationHeaders = new List<KeyValuePair<string, string>>
220  {
221  new KeyValuePair<string, string>("Authorization",
222  $"Basic ({Convert.ToBase64String(byteKey)})")
223  };
224 
225  return new SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile, FileFormat.Csv,
226  authorizationHeaders);
227  }
228  }
229 }