- improved

git-svn-id: http://moon:8086/svn/projects/Stock@333 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-12-27 11:34:00 +00:00
parent fe706dead2
commit a739314378
3 changed files with 54 additions and 48 deletions
+25 -26
View File
@@ -2,27 +2,27 @@ import numpy as np
import math
def exponential_moving_average(data, key, alpha=0.5, ic=None):
result = np.zeros_like(data[key])
def exponential_moving_average(data, alpha=0.5, ic=None):
result = np.zeros_like(data)
if ic is None:
r = data[key][0]
r = data[0]
else:
r = ic
n = 0
for v in data[key]:
for v in data:
r = alpha*r + (1-alpha)*v
result[n] = r
n += 1
return np.array(result)
def moving_average(data, key, window_days, ic=0):
def moving_average(data, window_days, ic=0):
mem = [ic] * window_days
result = np.zeros_like(data[key])
result = np.zeros_like(data)
k=0
cumsum = ic
n = 0
for v in data[key]:
for v in data:
cumsum += (v - mem[k])
mem[k] = v
k += 1
@@ -34,12 +34,12 @@ def moving_average(data, key, window_days, ic=0):
return result
def moving_variance(data, key, window_days, ic=0):
def moving_variance(data, window_days, ic=0):
mem = [ic] * window_days
result = np.zeros_like(data[key])
result = np.zeros_like(data)
k=0
cumsum = ic
data_mean = data[key] - moving_average(data, key, window_days, ic)
data_mean = data - moving_average(data, window_days, ic)
n = 0
for v in data_mean:
try:
@@ -58,12 +58,12 @@ def moving_variance(data, key, window_days, ic=0):
return result
def moving_max(data, key, window_days, ic=-1e9):
def moving_max(data, window_days, ic=-1e9):
mem = [ic] * window_days
result = np.zeros_like(data[key])
result = np.zeros_like(data)
k=0
n = 0
for v in data[key]:
for v in data:
mem[k] = v
k += 1
if k >= window_days:
@@ -74,12 +74,12 @@ def moving_max(data, key, window_days, ic=-1e9):
return result
def moving_min(data, key, window_days, ic=1e9):
def moving_min(data, window_days, ic=1e9):
mem = [ic] * window_days
result = np.zeros_like(data[key])
result = np.zeros_like(data)
k=0
n = 0
for v in data[key]:
for v in data:
mem[k] = v
k += 1
if k >= window_days:
@@ -90,15 +90,15 @@ def moving_min(data, key, window_days, ic=1e9):
return result
def normalize(data, key, ic=None):
result = np.zeros_like(data[key])
def normalize(data, ic=None):
result = np.zeros_like(data)
cumsum = 0
if ic is None:
prev = data[key][0]
prev = data[0]
else:
prev = ic
n = 0
for v in data[key]:
for v in data:
if prev == 0:
print (prev)
cumsum += 100*(v - prev)/(prev)
@@ -118,18 +118,17 @@ def colsum(data, keys):
return result
def macd(data, key):
ema_short = exponential_moving_average(data, key, alpha=0.85)
ema_long = exponential_moving_average(data, key, alpha=0.925)
def macd(data):
ema_short = exponential_moving_average(data, alpha=0.85)
ema_long = exponential_moving_average(data, alpha=0.925)
return ema_short - ema_long
def bollinger(data, window_days, f=2):
tp = colsum(data, keys=['high', 'low', 'close']) / 3
tp_dict = {'tp': tp}
stddev = np.array(moving_variance(tp_dict, 'tp', window_days))
stddev = np.array(moving_variance(tp, window_days))
mid = np.array(moving_average(tp_dict, key='tp', window_days=window_days))
mid = np.array(moving_average(tp, window_days=window_days))
upper = mid + f * stddev
lower = mid - f * stddev
result = {'index' : data['index'], 'upper' : upper, 'mid' : mid, 'lower' : lower}
+10 -5
View File
@@ -14,9 +14,8 @@
import matplotlib.pyplot as plt
from stock import Stock
key = '0UO7Z2MVZ2YSQSVE'
params = {
'show_range_days' : 10,
'show_range_days' : 5,
'k_euro' : 1 / 1.11,
'ema_alpha' : 0.75,
'sma_days' : 10,
@@ -28,13 +27,13 @@ show_symbols = ['OHB.DE', 'ITMPF', 'PLUG', 'MOR.DE', 'CSCO', 'ERCA.DE', 'AVGO',
show_symbols = ['CSCO']
#show_symbols = ['OHB.DE']
#show_symbols = ['UBSFF']
show_symbols = ['DHER.DE']
#show_symbols = ['DHER.DE']
#show_symbols = ['WDI.DE']
#show_symbols = ['EVT.DE']
show_symbols = []
symbols = {
'SSHPF' : {'name' : 'Scanship Holding ASA', 'currency' : '$'},
'RIG' : {'name' : 'Transocean Ltd.', 'currency' : '$'},
'BBIO' : {'name' : 'BridgeBio Pharma, Inc.', 'currency' : '$'},
'APA' : {'name' : 'Apache Corporation', 'currency' : '$'},
@@ -140,20 +139,26 @@ if len(show_symbols) > 0:
title = Stock(params, symbol, symbols[symbol])
title.fetch()
title.statistics()
try:
ind = title.analyze(buy_callback, range_days=params['show_range_days'])
title.show(ind, figNum=figNum)
figNum += 1
except:
print ("Exception occurred for {}".format(symbol))
else:
for symbol in symbols:
title = Stock(params, symbol, symbols[symbol])
title.fetch()
title.statistics()
try:
ind = title.analyze(buy_callback, range_days=params['show_range_days'])
if Stock.has_candidate(ind, 'BTFD'):
print('-----------------------------------------------')
title.show(ind, figNum=figNum)
figNum += 1
except:
print ("Exception occurred for {}".format(symbol))
gain_accum = 0
num_stocks = 0
for symbol in buy_list:
+11 -9
View File
@@ -12,6 +12,8 @@ import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0)
key = '0UO7Z2MVZ2YSQSVE'
class Stock(object):
def __init__(self, params, symbol, symbol_params):
self.symbol_params = symbol_params
@@ -84,26 +86,26 @@ class Stock(object):
def statistics(self):
N = len(self.data['index'])
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.params['q_days'])
self.data['max'] = moving_max(self.data, key='close_n', window_days=self.params['q_days'])
self.data['ema'] = exponential_moving_average(self.data['close'], alpha=self.params['ema_alpha'])
self.data['sma'] = moving_average(self.data['close'], window_days=self.params['sma_days'])
self.data['close_n'] = normalize(self.data['close'])
self.data['macd'] = macd(self.data['close_n'])
self.data['min'] = moving_min(self.data['close_n'], window_days=self.params['q_days'])
self.data['max'] = moving_max(self.data['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']
self.boll = bollinger(self.data, window_days=30)
x_r = np.linspace(0, N, N)
y_r = self.data['macd']
y_r = np.append(self.data['macd'], self.data['macd'][N-1])
x_r = np.linspace(1, N, N+1)
spl = UnivariateSpline(x=x_r, y=y_r, k=5)
spl.set_smoothing_factor(0.25)
spl_d = spl.derivative()
spl_dd = spl.derivative().derivative()
yf = spl(x_r)
yf_d = spl_d(x_r)
x_r2 = np.linspace(-1, N-1, N)
x_r2 = np.linspace(1, N, N+1)
yf_dd = spl_dd(x_r2)
self.data['macd_f'] = np.transpose(yf)