1
1
import datetime
2
2
import json
3
+ import os
3
4
4
5
import pandas as pd
5
6
@@ -40,10 +41,24 @@ class IEXDailyReader(_DailyBaseReader):
40
41
Number of symbols to download consecutively before intiating pause.
41
42
session : Session, default None
42
43
requests.sessions.Session instance to be used
44
+ api_key: str
45
+ IEX Cloud Secret Token
43
46
"""
44
47
45
48
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
47
62
super (IEXDailyReader , self ).__init__ (symbols = symbols , start = start ,
48
63
end = end , retry_count = retry_count ,
49
64
pause = pause , session = session ,
@@ -52,7 +67,10 @@ def __init__(self, symbols=None, start=None, end=None, retry_count=3,
52
67
@property
53
68
def url (self ):
54
69
"""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'
56
74
57
75
@property
58
76
def endpoint (self ):
@@ -69,20 +87,24 @@ def _get_params(self, symbol):
69
87
"symbols" : symbolList ,
70
88
"types" : self .endpoint ,
71
89
"range" : chart_range ,
90
+ "token" : self .api_key
72
91
}
73
92
return params
74
93
75
94
def _range_string_from_date (self ):
76
95
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 :
78
100
return "5y"
79
- elif 1 <= ( delta . years * - 1 ) <= 2 :
101
+ elif 1 <= years < 2 :
80
102
return "2y"
81
- elif 0 <= ( delta . years * - 1 ) < 1 :
103
+ elif 0 <= years < 1 :
82
104
return "1y"
83
105
else :
84
106
raise ValueError (
85
- "Invalid date specified. Must be within past 5 years." )
107
+ "Invalid date specified. Must be within past 15 years." )
86
108
87
109
def read (self ):
88
110
"""Read data"""
0 commit comments