- made a class

git-svn-id: http://moon:8086/svn/projects/Stock@305 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-12-13 20:28:19 +00:00
parent a5ce8ff53f
commit f2f6e7e536
+82 -53
View File
@@ -20,6 +20,40 @@ import os
# https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1
key = '0UO7Z2MVZ2YSQSVE'
thresh_macd = 0.5
show_range_days = 20
titles = [
'AAPL',
'XLNX',
'QCOM',
'DPW.DE',
'CSCO',
'AIR',
'BA',
'NVDA',
'MSFT',
'DIS',
'NFLX',
'OHB.DE',
'ERCA.DE',
'VAR1.DE',
'HD',
'AMZN',
'GOOGL',
'ZIL2.DE',
'SIS.DE',
'GFT.DE',
'AMD.DE',
'PANW',
'RIB.DE',
'WAF.DE',
'EVT.DE',
'VOW.DE',
'AMS.SW',
'I',
'OQ3.F']
def ema(data, key, alpha=0.5, ic=None):
result = []
@@ -95,28 +129,21 @@ def bollinger(data, window_days, f=2):
result['mid'] = mid
return result
data = {}
titles = []
titles.append('AAPL')
titles.append('XLNX')
titles.append('QCOM')
titles.append('DPW.DE')
titles.append('CSCO')
titles.append('AIR')
titles.append('NVDA')
show_title = 'XLNX'
show_range_days = 20
class Symbol(object):
def __init__(self, name):
self.name = name
self.ax = None
self.fig = None
self.k_euro = 1/1.11
pd.DatetimeIndex
def fetch(title):
filename = title + '.h5'
def fetch(self):
filename = self.name.replace('.','_') + '.h5'
fetch = False
today = dt.date.today()
try:
hdf = pd.HDFStore(filename, 'r')
hdf.close()
except:
fetch = True
@@ -128,53 +155,48 @@ def fetch(title):
fetch = True
if fetch:
print ("Fetching \"{}\"".format(title))
print ("Fetching \"{}\"".format(self.name))
# Get stock price via data reader
start = dt.datetime(2019,1,1)
end = dt.date.today()
data = web.DataReader(title, "av-daily", start, end, api_key=key)
hdf = pd.HDFStore(title + '.h5')
hdf[title] = data
else:
hdf = pd.HDFStore(filename, 'r')
data = hdf[title]
data = web.DataReader(self.name, "av-daily", start, end, api_key=key)
hdf = pd.HDFStore(filename)
hdf[self.name] = data
self.data_full = hdf[self.name]*self.k_euro
hdf.close()
def plot(ax, fig, data):
def __plot(self, data):
dates = data.index
data.plot()
def hover(event):
if event.inaxes == ax:
if event.inaxes == self.ax:
x_idx = int(event.xdata)
ax.format_xdata = lambda x: dates[x_idx]
ax.format_ydata = lambda y: '{:.2f}'.format(data[x_idx])
self.ax.format_xdata = lambda x: dates[x_idx]
self.ax.format_ydata = lambda y: '{:.2f}'.format(data[x_idx])
fig.canvas.mpl_connect("motion_notify_event", hover)
self.fig.canvas.mpl_connect("motion_notify_event", hover)
def show(title):
def analyze(self):
self.data_full['ema'] = ema(self.data_full, key='close', alpha=0.75)
self.data_full['macd'] = macd(self.data_full, key='close')
self.data_full['sma'] = sma(self.data_full, key='close', window_days=30)
self.boll_full = bollinger(self.data_full, window_days=30)
k_euro = 1/1.11
_macd = self.data_full['macd'][len(self.data_full)-1]
return {'macd' : _macd}
filename = title + '.h5'
hdf = pd.HDFStore(filename, 'r')
data_full = hdf[title]*k_euro
def show(self, figNum=1):
data = self.data_full[len(self.data_full)-1 - show_range_days:]
boll = self.boll_full[len(self.boll_full)-1 - show_range_days:]
data_full['ema'] = ema(data_full, key='close', alpha=0.75)
data_full['macd'] = macd(data_full, key='close')
data_full['sma'] = sma(data_full, key='close', window_days=30)
boll_full = bollinger(data_full, window_days=30)
hdf.close()
data = data_full[len(data_full)-1 - show_range_days:]
boll = boll_full[len(boll_full)-1 - show_range_days:]
fig = plt.figure(1)
ax = plt.subplot(311)
plot(ax, fig, data['close'])
plot(ax, fig, data['ema'])
plot(ax, fig, data['sma'])
plt.title(title)
self.fig = plt.figure(figNum)
self.ax = plt.subplot(311)
self.__plot(data['close'])
self.__plot(data['ema'])
self.__plot(data['sma'])
plt.title(self.name)
plt.legend()
plt.grid()
@@ -190,11 +212,18 @@ def show(title):
plt.legend()
plt.grid()
figNum = 1
for title in titles:
sym = Symbol(title)
sym.fetch()
result = sym.analyze()
if result['macd'] > thresh_macd:
sym.show(figNum)
figNum += 1
plt.show()
for title in titles:
fetch(title)
show(show_title)