11import datetime
22import json
3+ import os
34
45import pandas as pd
56
@@ -40,10 +41,24 @@ class IEXDailyReader(_DailyBaseReader):
4041 Number of symbols to download consecutively before intiating pause.
4142 session : Session, default None
4243 requests.sessions.Session instance to be used
44+ api_key: str
45+ IEX Cloud Secret Token
4346 """
4447
4548 def __init__ (self , symbols = None , start = None , end = None , retry_count = 3 ,
46- pause = 0.1 , session = None , chunksize = 25 ):
49+ pause = 0.1 , session = None , chunksize = 25 , api_key = None ):
50+ if api_key is None :
51+ api_key = os .getenv ('IEX_API_KEY' )
52+ if not api_key or not isinstance (api_key , str ):
53+ raise ValueError ('The IEX Cloud API key must be provided either '
54+ 'through the api_key variable or through the '
55+ ' environment variable IEX_API_KEY' )
56+ # Support for sandbox environment (testing purposes)
57+ if os .getenv ("IEX_SANDBOX" ) == "enable" :
58+ self .sandbox = True
59+ else :
60+ self .sandbox = False
61+ self .api_key = api_key
4762 super (IEXDailyReader , self ).__init__ (symbols = symbols , start = start ,
4863 end = end , retry_count = retry_count ,
4964 pause = pause , session = session ,
@@ -52,7 +67,10 @@ def __init__(self, symbols=None, start=None, end=None, retry_count=3,
5267 @property
5368 def url (self ):
5469 """API URL"""
55- return 'https://api.iextrading.com/1.0/stock/market/batch'
70+ if self .sandbox is True :
71+ return 'https://sandbox.iexapis.com/stable/stock/market/batch'
72+ else :
73+ return 'https://cloud.iexapis.com/stable/stock/market/batch'
5674
5775 @property
5876 def endpoint (self ):
@@ -69,20 +87,24 @@ def _get_params(self, symbol):
6987 "symbols" : symbolList ,
7088 "types" : self .endpoint ,
7189 "range" : chart_range ,
90+ "token" : self .api_key
7291 }
7392 return params
7493
7594 def _range_string_from_date (self ):
7695 delta = relativedelta (self .start , datetime .datetime .now ())
77- if 2 <= (delta .years * - 1 ) <= 5 :
96+ years = (delta .years * - 1 )
97+ if 5 <= years <= 15 :
98+ return "max"
99+ if 2 <= years < 5 :
78100 return "5y"
79- elif 1 <= ( delta . years * - 1 ) <= 2 :
101+ elif 1 <= years < 2 :
80102 return "2y"
81- elif 0 <= ( delta . years * - 1 ) < 1 :
103+ elif 0 <= years < 1 :
82104 return "1y"
83105 else :
84106 raise ValueError (
85- "Invalid date specified. Must be within past 5 years." )
107+ "Invalid date specified. Must be within past 15 years." )
86108
87109 def read (self ):
88110 """Read data"""
0 commit comments