- fixed plot

- refactored

git-svn-id: http://moon:8086/svn/projects/Stock@331 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-12-25 21:18:41 +00:00
parent cc5134b800
commit 8a01150261
2 changed files with 26 additions and 27 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ from stock import Stock
key = '0UO7Z2MVZ2YSQSVE' key = '0UO7Z2MVZ2YSQSVE'
params = { params = {
'show_range_days' : 5, 'show_range_days' : 10,
'k_euro' : 1 / 1.11, 'k_euro' : 1 / 1.11,
'ema_alpha' : 0.75, 'ema_alpha' : 0.75,
'sma_days' : 10, 'sma_days' : 10,
+25 -26
View File
@@ -13,20 +13,20 @@ import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0) mpl.rc('figure', max_open_warning = 0)
class Stock(object): class Stock(object):
def __init__(self, params2, symbol, params): def __init__(self, params, symbol, symbol_params):
self.params = params self.symbol_params = symbol_params
self.symbol = symbol self.symbol = symbol
self.params2 = params2 self.params = params
try: try:
self.name = params['name'] self.name = symbol_params['name']
except: except:
self.name = symbol self.name = symbol
self.ax = None self.ax = None
self.fig = None self.fig = None
self.currency = params['currency'] self.currency = symbol_params['currency']
self.currency_corr = 1 self.currency_corr = 1
if '$' in self.currency: if '$' in self.currency:
self.currency_corr = self.params2['k_euro'] self.currency_corr = self.params['k_euro']
self.data = {} self.data = {}
self.boll = {} self.boll = {}
@@ -48,7 +48,7 @@ class Stock(object):
try: try:
lastmodified = dt.datetime.fromtimestamp(os.stat(filename).st_mtime).date() lastmodified = dt.datetime.fromtimestamp(os.stat(filename).st_mtime).date()
if lastmodified != today: if lastmodified != today:
fetch = self.params2['fetch_on_outdated'] fetch = self.params['fetch_on_outdated']
except: except:
fetch = True fetch = True
@@ -84,12 +84,12 @@ class Stock(object):
def statistics(self): def statistics(self):
N = len(self.data['index']) N = len(self.data['index'])
self.data['ema'] = exponential_moving_average(self.data, key='close', alpha=self.params2['ema_alpha']) self.data['ema'] = exponential_moving_average(self.data, key='close', alpha=self.params['ema_alpha'])
self.data['sma'] = moving_average(self.data, key='close', window_days=self.params2['sma_days']) self.data['sma'] = moving_average(self.data, key='close', window_days=self.params['sma_days'])
self.data['close_n'] = normalize(self.data, key='close') self.data['close_n'] = normalize(self.data, key='close')
self.data['macd'] = macd(self.data, key='close_n') self.data['macd'] = macd(self.data, key='close_n')
self.data['min'] = moving_min(self.data, key='close_n', window_days=self.params2['q_days']) self.data['min'] = moving_min(self.data, key='close_n', window_days=self.params['q_days'])
self.data['max'] = moving_max(self.data, key='close_n', window_days=self.params2['q_days']) self.data['max'] = moving_max(self.data, key='close_n', window_days=self.params['q_days'])
self.data['Qmin'] = self.data['close_n'] - self.data['min'] self.data['Qmin'] = self.data['close_n'] - self.data['min']
self.data['Qmax'] = self.data['close_n'] - self.data['max'] self.data['Qmax'] = self.data['close_n'] - self.data['max']
@@ -125,9 +125,10 @@ class Stock(object):
return result return result
def __plot(self, data, keys): def __plot(self, id, data, keys):
ax = plt.subplot(id)
N = len(data['index']) N = len(data['index'])
start = max(0, N - self.params2['show_range_days']) start = max(0, N - self.params['show_range_days'])
stop = N stop = N
xr = list(range(start, stop)) xr = list(range(start, stop))
sliced = Stock.slice(data, start, stop) sliced = Stock.slice(data, start, stop)
@@ -137,16 +138,15 @@ class Stock(object):
def hover(event): def hover(event):
if event.inaxes == self.ax: if event.inaxes == ax:
x_idx = min(N-1, int(event.xdata)) x_idx = min(N-1, int(event.xdata))
self.ax.format_xdata = lambda x: self.data['index'][x_idx] ax.format_xdata = lambda x: self.data['index'][x_idx]
self.ax.format_ydata = lambda y: '{:.2f}%'.format(self.data['close_n'][x_idx])
self.fig.canvas.mpl_connect("motion_notify_event", hover) self.fig.canvas.mpl_connect("motion_notify_event", hover)
def __ind(self, data, keys): def __ind(self, data, keys):
N = len(data['index']) N = len(data['index'])
start = max(0, N - self.params2['show_range_days']) start = max(0, N - self.params['show_range_days'])
stop = N stop = N
xr = list(range(start, stop)) xr = list(range(start, stop))
sliced = Stock.slice(data, start, stop) sliced = Stock.slice(data, start, stop)
@@ -156,27 +156,26 @@ class Stock(object):
def show(self, indicators, figNum=1): def show(self, indicators, figNum=1):
num_subplots = 4 num_subplots = 4
subplot_id = 100*num_subplots + 10 + 1
self.fig = plt.figure(figNum) self.fig = plt.figure(figNum)
self.ax = plt.subplot(100*num_subplots + 10 + 1) self.__plot(subplot_id, self.data, ['min', 'max', 'close_n'])
self.__plot(self.data, ['min', 'max', 'close_n'])
self.__ind(indicators, ['BTFD']) self.__ind(indicators, ['BTFD'])
plt.title('{} ({})'.format(self.name, self.symbol)) plt.title('{} ({})'.format(self.name, self.symbol))
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.subplot(100*num_subplots + 10 + 2) subplot_id += 1
# self.__plot(self.boll, ['lower', 'mid', 'upper']) # self.__plot(self.boll, ['lower', 'mid', 'upper'])
self.__plot(self.data, ['Qmin', 'Qmax']) self.__plot(subplot_id, self.data, ['Qmin', 'Qmax'])
plt.legend() plt.legend()
plt.grid() plt.grid()
subplot_id += 1
plt.subplot(100*num_subplots + 10 + 3) self.__plot(subplot_id, self.data, ['macd', 'macd_f'])
self.__plot(self.data, ['macd', 'macd_f'])
plt.legend() plt.legend()
plt.grid() plt.grid()
plt.subplot(100*num_subplots + 10 + 4) subplot_id += 1
self.__plot(self.data, ['macd_fd', 'macd_fdd']) self.__plot(subplot_id, self.data, ['macd_fd', 'macd_fdd'])
plt.legend() plt.legend()
plt.grid() plt.grid()