-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathdataDownloader.py
More file actions
executable file
·264 lines (185 loc) · 9.47 KB
/
dataDownloader.py
File metadata and controls
executable file
·264 lines (185 loc) · 9.47 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# coding=utf-8
"""
Goal: Downloading financial data (related to stock markets) from diverse sources
(Alpha Vantage, Yahoo Finance).
Authors: Thibaut Théate and Damien Ernst
Institution: University of Liège
"""
###############################################################################
################################### Imports ###################################
###############################################################################
import pandas as pd
import pandas_datareader as pdr
import requests
from io import StringIO
###############################################################################
############################## Class AlphaVantage #############################
###############################################################################
class AlphaVantage:
"""
GOAL: Downloading stock market data from the Alpha Vantage API. See the
AlphaVantage documentation for more information.
VARIABLES: - link: Link to the Alpha Vantage website.
- apikey: Key required to access the Alpha Vantage API.
- datatype: 'csv' or 'json' data format.
- outputsize: 'full' or 'compact' (only 100 time steps).
- data: Pandas dataframe containing the stock market data.
METHODS: - __init__: Object constructor initializing some variables.
- getDailyData: Retrieve daily stock market data.
- getIntradayData: Retrieve intraday stock market data.
- processDataframe: Process the dataframe to homogenize the format.
"""
def __init__(self):
"""
GOAL: Object constructor initializing the class variables.
INPUTS: /
OUTPUTS: /
"""
self.link = 'https://www.alphavantage.co/query'
self.apikey = 'APIKEY'
self.datatype = 'csv'
self.outputsize = 'full'
self.data = pd.DataFrame()
def getDailyData(self, marketSymbol, startingDate, endingDate):
"""
GOAL: Downloading daily stock market data from the Alpha Vantage API.
INPUTS: - marketSymbol: Stock market symbol.
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
OUTPUTS: - data: Pandas dataframe containing the stock market data.
"""
# Send an HTTP request to the Alpha Vantage API
payload = {'function': 'TIME_SERIES_DAILY_ADJUSTED', 'symbol': marketSymbol,
'outputsize': self.outputsize, 'datatype': self.datatype,
'apikey': self.apikey}
response = requests.get(self.link, params=payload)
# Process the CSV file retrieved
csvText = StringIO(response.text)
data = pd.read_csv(csvText, index_col='timestamp')
# Process the dataframe to homogenize the output format
self.data = self.processDataframe(data)
if(startingDate != 0 and endingDate != 0):
self.data = self.data.loc[startingDate:endingDate]
return self.data
def getIntradayData(self, marketSymbol, startingDate, endingDate, timePeriod=60):
"""
GOAL: Downloading intraday stock market data from the Alpha Vantage API.
INPUTS: - marketSymbol: Stock market symbol.
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- timePeriod: Time step of the stock market data (in seconds).
OUTPUTS: - data: Pandas dataframe containing the stock market data.
"""
# Round the timePeriod value to the closest accepted value
possiblePeriods = [1, 5, 15, 30, 60]
timePeriod = min(possiblePeriods, key=lambda x:abs(x-timePeriod))
# Send a HTTP request to the AlphaVantage API
payload = {'function': 'TIME_SERIES_INTRADAY', 'symbol': marketSymbol,
'outputsize': self.outputsize, 'datatype': self.datatype,
'apikey': self.apikey, 'interval': str(timePeriod)+'min'}
response = requests.get(self.link, params=payload)
# Process the CSV file retrieved
csvText = StringIO(response.text)
data = pd.read_csv(csvText, index_col='timestamp')
# Process the dataframe to homogenize the output format
self.data = self.processDataframe(data)
if(startingDate != 0 and endingDate != 0):
self.data = self.data.loc[startingDate:endingDate]
return self.data
def processDataframe(self, dataframe):
"""
GOAL: Process a downloaded dataframe to homogenize the output format.
INPUTS: - dataframe: Pandas dataframe to be processed.
OUTPUTS: - dataframe: Processed Pandas dataframe.
"""
# Reverse the order of the dataframe (chronological order)
dataframe = dataframe[::-1]
# Remove useless columns
dataframe['close'] = dataframe['adjusted_close']
del dataframe['adjusted_close']
del dataframe['dividend_amount']
del dataframe['split_coefficient']
# Adapt the dataframe index and column names
dataframe.index.names = ['Timestamp']
dataframe = dataframe.rename(index=str, columns={"open": "Open",
"high": "High",
"low": "Low",
"close": "Close",
"volume": "Volume"})
# Adjust the format of the index values
dataframe.index = dataframe.index.map(pd.Timestamp)
return dataframe
###############################################################################
########################### Class YahooFinance ################################
###############################################################################
class YahooFinance:
"""
GOAL: Downloading stock market data from the Yahoo Finance API. See the
pandas.datareader documentation for more information.
VARIABLES: - data: Pandas dataframe containing the stock market data.
METHODS: - __init__: Object constructor initializing some variables.
- getDailyData: Retrieve daily stock market data.
- processDataframe: Process a dataframe to homogenize the
output format.
"""
def __init__(self):
"""
GOAL: Object constructor initializing the class variables.
INPUTS: /
OUTPUTS: /
"""
self.data = pd.DataFrame()
def getDailyData(self, marketSymbol, startingDate, endingDate):
"""
GOAL: Downloding daily stock market data from the Yahoo Finance API.
INPUTS: - marketSymbol: Stock market symbol.
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
OUTPUTS: - data: Pandas dataframe containing the stock market data.
"""
data = pdr.data.DataReader(marketSymbol, 'yahoo', startingDate, endingDate)
self.data = self.processDataframe(data)
return self.data
def processDataframe(self, dataframe):
"""
GOAL: Process a downloaded dataframe to homogenize the output format.
INPUTS: - dataframe: Pandas dataframe to be processed.
OUTPUTS: - dataframe: Processed Pandas dataframe.
"""
# Remove useless columns
dataframe['Close'] = dataframe['Adj Close']
del dataframe['Adj Close']
# Adapt the dataframe index and column names
dataframe.index.names = ['Timestamp']
dataframe = dataframe[['Open', 'High', 'Low', 'Close', 'Volume']]
return dataframe
###############################################################################
############################# Class CSVHandler ################################
###############################################################################
class CSVHandler:
"""
GOAL: Converting "Pandas dataframe" <-> "CSV file" (bidirectional).
VARIABLES: /
METHODS: - dataframeToCSV: Saving a dataframe into a CSV file.
- CSVToDataframe: Loading a CSV file into a dataframe.
"""
def dataframeToCSV(self, name, dataframe):
"""
GOAL: Saving a dataframe into a CSV file.
INPUTS: - name: Name of the CSV file.
- dataframe: Pandas dataframe to be saved.
OUTPUTS: /
"""
path = name + '.csv'
dataframe.to_csv(path)
def CSVToDataframe(self, name):
"""
GOAL: Loading a CSV file into a dataframe.
INPUTS: - name: Name of the CSV file.
OUTPUTS: - dataframe: Pandas dataframe loaded.
"""
path = name + '.csv'
return pd.read_csv(path,
header=0,
index_col='Timestamp',
parse_dates=True)