MACD quantization trade
来源:https://uqer.io/community/share/56265ed3f9f06c4ca62fb618
import talib
import copy
from numpy import arange, array, isnan
start = '2014-01-01'
end = '2015-10-19'
benchmark = 'HS300'
universe = set_universe('HS300')
capital_base = 1000000
freq = 'd'
refresh_rate = 1
def dea_deviate_from_k_line(k_line, dea):
k_line_re = k_line[::-1]
dea_re = dea[::-1]
k_line_extremum = []
k_line_extremum_inx = {}
dea_extremum = []
dea_extremum_inx = {}
for c in xrange(1, len(k_line_re) - 1):
if not isnan(k_line_re[c - 1]) and not isnan(k_line_re[c]) and not isnan(k_line_re[c + 1]):
if k_line_re[c - 1] < k_line_re[c] and k_line_re[c + 1] < k_line_re[c]:
k_line_extremum.append(k_line_re[c])
k_line_extremum_inx[k_line_re[c]] = c
for c in xrange(1, len(dea_re) - 1):
if not isnan(dea_re[c - 1]) and not isnan(dea_re[c]) and not isnan(dea_re[c + 1]):
if dea_re[c - 1] < dea_re[c] and dea_re[c + 1] < dea_re[c]:
dea_extremum.append(dea_re[c])
dea_extremum_inx[dea_re[c]] = c
sig_extremum = 0
if len(k_line_extremum) >= 2 and len(dea_extremum) >= 2:
k_line_first_deviate = k_line_extremum[0] - k_line_extremum[1]
dea_first_deviate = dea_extremum[0] - dea_extremum[1]
k_and_dea_start_deviate = k_line_extremum_inx[k_line_extremum[0]] - dea_extremum_inx[dea_extremum[0]]
k_and_dea_end_deviate = k_line_extremum_inx[k_line_extremum[1]] - dea_extremum_inx[dea_extremum[1]]
if abs(k_and_dea_start_deviate) < 2:
k_deviate_dates = abs(k_line_extremum_inx[k_line_extremum[0]]) - abs(k_line_extremum_inx[k_line_extremum[1]])
dea_deviate_date = abs(dea_extremum_inx[dea_extremum[0]]) - abs(dea_extremum_inx[dea_extremum[1]])
dea_k_deviate = abs(k_deviate_dates) - abs(dea_deviate_date)
if abs(k_deviate_dates) >= 3 and abs(dea_deviate_date) >= 3 and dea_k_deviate < 3:
if k_line_first_deviate < 0 and dea_first_deviate > 0 :
sig_extremum = 1;
elif k_line_first_deviate > 0 and dea_first_deviate < 0:
sig_extremum = -1
else:
if abs(k_deviate_dates) > abs(dea_deviate_date):
if k_line_first_deviate < 0:
sig_extremum = -1
else:
sig_extremum = 1
else:
if dea_first_deviate < 0:
sig_extremum = -1
else:
sig_extremum = 1
return sig_extremum
def initialize(account):
pass
def handle_data(account):
fibonacci_sequence_date1 = 144
fibonacci_sequence_date2 = 143
hist = account.get_attribute_history('closePrice', fibonacci_sequence_date1)
for stock in account.universe:
s_num = 0
for c in hist[stock]:
if isnan(c):
s_num += 1
if s_num >= len(hist[stock]) - 10:
continue
macd, macdsignal, macdhist = talib.MACD(hist[stock])
trade_signal = 0
for c in range(fibonacci_sequence_date2, len(macd) - 1):
if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] > 0 and macdsignal[c] > 0:
if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] < macdsignal[c - 1]:
if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] > macdsignal[c + 1]:
trade_signal += 1
if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] < 0 and macdsignal[c] < 0:
if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] > macdsignal[c - 1]:
if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] < macdsignal[c + 1]:
trade_signal -= 1
if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]):
if macdhist[c] > macdhist[c - 1] and macdhist[c + 1] > macdhist[c] and macdhist[c] > 0 and macdhist[c - 1] < 0:
trade_signal += 1
if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]):
if macdhist[c] < macdhist[c - 1] and macdhist[c + 1] < macdhist[c] and macdhist[c] < 0 and macdhist[c - 1] > 0:
trade_signal -= 1
trade_signal += dea_deviate_from_k_line(hist[stock], macdsignal)
if trade_signal > 0 and trade_signal <= 1:
order(stock, 100)
elif trade_signal > 1:
order(stock, 200)
elif trade_signal < 0 and trade_signal >= -1:
order(stock, -100)
elif trade_signal < -1:
order(stock, -200)