From 8a01150261e4b960db4bfbd67726c931127f896c Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 25 Dec 2019 21:18:41 +0000 Subject: [PATCH] - fixed plot - refactored git-svn-id: http://moon:8086/svn/projects/Stock@331 fda53097-d464-4ada-af97-ba876c37ca34 --- robot.py | 2 +- stock.py | 51 +++++++++++++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/robot.py b/robot.py index d699de2..5ef9e0d 100644 --- a/robot.py +++ b/robot.py @@ -16,7 +16,7 @@ from stock import Stock key = '0UO7Z2MVZ2YSQSVE' params = { - 'show_range_days' : 5, + 'show_range_days' : 10, 'k_euro' : 1 / 1.11, 'ema_alpha' : 0.75, 'sma_days' : 10, diff --git a/stock.py b/stock.py index 18fb5a1..f03dc3c 100644 --- a/stock.py +++ b/stock.py @@ -13,20 +13,20 @@ import matplotlib as mpl mpl.rc('figure', max_open_warning = 0) class Stock(object): - def __init__(self, params2, symbol, params): - self.params = params + def __init__(self, params, symbol, symbol_params): + self.symbol_params = symbol_params self.symbol = symbol - self.params2 = params2 + self.params = params try: - self.name = params['name'] + self.name = symbol_params['name'] except: self.name = symbol self.ax = None self.fig = None - self.currency = params['currency'] + self.currency = symbol_params['currency'] self.currency_corr = 1 if '$' in self.currency: - self.currency_corr = self.params2['k_euro'] + self.currency_corr = self.params['k_euro'] self.data = {} self.boll = {} @@ -48,7 +48,7 @@ class Stock(object): try: lastmodified = dt.datetime.fromtimestamp(os.stat(filename).st_mtime).date() if lastmodified != today: - fetch = self.params2['fetch_on_outdated'] + fetch = self.params['fetch_on_outdated'] except: fetch = True @@ -84,12 +84,12 @@ class Stock(object): def statistics(self): N = len(self.data['index']) - self.data['ema'] = exponential_moving_average(self.data, key='close', alpha=self.params2['ema_alpha']) - self.data['sma'] = moving_average(self.data, key='close', window_days=self.params2['sma_days']) + 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.params['sma_days']) self.data['close_n'] = normalize(self.data, key='close') 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['max'] = moving_max(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.params['q_days']) self.data['Qmin'] = self.data['close_n'] - self.data['min'] self.data['Qmax'] = self.data['close_n'] - self.data['max'] @@ -125,9 +125,10 @@ class Stock(object): return result - def __plot(self, data, keys): + def __plot(self, id, data, keys): + ax = plt.subplot(id) N = len(data['index']) - start = max(0, N - self.params2['show_range_days']) + start = max(0, N - self.params['show_range_days']) stop = N xr = list(range(start, stop)) sliced = Stock.slice(data, start, stop) @@ -137,16 +138,15 @@ class Stock(object): def hover(event): - if event.inaxes == self.ax: + if event.inaxes == ax: x_idx = min(N-1, int(event.xdata)) - self.ax.format_xdata = lambda x: self.data['index'][x_idx] - self.ax.format_ydata = lambda y: '{:.2f}%'.format(self.data['close_n'][x_idx]) + ax.format_xdata = lambda x: self.data['index'][x_idx] self.fig.canvas.mpl_connect("motion_notify_event", hover) def __ind(self, data, keys): N = len(data['index']) - start = max(0, N - self.params2['show_range_days']) + start = max(0, N - self.params['show_range_days']) stop = N xr = list(range(start, stop)) sliced = Stock.slice(data, start, stop) @@ -156,27 +156,26 @@ class Stock(object): def show(self, indicators, figNum=1): num_subplots = 4 + subplot_id = 100*num_subplots + 10 + 1 self.fig = plt.figure(figNum) - self.ax = plt.subplot(100*num_subplots + 10 + 1) - self.__plot(self.data, ['min', 'max', 'close_n']) + self.__plot(subplot_id, self.data, ['min', 'max', 'close_n']) self.__ind(indicators, ['BTFD']) plt.title('{} ({})'.format(self.name, self.symbol)) plt.legend() plt.grid() - plt.subplot(100*num_subplots + 10 + 2) + subplot_id += 1 # self.__plot(self.boll, ['lower', 'mid', 'upper']) - self.__plot(self.data, ['Qmin', 'Qmax']) + self.__plot(subplot_id, self.data, ['Qmin', 'Qmax']) plt.legend() plt.grid() - - plt.subplot(100*num_subplots + 10 + 3) - self.__plot(self.data, ['macd', 'macd_f']) + subplot_id += 1 + self.__plot(subplot_id, self.data, ['macd', 'macd_f']) plt.legend() plt.grid() - plt.subplot(100*num_subplots + 10 + 4) - self.__plot(self.data, ['macd_fd', 'macd_fdd']) + subplot_id += 1 + self.__plot(subplot_id, self.data, ['macd_fd', 'macd_fdd']) plt.legend() plt.grid()