-
Notifications
You must be signed in to change notification settings - Fork 6
/
stock.py
159 lines (121 loc) · 5.64 KB
/
stock.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
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
# -*- coding: utf-8 -*-
"""
=================================
myInvestor-toolkit startup script
=================================
"""
import datetime as dt
import os
import pandas as pd
from fundamental import DividendYield
from source import YahooFinanceSource
class StockAnalysis:
"""
Stock analysis.
"""
_TICKER_FILE = 'dataset/ticker.csv'
_CURRENT_PRICE_FILE = 'dataset/current_price.csv'
def fund_update_dividend_yields_for_exchange(self, exchange):
"""
Update existing dividend yield file.
:param exchange: Exchange symbol.
:return: None
"""
print('Updating dividend yields for {}'.format(exchange))
df_dividend_data = pd.read_csv('dataset/{}_dividend_yields.csv'.format(exchange), dtype=str)
# Check if dividend file exists
def fund_get_dividend_yields_for_exchange(self, exchange, skip_if_exist=True):
"""
Get dividends yields for the exchange.
:param exchange: Exchange symbol.
:param skip_if_exist: Skip if the dividend yields already in the file.
:return: True on success, otherwise return False.
"""
df_stocks = pd.read_csv(self._TICKER_FILE, dtype=str)
df_stocks = df_stocks.loc[df_stocks['Exchange'] == exchange]
df_stocks = df_stocks.set_index(['Ticker'])
count = len(df_stocks)
index = 1
if (count == 0): return
dividend_file = 'dataset/{}_dividend_yields.csv'.format(exchange)
if (os.path.exists(dividend_file)):
df_dividend_data = pd.read_csv(dividend_file)
df_dividend_data = df_dividend_data.set_index(['symbol', 'date'])
else:
df_dividend_data = pd.DataFrame()
for ticker, row in df_stocks.iterrows():
print('{} / {} - Getting dividend yields for {}'.format(index, count, ticker))
index = index + 1
# Skip if already exist
if (skip_if_exist):
if not df_dividend_data.empty and ticker in df_dividend_data.index:
print('Skipping {}'.format(ticker))
continue
try:
dividend_yield = DividendYield(ticker)
stock_dividends = dividend_yield.get_history()
for symbol, values in stock_dividends.items():
prices = values['prices']
dividend_list = []
for dividend in prices:
dividend_list.append(pd.Series(dividend))
if len(dividend_list) > 0:
df_dividend = pd.DataFrame(dividend_list)
df_dividend['symbol'] = symbol
df_dividend = df_dividend.set_index(['symbol', 'date'])
if (df_dividend_data.empty):
df_dividend_data = df_dividend
else:
df_dividend_data = df_dividend_data.combine_first(df_dividend)
df_dividend_data.to_csv(dividend_file, encoding='utf-8')
except Exception as e:
print('Ooops...error with {} - {}'.format(ticker, str(e)))
continue
return True
return False
def fund_get_stock_financials(self, ticker_file, price_file_name=_CURRENT_PRICE_FILE):
"""
Getting current prices into a file.
:param ticker_file: Ticket file.
:param price_file_name: Output price file name.
:return: True on success, otherwise return False.
"""
df_stocks = pd.read_csv(ticker_file, dtype=str)
tickers = df_stocks.symbol.unique()
current = 1
df_all_stocks_summaries = pd.DataFrame()
for ticker in tickers:
print('{} - Getting current info for {}.'.format(current, ticker))
current = current + 1
yahoo_finance_source = YahooFinanceSource(ticker)
stock_summary_data = yahoo_finance_source.get_stock_summary_data()
stock_summary_data[ticker]['symbol'] = ticker
df_stock_summary = pd.DataFrame([pd.Series(stock_summary_data[ticker])])
if (df_all_stocks_summaries.empty):
df_all_stocks_summaries = df_stock_summary
else:
df_all_stocks_summaries = df_all_stocks_summaries.append(df_stock_summary)
df_all_stocks_summaries.to_csv(price_file_name, encoding='utf-8', index=False)
def download_stock_history_to_csv(self, ticker, start_date, end_date, csv_file_name):
yahoo_finance_source = YahooFinanceSource(ticker)
historical_stock_prices = yahoo_finance_source.get_historical_stock_data(start_date, end_date, 'daily')
print(historical_stock_prices)
#df_stock_prices = pd.DataFrame([pd.Series(historical_stock_prices[ticker]['prices'])])
#print(df_stock_prices.head(10))
def main():
"""
Main script.
"""
stock_analysis = StockAnalysis()
# stock_analysis.fund_get_dividend_yields_for_exchange('KLS')
# stock_analysis.fund_get_stock_financials(ticker_file='dataset/KLS_selected_equities.csv',
# price_file_name='dataset/KLS_stock_financials.csv')
# yahoo_finance_source = YahooFinanceSource("6742.KL")
# stock_summary_data = yahoo_finance_source.get_stock_summary_data()
# print(stock_summary_data)
year_interval = 1
start_date = dt.date(dt.date.today().year - year_interval, 1, 1).strftime('%Y-%m-%d')
end_date = dt.datetime.today().strftime('%Y-%m-%d')
stock_analysis.download_stock_history_to_csv("6742.KL", start_date, end_date, "dataset/6742.KL")
if __name__ == "__main__":
main()