hikyuu2/tools/hikyuu/interactive/elder.py

98 lines
3.9 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#===============================================================================
# 作者fasiondog
# 历史120100224, Added by fasiondog
#===============================================================================
"""
.
2007 Alexander Elder
"""
from pylab import plot
from numpy import mean
from hikyuu import QueryByIndex, constant
from hikyuu.indicator import Indicator, CLOSE, EMA, MACD, VIGOR, SAFTYLOSS
from hikyuu.interactive.drawplot import (create_three_axes_figure,
ax_draw_macd2,
adjust_axes_show,
ax_set_locator_formatter)
def _find_ema_coefficient(closes, emas, number=66, percent=0.95):
"""计算EMA通道系数。
EMA EMA
EMA EMA
使95
closes
emasEMA序列
number: N天的数据作为计算标准
percent0.9595%
"""
assert len(closes)==len(emas), "数据长度不等"
assert number>=1, "Number必须大于0"
tmp_closes = closes[-number:]
tmp_emas = emas[-number:]
dev = [abs(tmp_closes[i]-tmp_emas[i]) for i in range(len(tmp_closes))]
sortdev = sorted(dev)
x = int(number*percent)
x = x if len(dev)-1>x else len(dev)-1
ix = dev.index(sortdev[x])
return float(dev[ix])/tmp_emas[ix] if tmp_emas[ix] != 0.0 else 0.0
def _draw_ema_pipe(axes, kdata, ema, n=22, w=0.10):
emas = [i for i in ema]
p = _find_ema_coefficient(list(CLOSE(kdata)), emas, number=66, percent=0.95) if w=='auto' else w
emas_high = [i+i*p for i in emas]
emas_low = [i-i*p for i in emas]
emas_len = len(emas)
axes.plot(emas, '-b', label='%s(%s) %.2f'%(ema.name, n, emas[-1]))
axes.plot(emas_high, ':b', label='U%s(%.2f%%) %.2f'%(ema.name, p*100, emas_high[-1]))
axes.plot(emas_low, ':b', label='L%s(%.2f%%) %.2f'%(ema.name, p*100, emas_low[-1]))
fy1 = [ i for i in emas_low]
fy2 = [ i for i in emas_high]
axes.fill_between(range(emas_len),fy1,fy2,alpha=0.2, color='y' )
def draw(stock, query=QueryByIndex(-130), ma_n=22, ma_w='auto', vigor_n=13):
kdata = stock.getKData(query)
close = CLOSE(kdata)
ema = EMA(close, ma_n)
sf = SAFTYLOSS(close, 10, 3, 2.0)
vigor = VIGOR(kdata, vigor_n)
ax1, ax2, ax3 = create_three_axes_figure()
kdata.plot(axes=ax1)
_draw_ema_pipe(ax1,kdata, ema, n=ma_n, w=ma_w)
sf.plot(axes=ax1, color='y', legend_on=True)
#ax1.legend(loc='upper left')
ax_draw_macd2(ax2, ema, kdata)
vigor.plot(axes=ax3, marker='.', color='r', zero_on=True,
legend_on=False, text_on=True)
u = [i for i in vigor if i > 0 and i != constant.null_price]
l = [i for i in vigor if i < 0]
umean = mean(u)
umax = max(u)
lmean = mean(l)
lmin = min(l)
up = int(umax / umean)
lp = int(lmin / lmean)
#ax3.hlines(umean,0,len(kdata),color='r',linestyle='--')
#ax3.hlines(lmean,0,len(kdata),color='r',linestyle='--')
for i in range(up):
ax3.hlines(umean * (i + 1),0,len(kdata),color='r',linestyle='--')
for i in range(lp):
ax3.hlines(lmean * (i + 1),0,len(kdata),color='g',linestyle='--')
ax1.set_xlim((0, len(kdata)))
ax_set_locator_formatter(ax1, kdata.getDatetimeList(), kdata.getQuery().kType)
adjust_axes_show([ax1, ax2, ax3])