- refactored

git-svn-id: http://moon:8086/svn/projects/Stock@321 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-12-16 19:55:38 +00:00
parent 045ab717d2
commit f7bcd1eeb4
+55 -51
View File
@@ -23,31 +23,32 @@ import os
key = '0UO7Z2MVZ2YSQSVE' key = '0UO7Z2MVZ2YSQSVE'
thresh_macd = 1.5 thresh_macd = 1.5
show_range_days = 20 show_range_days = 20
show_title = ['WDI.DE', 'QCOM', 'VAR1.DE', 'MOR.DE', 'CSCO', 'OHB.DE'] show_symbols = ['ITMPF', 'PLUG', 'MOR.DE', 'CSCO', 'ERCA.DE', 'AVGO', 'DIS']
#show_symbols = []
k_euro = 1 / 1.11 k_euro = 1 / 1.11
N_poly = 7 N_poly = 7
ema_alpha = 0.75 ema_alpha = 0.75
sma_days = 10 sma_days = 10
titles = { symbols = {
'WDI.DE' : {'currency' : ''}, 'WDI.DE' : {'name' : 'WireCard', 'currency' : ''},
'AAPL' : {'currency' : '$'}, 'AAPL' : {'name' : 'Apple', 'currency' : '$'},
'XLNX' : {'currency' : '$'}, 'XLNX' : {'name' : 'Xilinx', 'currency' : '$'},
'QCOM' : {'currency' : '$'}, 'QCOM' : {'name' : 'Qualcomm', 'currency' : '$'},
'DPW.DE' : {'currency' : ''}, 'DPW.DE' : {'name' : 'Deutsche Post', 'currency' : ''},
'CSCO' : {'currency' : '$'}, 'CSCO' : {'name' : 'Cisco', 'currency' : '$'},
'AIR' : {'currency' : '$'}, 'AIR' : {'name' : 'Airbus', 'currency' : '$'},
'BA' : {'currency' : '$'}, 'BA' : {'name' : 'Boeing', 'currency' : '$'},
'NVDA' : {'currency' : '$'}, 'NVDA' : {'name' : 'Nvidia', 'currency' : '$'},
'MSFT' : {'currency' : '$'}, 'MSFT' : {'name' : 'Microsoft', 'currency' : '$'},
'DIS' : {'currency' : '$'}, 'DIS' : {'name' : 'Disney', 'currency' : '$'},
'NFLX' : {'currency' : '$'}, 'NFLX' : {'name' : 'Netflix', 'currency' : '$'},
'OHB.DE' : {'currency' : ''}, 'OHB.DE' : {'name' : 'OHB', 'currency' : ''},
'ERCA.DE' : {'currency' : ''}, 'ERCA.DE' : {'name' : 'Ericsson', 'currency' : ''},
'VAR1.DE' : {'currency' : ''}, 'VAR1.DE' : {'name' : 'Varta', 'currency' : ''},
'HD' : {'currency' : '$'}, 'HD' : {'name' : 'Home Depot', 'currency' : '$'},
'AMZN' : {'currency' : '$'}, 'AMZN' : {'name' : 'Amazon', 'currency' : '$'},
'GOOGL' : {'currency' : '$'}, 'GOOGL' : {'name' : 'Google', 'currency' : '$'},
'FB2A.DE' : {'currency' : ''}, 'FB2A.DE' : {'currency' : ''},
'ZIL2.DE' : {'currency' : ''}, 'ZIL2.DE' : {'currency' : ''},
'SIS.DE' : {'currency' : ''}, 'SIS.DE' : {'currency' : ''},
@@ -67,7 +68,7 @@ titles = {
'DAI.DE' : {'currency' : ''}, 'DAI.DE' : {'currency' : ''},
'SHA.DE' : {'currency' : ''}, 'SHA.DE' : {'currency' : ''},
'CON.DE' : {'currency' : ''}, 'CON.DE' : {'currency' : ''},
'DHER.DE' : {'currency' : ''}, 'DHER.DE' : {'name' : 'Delivery Hero', 'currency' : ''},
'BSL.DE' : {'currency' : ''}, 'BSL.DE' : {'currency' : ''},
'D6H.DE' : {'currency' : ''}, 'D6H.DE' : {'currency' : ''},
'TC1.DE' : {'currency' : ''}, 'TC1.DE' : {'currency' : ''},
@@ -86,7 +87,7 @@ titles = {
'ORCL' : {'currency' : '$'}, 'ORCL' : {'currency' : '$'},
'UBER' : {'currency' : '$'}, 'UBER' : {'currency' : '$'},
'VMW' : {'currency' : '$'}, 'VMW' : {'currency' : '$'},
'AVGO' : {'currency' : '$'}, 'AVGO' : {'name' : 'Avago', 'currency' : '$'},
'CY' : {'currency' : '$'}, 'CY' : {'currency' : '$'},
'QABSY' : {'currency' : '$'}, 'QABSY' : {'currency' : '$'},
'BLDP' : {'currency' : '$'}, 'BLDP' : {'currency' : '$'},
@@ -98,13 +99,13 @@ titles = {
# 'SON1.DE' : {'currency' : '€'}, # 'SON1.DE' : {'currency' : '€'},
'STM.DE' : {'currency' : ''}, 'STM.DE' : {'currency' : ''},
'BC8.DE' : {'currency' : ''}, 'BC8.DE' : {'currency' : ''},
'MOR.DE' : {'currency' : ''}, 'MOR.DE' : {'name' : 'Morphosys', 'currency' : ''},
'BAYN.DE' : {'currency' : ''}, 'BAYN.DE' : {'currency' : ''},
'BEI.DE' : {'currency' : ''}, 'BEI.DE' : {'currency' : ''},
'EUZ.DE' : {'currency' : ''}, 'EUZ.DE' : {'currency' : ''},
'SLAB' : {'currency' : '$'}, 'SLAB' : {'currency' : '$'},
'SPLK' : {'currency' : '$'}, 'SPLK' : {'name' : 'Splunk', 'currency' : '$'},
'IAG' : {'currency' : '$'}, 'IAG' : {'name' : 'IAG', 'currency' : '$'},
'INTC' : {'currency' : '$'}, 'INTC' : {'currency' : '$'},
'I' : {'currency' : '$'}} 'I' : {'currency' : '$'}}
@@ -182,20 +183,24 @@ def bollinger(data, window_days, f=2):
result['mid'] = mid result['mid'] = mid
return result return result
class Symbol(object): class Title(object):
def __init__(self, title, params): def __init__(self, symbol, params):
self.name = title self.symbol = symbol
self.params = params self.params = params
try:
self.name = params['name']
except:
self.name = symbol
self.ax = None self.ax = None
self.fig = None self.fig = None
self.currency = params['currency'] self.currency = params['currency']
self.currency_corr = 1 self.currency_corr = 1
if '$' in self.currency : if '$' in self.currency:
self.currency_corr = 1/1.11 self.currency_corr = k_euro
def fetch(self): def fetch(self):
filename = self.name.replace('.','_') + '.h5' filename = self.symbol.replace('.', '_') + '.h5'
fetch = False fetch = False
today = dt.date.today() today = dt.date.today()
try: try:
@@ -212,17 +217,17 @@ class Symbol(object):
fetch = True fetch = True
if fetch: if fetch:
print ("Fetching \"{}\"".format(self.name)) print ("Fetching \"{}\"".format(self.symbol))
# Get stock price via data reader # Get stock price via data reader
start = dt.datetime(today.year,1,1) start = dt.datetime(today.year,1,1)
end = dt.date.today() end = dt.date.today()
data = web.DataReader(self.name, "av-daily", start, end, api_key=key) data = web.DataReader(self.symbol, "av-daily", start, end, api_key=key)
hdf = pd.HDFStore(filename) hdf = pd.HDFStore(filename)
hdf[self.name] = data hdf[self.symbol] = data
else: else:
hdf = pd.HDFStore(filename, 'r') hdf = pd.HDFStore(filename, 'r')
self.data_full = hdf[self.name]*self.currency_corr self.data_full = hdf[self.symbol] * self.currency_corr
hdf.close() hdf.close()
def __plot(self, d, keys): def __plot(self, d, keys):
@@ -236,7 +241,7 @@ class Symbol(object):
if event.inaxes == self.ax: if event.inaxes == self.ax:
x_idx = int(event.xdata) x_idx = int(event.xdata)
self.ax.format_xdata = lambda x: dates[x_idx] self.ax.format_xdata = lambda x: dates[x_idx]
self.ax.format_ydata = lambda y: '{}{:.2f}'.format(self.currency, data[x_idx]) self.ax.format_ydata = lambda y: '{:.2f}'.format(data[x_idx])
self.fig.canvas.mpl_connect("motion_notify_event", hover) self.fig.canvas.mpl_connect("motion_notify_event", hover)
@@ -294,23 +299,22 @@ class Symbol(object):
figNum = 1 figNum = 1
if len(show_title) > 0: if len(show_symbols) > 0:
for title in show_title: for symbol in show_symbols:
if title in titles: if symbol in symbols:
sym = Symbol(title, titles[title]) title = Title(symbol, symbols[symbol])
sym.fetch() title.fetch()
print(sym.analyze()) print(title.analyze())
sym.show(figNum) title.show(figNum)
figNum += 1 figNum += 1
else: else:
for title in show_title: for symbol in symbols:
if title in titles: title = Title(symbol, symbols[symbol])
sym = Symbol(title, titles[title]) title.fetch()
sym.fetch() result = title.analyze()
result = sym.analyze() if result['macd_f'] > thresh_macd and result['macd_fd'] > 0:
if result['macd_f'] > thresh_macd and result['macd_fd'] > 0: title.show(figNum)
sym.show(figNum) figNum += 1
figNum += 1
plt.show() plt.show()