-
Notifications
You must be signed in to change notification settings - Fork 9
/
FredApi.cs
157 lines (132 loc) · 5.52 KB
/
FredApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using Newtonsoft.Json;
using QuantConnect.Data;
using QuantConnect.Data.UniverseSelection;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace QuantConnect.DataSource
{
#pragma warning disable 1591
public class FredApi : BaseData
{
/// <summary>
/// Data source ID
/// </summary>
public static int DataSourceId { get; } = 2011;
[JsonProperty("realtime_start")]
public string RealtimeStart { get; set; }
[JsonProperty("realtime_end")]
public string RealtimeEnd { get; set; }
[JsonProperty("observation_start")]
public string ObservationStart { get; set; }
[JsonProperty("observation_end")]
public string ObservationEnd { get; set; }
[JsonProperty("units")]
public string Units { get; set; }
[JsonProperty("output_type")]
public int OutputType { get; set; }
[JsonProperty("file_type")]
public string FileType { get; set; }
[JsonProperty("order_by")]
public string OrderBy { get; set; }
[JsonProperty("sort_order")]
public string SortOrder { get; set; }
[JsonProperty("count")]
public int Count { get; set; }
[JsonProperty("offset")]
public int Offset { get; set; }
[JsonProperty("limit")]
public int Limit { get; set; }
[JsonProperty("observations")]
public IList<Observation> Observations { get; set; }
#pragma warning restore 1591
/// <summary>
/// Gets the FRED API token.
/// </summary>
public static string AuthCode { get; private set; } = string.Empty;
/// <summary>
/// Returns true if the FRED API token has been set.
/// </summary>
public static bool IsAuthCodeSet { get; private set; }
/// <summary>
/// Sets the EIA API token.
/// </summary>
/// <param name="authCode">The EIA API token</param>
public static void SetAuthCode(string authCode)
{
if (string.IsNullOrWhiteSpace(authCode)) return;
AuthCode = authCode;
IsAuthCodeSet = true;
}
/// <summary>
/// Return the URL string source of the file. This will be converted to a stream
/// </summary>
/// <param name="config">Configuration object</param>
/// <param name="date">Date of this source file</param>
/// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
/// <returns>
/// String URL of source file.
/// </returns>
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
return new SubscriptionDataSource(
$"https://api.stlouisfed.org/fred/series/observations?file_type=json&observation_start=1998-01-01&api_key={AuthCode}&series_id={config.Symbol}",
SubscriptionTransportMedium.Rest,
FileFormat.UnfoldingCollection);
}
/// <summary>
/// Readers the specified configuration.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="content">The content.</param>
/// <param name="date">The date.</param>
/// <param name="isLiveMode">if set to <c>true</c> [is live mode].</param>
/// <returns></returns>
public override BaseData Reader(SubscriptionDataConfig config, string content, DateTime date, bool isLiveMode)
{
var series = JsonConvert.DeserializeObject<FredApi>(content).Observations;
var objectList = new List<FredApi>();
foreach (var observation in series)
{
decimal value;
if (Parse.TryParse(observation.Value, NumberStyles.Any, out value))
{
objectList.Add(new FredApi
{
Symbol = config.Symbol,
Time = observation.Date,
EndTime = observation.Date + TimeSpan.FromDays(1),
Value = value
});
}
}
return new BaseDataCollection(date, config.Symbol, objectList);
}
public class Observation
{
[JsonProperty("realtime_start")]
public string RealtimeStart { get; set; }
[JsonProperty("realtime_end")]
public string RealtimeEnd { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
}
}