From 8e8b9c2d950c6b7663766e0881c1c039827564c6 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Fri, 27 Dec 2019 14:13:48 +0000 Subject: [PATCH] - buy_BTFD: added params - stock::fetch: support slice by date if loaded from file git-svn-id: http://moon:8086/svn/projects/Stock@334 fda53097-d464-4ada-af97-ba876c37ca34 --- agent.py | 15 ++++++++------- robot.py | 1 + stock.py | 19 +++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/agent.py b/agent.py index 576d0b7..b84f0f7 100644 --- a/agent.py +++ b/agent.py @@ -1,7 +1,7 @@ import numpy as np -def buy_BTFD(name, data, range_days, cand_window, marker_key='close_n', thresh_max=-10, thresh_min=1, buy_callback=None): +def buy_BTFD(name, data, range_days, params, marker_key='close_n', thresh_max=-5, thresh_min=1, buy_callback=None): N = len(data['index']) key = 'BTFD' buy_list = {'index' : np.array([None]*N), key : np.array([None]*N)} @@ -10,20 +10,21 @@ def buy_BTFD(name, data, range_days, cand_window, marker_key='close_n', thresh_m for n in range(N-range_days, N): vmax = data['Qmax'][n] vmin = data['Qmin'][n] - trend = data['macd_fdd'][n] + trend = data['macd_fd'][n] + trend2 = data['macd_fdd'][n] index = data['index'][n] value = data[marker_key][n] - if vmin <= thresh_min: - if vmax <= thresh_max: + if vmin <= params['thresh_min']: + if vmax <= params['thresh_max']: cand = n - if trend >= 0: + if trend2 >= 0: do_buy = True print ("{}: Buy on {} at {:0.2f}".format(name, index, value)) cand = None if cand is not None: - if n - cand <= cand_window: - if trend >= 0: + if n - cand <= params['cand_window']: + if trend2 >= 0: do_buy = True former = data[marker_key][cand] print("{}: Delayed buy on {} at {:0.2f} ({:0.2f})".format(name, index, value, former-value)) diff --git a/robot.py b/robot.py index f9c3b20..d6523c0 100644 --- a/robot.py +++ b/robot.py @@ -16,6 +16,7 @@ from stock import Stock params = { 'show_range_days' : 5, + 'btfd' : {'thresh_max' : -10, 'thresh_min' : 1, 'cand_window' : 5}, 'k_euro' : 1 / 1.11, 'ema_alpha' : 0.75, 'sma_days' : 10, diff --git a/stock.py b/stock.py index c1e3019..a9ef903 100644 --- a/stock.py +++ b/stock.py @@ -72,11 +72,18 @@ class Stock(object): hdf = pd.HDFStore(filename, 'r') data = hdf[self.symbol] * self.currency_corr - N = len(data.index) - self.data['index'] = np.array(data.index[0:N]) - self.data['close'] = np.array(data['close'][0:N]) - self.data['high'] = np.array(data['high'][0:N]) - self.data['low'] = np.array(data['low'][0:N]) + start_pos = 0 + end_pos = 0 + + for index in data.index: + end_pos += 1 + if str(end) in index: + break + + self.data['index'] = np.array(data.index[start_pos:end_pos]) + self.data['close'] = np.array(data['close'][start_pos:end_pos]) + self.data['high'] = np.array(data['high'][start_pos:end_pos]) + self.data['low'] = np.array(data['low'][start_pos:end_pos]) hdf.close() @@ -113,7 +120,7 @@ class Stock(object): self.data['macd_fdd'] = np.transpose(yf_dd) def analyze(self, buy_callback, range_days): - return agent.buy_BTFD(self.symbol, self.data, marker_key='close_n', cand_window=5, range_days=range_days, buy_callback=buy_callback) + return agent.buy_BTFD(self.symbol, self.data, params=self.params['btfd'], marker_key='close_n', range_days=range_days, buy_callback=buy_callback) @staticmethod def has_candidate(data, key):