@@ -23,6 +23,97 @@ def get_tiingo_symbols():
2323 return pd .read_csv (url )
2424
2525
26+ class TiingoIEXHistoricalReader (_BaseReader ):
27+ """
28+ Historical data from Tiingo on equities, ETFs and mutual funds,
29+ with re-sampling capability. This query is limited to the last
30+ 1,000 bars based in the endDate. So the startDate is moved if
31+ it goes past the limit.
32+
33+ Parameters
34+ ----------
35+ symbols : {str, List[str]}
36+ String symbol of like of symbols
37+ start : str, (defaults to '1/1/2010')
38+ Starting date, timestamp. Parses many different kind of date
39+ representations (e.g., 'JAN-01-2010', '1/1/10', 'Jan, 1, 1980')
40+ end : str, (defaults to today)
41+ Ending date, timestamp. Same format as starting date.
42+ retry_count : int, default 3
43+ Number of times to retry query request.
44+ pause : float, default 0.1
45+ Time, in seconds, of the pause between retries.
46+ session : Session, default None
47+ requests.sessions.Session instance to be used
48+ freq : {str, None}
49+ Re-sample frequency. Format is # + (min/hour); e.g. "15min" or "4hour".
50+ If no value is provided, defaults to 5min. The minimum value is "1min".
51+ Units in minutes (min) and hours (hour) are accepted.
52+ api_key : str, optional
53+ Tiingo API key . If not provided the environmental variable
54+ TIINGO_API_KEY is read. The API key is *required*.
55+ """
56+
57+ def __init__ (self , symbols , start = None , end = None , retry_count = 3 , pause = 0.1 ,
58+ timeout = 30 , session = None , freq = None , api_key = None ):
59+ super ().__init__ (symbols , start , end , retry_count , pause , timeout ,
60+ session , freq )
61+
62+ if isinstance (self .symbols , str ):
63+ self .symbols = [self .symbols ]
64+ self ._symbol = ''
65+ if api_key is None :
66+ api_key = os .getenv ('TIINGO_API_KEY' )
67+ if not api_key or not isinstance (api_key , str ):
68+ raise ValueError ('The tiingo API key must be provided either '
69+ 'through the api_key variable or through the '
70+ 'environmental variable TIINGO_API_KEY.' )
71+ self .api_key = api_key
72+ self ._concat_axis = 0
73+
74+ @property
75+ def url (self ):
76+ """API URL"""
77+ _url = 'https://api.tiingo.com/iex/{ticker}/prices'
78+ return _url .format (ticker = self ._symbol )
79+
80+ @property
81+ def params (self ):
82+ """Parameters to use in API calls"""
83+ return {'startDate' : self .start .strftime ('%Y-%m-%d' ),
84+ 'endDate' : self .end .strftime ('%Y-%m-%d' ),
85+ 'resampleFreq' : self .freq ,
86+ 'format' : 'json' }
87+
88+ def _get_crumb (self , * args ):
89+ pass
90+
91+ def _read_one_data (self , url , params ):
92+ """ read one data from specified URL """
93+ headers = {'Content-Type' : 'application/json' ,
94+ 'Authorization' : 'Token ' + self .api_key }
95+ out = self ._get_response (url , params = params , headers = headers ).json ()
96+ return self ._read_lines (out )
97+
98+ def _read_lines (self , out ):
99+ df = pd .DataFrame (out )
100+ df ['symbol' ] = self ._symbol
101+ df ['date' ] = pd .to_datetime (df ['date' ])
102+ df = df .set_index (['symbol' , 'date' ])
103+ return df
104+
105+ def read (self ):
106+ """Read data from connector"""
107+ dfs = []
108+ for symbol in self .symbols :
109+ self ._symbol = symbol
110+ try :
111+ dfs .append (self ._read_one_data (self .url , self .params ))
112+ finally :
113+ self .close ()
114+ return pd .concat (dfs , self ._concat_axis )
115+
116+
26117class TiingoDailyReader (_BaseReader ):
27118 """
28119 Historical daily data from Tiingo on equities, ETFs and mutual funds
0 commit comments