-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_data.py
More file actions
400 lines (322 loc) · 14.4 KB
/
Copy pathstock_data.py
File metadata and controls
400 lines (322 loc) · 14.4 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
# ============================================================================
# STOCK_DATA.PY
# ----------------------------------------------------------------------------
# Contains the 'StockData' class and associated methods for importing
# stock data using the 'yfinance' package, as well as calculating
# metrics and averaging data.
#
# ============================================================================
"""
# Imports.
import os
import warnings
import pickle
import pandas as pd
import yfinance as yf
from .utils.utils import (check_and_convert_value_to_list,
reduce_data_period, add_year_month_quarter)
from .analysis.moving_average import simple_moving_average, exp_moving_average
from .analysis.macd import macd
from .analysis.returns import (calculate_daily_returns, calculate_monthly_returns,
calculate_quarterly_returns, calculate_annual_returns,
dividend_summary)
from .plotting.plotting import macd_chart, line_chart
class StockData:
"""
Containing class for saving and updating stock data.
Args:
data_folder (str): Path to saved data, and where
data will be saved.
"""
def __init__(self, data_folder):
"""
Constructor.
"""
# Get list of folders.
self.root = data_folder
self.dir_list = next(os.walk(self.root))[1]
self.dir_list = [d.lower() for d in self.dir_list]
if not self.dir_list:
print(f"Folder '{self.root}' has no data.")
#else:
# print(f"Folder '{self.root}' the following stocks:\n\t|")
# for folder in self.dir_list:
# print(f"\t|-- {folder.upper()}")
def create_folder_path(self, folder):
"""
Returns the folder path for the specific stock.
"""
if self.root[-1] == "/":
return self.root + folder.lower()
else:
return self.root + "/" + folder.lower()
def add_columns_on_import(self, df_input):
"""
Centralized adding of useful columns.
Args:
df_input (pandas.DataFrame): The stock price data table.
Returns:
(pandas.DataFrame): The table with the new columns.
"""
# Copy to avoid overwrite.
df = df_input.copy()
# Add columns.
df['Date'] = df.index
df['FracDividends'] = df['Dividends'] / df['Close']
df = add_year_month_quarter(df, date_col='Date')
df = calculate_daily_returns(df, 'Close')
df = calculate_monthly_returns(df, 'Close')
df = calculate_quarterly_returns(df, 'Close')
df = calculate_annual_returns(df, 'Close')
return df
def add(self, labels):
"""
Adds the indicated stock or stocks to the StockData object
and writes the data to the data folder.
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to be
added.
"""
labels = check_and_convert_value_to_list(labels, str)
# Loop and add data.
for label in labels:
# Warn if the label exists.
if label in self.dir_list:
warnings.warn(f"Stock '{label.upper()}' is currently in the data library. Use StockData.update('{label}') to update the stock data.")
continue
# Construct the local data folder.
path = self.create_folder_path(label.lower())
# Download data.
tckr = yf.Ticker(label.upper())
data = self.add_columns_on_import(tckr.history(period='max'))
meta = {
'symbol': label.upper(),
'last_date': f"{str(data['Date'].max().year)}-{str(data['Date'].max().month).zfill(2)}-{str(data['Date'].max().day).zfill(2)}"
}
# Create the folder if it doesn't exist.
if not os.path.exists(path):
os.mkdir(path)
# Save data for folder.
data.to_pickle(f"{path}/data.pkl")
with open(f"{path}/meta.pkl", "wb") as fp:
pickle.dump(meta, fp, protocol=pickle.HIGHEST_PROTOCOL)
def update(self, labels=None):
"""
Update to the most recent pricing data and write to file.
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to be
updated. Default is None, which updates all stocks
found in the data directory.
"""
if not self.dir_list:
return
# Handle labels list if string or None.
if isinstance(labels, type(None)):
labels = [l.lower() for l in self.dir_list]
else:
labels = check_and_convert_value_to_list(labels, str)
# Loop and udpate data.
for label in labels:
# Warn if the label does not currently exist.
if label not in self.dir_list:
warnings.warn(f"Stock '{label.upper()}' is not in data library. Use StockData.add('{label}') to add the stock to the library.")
continue
# Construct the local data folder.
path = self.create_folder_path(label.lower())
# Load current data.
with open(f"{path}/meta.pkl", "rb") as fp:
meta = pickle.load(fp)
data = pd.read_pickle(f"{path}/data.pkl")
last_date = meta['last_date']
# Update data.
tckr = yf.Ticker(label.upper())
new_data = self.add_columns_on_import(tckr.history(start=last_date))
# Add new date to old data.
data = pd.concat([data, new_data], axis=0).drop_duplicates()
data.to_pickle(f"{path}/data.pkl")
meta['last_date'] = f"{str(data['Date'].max().year)}-{str(data['Date'].max().month).zfill(2)}-{str(data['Date'].max().day).zfill(2)}"
with open(f"{path}/meta.pkl", "wb") as fp:
pickle.dump(meta, fp, protocol=pickle.HIGHEST_PROTOCOL)
def add_and_update(self, labels):
"""
Updates existing data and adds data for new symbols.
Args:
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to be
updated or added. Default is None, which only updates
the existing stocks found in the directory.
"""
labels = check_and_convert_value_to_list(labels, str)
add_labels, update_labels = [], []
for label in labels:
if label.lower() in self.dir_list:
update_labels.append(label.lower())
else:
add_labels.append(label.lower())
# Add and update.
self.update(labels=update_labels)
self.add(labels=add_labels)
def load(self, labels=None):
"""
Load data from the data folder into the StockData object. Data is loaded
into a dictionary of pandas.DataFrames.
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to be
loaded. Default is None, which will load all stock data
found in the data directory.
"""
# Initialize empty container.
self.d_data = {}
# Handle default case.
if isinstance(labels, type(None)):
labels = [l.lower() for l in self.dir_list]
else:
labels = check_and_convert_value_to_list(labels, str)
# Loop and populate object data.
for label in labels:
# Ensure data is in library, instruct otherwise.
if label.lower() not in self.dir_list:
print(f"No stock data found for '{label.upper()}'. Use the '.add()' method to add a new stock symbol.")
continue
self.d_data[label.lower()] = pd.read_pickle(self.create_folder_path(label.lower())+"/data.pkl")
def get_object_data(self):
"""
Returns the dictionary containing all object data.
"""
try:
return self.d_data
except:
print("StockData object does not has any data loaded.")
def add_moving_average(self, labels=None, column='Close', windows=None, method='sma'):
"""
Calculates the moving average for a given label and column.
Columns are added to each stock DataFrame as SMAX or EMAX,
where X is the window size in days.
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to have
the moving average calculated. Default is None, which
will calculate the moving average for all labels.
column (str): The column from the stock DataFrame to use
to calculate the moving average. Accepted values are 'Open',
'Close', 'High', 'Low'.
windows (int, list): The window for moving average calculated, in days.
If a list is provided, moving averages will be calculated
for each of them.
method (str): Type of moving average to calculate. Accepted values
are 'sma' (simple moving average), 'ema' (exponential moving
average). Default is 'ema'.
"""
# Handle errors.
if not self.d_data:
raise ValueError("There is no data loaded into this StockData object.")
if isinstance(windows, type(None)):
raise ValueError("At least one value must be passed to argument 'windows'.")
if method not in ['sma', 'ema']:
raise ValueError("Argument 'method' must be one of: 'sma', 'ema'.")
# Handle default case.
if isinstance(labels, type(None)):
labels = [l.lower() for l in self.dir_list]
else:
labels = check_and_convert_value_to_list(labels, str)
# Handle moving averages.
d_ma = {'sma': simple_moving_average,
'ema': exp_moving_average}
for label in labels:
self.d_data[label] = d_ma[method](self.d_data[label], column, windows)
def add_macd(self, labels=None):
"""
Adds the MACD and 12- and 26-period EMA to each stock symbol data.
Args:
labels (str, list): A single string or list of strings
of the symbols indicating the stock or stocks to have
the moving average calculated. Default is None, which
will calculate the moving average for all labels.
"""
# Handle default case.
if isinstance(labels, type(None)):
labels = [l.lower() for l in self.dir_list]
else:
labels = check_and_convert_value_to_list(labels, str)
for label in labels:
self.d_data[label] = macd(self.d_data[label], 'Close')
def plot_single_analysis(self, symbol, period='max', width=900, height=400):
"""
Plots candlestick and MACD for a single stock in the data.
Args:
symbol (str): A stock symbol to plot.
period (str): The time period to plot, in days ('5d'),
months ('6m'), years ('4y') or 'max'.
width (int): Chart width in pixels.
height (int): Chart height in pixels.
Returns:
(None)
Raises:
ValueError - If stock symbol is not found in the object data.
"""
# Handle missing stock data symbol.
if symbol.lower() not in self.dir_list:
raise ValueError(f"Symbol {symbol} not found in the StockData object.")
# Add MACD if missing from data.
if 'MACD' not in self.d_data[symbol.lower()].columns:
self.add_macd(labels=symbol.lower())
# Plot graph.
macd_chart(
source=reduce_data_period(self.d_data[symbol.lower()], 'Date', period),
width=width,
height=height
).display()
def plot_compare_multiple(self, labels, data_col='Close', period='max',
width=900, height=400):
"""
Plots multiple stocks on the same chart.
Args:
labels (str, list): Stock symbols to plot on the same chart.
data_col (str): The column with values to plot.
period (str): The time period to plot, in days ('5d'),
months ('6m'), years ('4y') or 'max'.
width (int): Chart width in pixels.
height (int): Chart height in pixels.
Returns:
(None)
"""
line_chart(
d_data=self.d_data, x_col='Date', y_col=data_col, labels=labels,
period=period, width=width, height=height
).display()
def dividend_summary(self, labels=None, div_type='FracDividends'):
"""
Summarizes the dividends in the available data.
"""
df_list = []
# Handle default case.
if isinstance(labels, type(None)):
labels = [l.lower() for l in self.dir_list]
else:
labels = check_and_convert_value_to_list(labels, str)
for label in labels:
df_list.append(dividend_summary(self.d_data[label], label, div_type))
df_summary = pd.DataFrame(df_list)
return df_summary
def get_stock_price(self, label, date=None, price_type='Close'):
"""
Returns the price of the stock at the given date.
Args:
label (str): The stock symbol.
date (str): The date for which to get the price, 'YYYY-MM-DD'.
Default is None, which gets the latest price.
price_type (str): One of 'Close', 'Open', 'High', 'Low'.
Returns:
(float): The price of the requested stock.
"""
data = self.d_data[label.lower()].copy()
if date:
date = pd.to_datetime(date, format='%Y-%m-%d')
else:
date = data['Date'].max()
return data.loc[data['Date']==date, price_type].iloc[0]