-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoo_finance.py
65 lines (48 loc) · 1.75 KB
/
yahoo_finance.py
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
import yfinance as yf
import pandas as pd
import ssl
import datetime
import time
import seaborn as sns
import matplot.pyplot as plt
import numpy as np
import warnings
from stocks import *
warnings.simplefilter(action='ignore', category=FutureWarning)
ssl._create_default_https_context = ssl._create_unverified_context
# documentation here: https://github.com/ranaroussi/yfinance
def include_keys(dictionary, keys):
"""Filters a dict by only including certain keys."""
key_set = set(keys) & set(dictionary.keys())
return {key: dictionary[key] for key in key_set}
def get_data(ticker):
comp = yf.Ticker(ticker)
print(comp)
# get stock info
info = comp.info
hist_div = comp.dividends
mean_dividend = np.mean(comp.dividends)
keep = ['regularMarketPreviousClose', 'industry', 'lastDividendValue', 'lastDividendDate',
'fiveYearAvgDividendYield', 'dividendRate']
temp_list = []
filtered = include_keys(info, keep)
filtered.update({'ticker': ticker})
temp_list.append(filtered)
# filtered = filtered.update({'ticker': ticker})
df = pd.DataFrame(temp_list)
# df['lastDividendDate'] = datetime.datetime.fromtimestamp(df['lastDividendDate']).isoformat()
# df['lastDividendDate'] = pd.to_datetime(df['lastDividendDate'], format='%Y-%m-%dT%H:%M:%S')
return df, info, hist_div, mean_dividend
for i, ticker in enumerate(tech):
df, info, hist_div, mean_dividend = get_data(ticker)
if len(hist_div) == 0:
print("No dividends to graph")
pass
else:
# plt.figure(i)
# ax = sns.distplot(hist_div, bins = 50)
# ax.set_title("Stock " + ticker)
fig = plt.figure()
ax = plt.axes()
ax.plot(hist_div)
ax.set_title("Stock " + ticker)