2015-01-07 01:26:14 +08:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
# cp936
|
2017-09-26 07:07:56 +08:00
|
|
|
|
#
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2010-2017 fasiondog
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
#===============================================================================
|
2017-09-26 07:07:56 +08:00
|
|
|
|
# History:
|
|
|
|
|
# 1. 20100224, Added by fasiondog
|
2015-01-07 01:26:14 +08:00
|
|
|
|
#===============================================================================
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)
|
|
|
|
|
参见:《精明交易者》(2006年 广东经济出版社)
|
|
|
|
|
"""
|
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
from hikyuu import Query, StockManager
|
2017-11-29 01:24:31 +08:00
|
|
|
|
from hikyuu.indicator import (OP, AMA, STDEV, CVAL, PRICELIST, EMA, CLOSE,
|
|
|
|
|
HIGH, LOW, OPEN, KDATA, POS)
|
2016-04-18 19:54:42 +08:00
|
|
|
|
from hikyuu.trade_sys.signal import SG_Single, SG_Cross, SG_Flex
|
2015-01-07 01:26:14 +08:00
|
|
|
|
from hikyuu.trade_manage import BUSINESS
|
2017-11-29 01:24:31 +08:00
|
|
|
|
from .drawplot import (show_gcf,
|
|
|
|
|
create_figure,
|
2017-11-28 01:47:44 +08:00
|
|
|
|
ax_set_locator_formatter,
|
|
|
|
|
adjust_axes_show,
|
2017-09-26 07:07:56 +08:00
|
|
|
|
ax_draw_macd)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
def draw(stock, query = Query(-130),
|
|
|
|
|
n = 10, filter_n = 20, filter_p = 0.1,
|
|
|
|
|
sg_type = "CROSS",
|
|
|
|
|
show_high_low = False,
|
|
|
|
|
arrow_style = 1):
|
2017-09-26 07:07:56 +08:00
|
|
|
|
"""绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)"""
|
2015-01-07 01:26:14 +08:00
|
|
|
|
kdata = stock.getKData(query)
|
|
|
|
|
|
2017-09-26 07:07:56 +08:00
|
|
|
|
ax1, ax2 = create_figure(2)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
kdata.plot(axes = ax1)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
|
|
cama = AMA(CLOSE(kdata), n = n )
|
|
|
|
|
cama.name = "CAMA"
|
2015-01-07 01:26:14 +08:00
|
|
|
|
cama.plot(axes = ax1, color = 'b', legend_on = True)
|
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
hama = AMA(HIGH(kdata), n = n)
|
|
|
|
|
hama.name = "HAMA"
|
|
|
|
|
hstd = STDEV(hama, n)
|
|
|
|
|
lama = AMA(LOW(kdata), n = n)
|
|
|
|
|
lama.name = "LAMA"
|
|
|
|
|
lstd = STDEV(lama, n)
|
|
|
|
|
fy1 = list(lama - lstd)[lstd.discard:]
|
|
|
|
|
fy2 = list(hama + hstd)[hstd.discard:]
|
|
|
|
|
ax1.fill_between(range(lstd.discard,len(kdata)),fy1,fy2,alpha=0.2, color='y' )
|
|
|
|
|
|
|
|
|
|
if show_high_low:
|
|
|
|
|
hama.plot(axes = ax1, color = 'r', legend_on = True)
|
|
|
|
|
lama.plot(axes = ax1, color = 'g', legend_on = True)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
if sg_type == 'CROSS':
|
2016-04-18 19:54:42 +08:00
|
|
|
|
fast_op = OP(AMA(n = n))
|
2016-04-03 00:08:31 +08:00
|
|
|
|
slow_op = OP(OP(EMA(n = 2*n)), fast_op)
|
2016-04-18 19:54:42 +08:00
|
|
|
|
sg = SG_Cross(fast_op, slow_op)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
sg.plot(axes = ax1, kdata = kdata)
|
|
|
|
|
ind = slow_op(KDATA(kdata))
|
|
|
|
|
ind.name = "EMA(CAMA)"
|
|
|
|
|
ind.plot(axes = ax1, color = 'm', legend_on = True)
|
|
|
|
|
|
|
|
|
|
elif sg_type == 'SINGLE':
|
2016-04-18 19:54:42 +08:00
|
|
|
|
sg = SG_Single(cama, filter_n = filter_n, filter_p = filter_p)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
sg.plot(axes = ax1, kdata = kdata)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("sg_type only in ('CORSS', 'SINGLE')")
|
|
|
|
|
|
|
|
|
|
cer = PRICELIST(cama, 1)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
label = "ER(%s)" % cer[-1]
|
|
|
|
|
cer.plot(axes=ax2, color='b', marker='o', label=label,
|
|
|
|
|
legend_on=False, text_on=True)
|
2017-11-28 01:47:44 +08:00
|
|
|
|
|
2017-11-29 01:24:31 +08:00
|
|
|
|
total = len(kdata)
|
|
|
|
|
CVAL(0.8, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(-0.6, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(-0.8, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(0, total).plot(axes=ax2,color='k',linestyle='-')
|
2017-11-28 01:47:44 +08:00
|
|
|
|
#ax2.hlines(0.8,0,len(kdata),color='r',linestyle='--')
|
2017-11-29 01:24:31 +08:00
|
|
|
|
#ax2.hlines(-0.6,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax2.hlines(-0.8,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax2.hlines(0,0,len(kdata))
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
ax1.set_xlim((0, len(kdata)))
|
|
|
|
|
ax_set_locator_formatter(ax1, kdata.getDatetimeList(), query.kType)
|
|
|
|
|
adjust_axes_show([ax1, ax2])
|
2017-11-29 01:24:31 +08:00
|
|
|
|
return show_gcf()
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw2(block, query = Query(-130),
|
|
|
|
|
ama1 = AMA(n=10, fast_n=2, slow_n=30),
|
|
|
|
|
ama2 = None,
|
|
|
|
|
n = 10, filter_n = 20, filter_p = 0.1,
|
|
|
|
|
sg_type = 'CROSS',
|
|
|
|
|
show_high_low = True,
|
|
|
|
|
arrow_style = 1):
|
2017-09-26 07:07:56 +08:00
|
|
|
|
"""绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)"""
|
2016-04-03 00:08:31 +08:00
|
|
|
|
sm = StockManager.instance()
|
|
|
|
|
if block.name == 'SZ':
|
|
|
|
|
kdata = sm['sz000001'].getKData(query)
|
|
|
|
|
elif block.name == 'GEM':
|
|
|
|
|
kdata = sm['sz399006'].getKData(query)
|
|
|
|
|
else:
|
|
|
|
|
kdata = sm['sh000001'].getKData(query)
|
|
|
|
|
|
2017-11-28 01:47:44 +08:00
|
|
|
|
ax1, ax2, ax3 = create_figure(3)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
kdata.plot(axes = ax1)
|
|
|
|
|
|
|
|
|
|
cama = AMA(CLOSE(kdata), n = n )
|
|
|
|
|
cama.name = "CAMA"
|
|
|
|
|
cama.plot(axes = ax1, color = 'b', legend_on = True)
|
|
|
|
|
|
|
|
|
|
hama = AMA(HIGH(kdata), n = n)
|
|
|
|
|
hama.name = "HAMA"
|
|
|
|
|
hstd = STDEV(hama, n)
|
|
|
|
|
lama = AMA(LOW(kdata), n = n)
|
|
|
|
|
lama.name = "LAMA"
|
|
|
|
|
lstd = STDEV(lama, n)
|
|
|
|
|
fy1 = list(lama - lstd)[lstd.discard:]
|
|
|
|
|
fy2 = list(hama + hstd)[hstd.discard:]
|
|
|
|
|
ax1.fill_between(range(lstd.discard,len(kdata)),fy1,fy2,alpha=0.2, color='y' )
|
|
|
|
|
|
|
|
|
|
if show_high_low:
|
|
|
|
|
hama.plot(axes = ax1, color = 'r', legend_on = True)
|
|
|
|
|
lama.plot(axes = ax1, color = 'g', legend_on = True)
|
|
|
|
|
|
|
|
|
|
if sg_type == 'CROSS':
|
2016-04-18 19:54:42 +08:00
|
|
|
|
fast_op = OP(OP(AMA(n = n)))
|
2016-04-03 00:08:31 +08:00
|
|
|
|
slow_op = OP(OP(EMA(n = 2*n)), fast_op)
|
2016-04-18 19:54:42 +08:00
|
|
|
|
sg = SG_Cross(fast_op, slow_op)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
sg.plot(axes = ax1, kdata = kdata)
|
|
|
|
|
ind = slow_op(KDATA(kdata))
|
|
|
|
|
ind.name = "EMA(CAMA)"
|
|
|
|
|
ind.plot(axes = ax1, color = 'm', legend_on = True)
|
|
|
|
|
|
|
|
|
|
elif sg_type == 'SINGLE':
|
2016-04-18 19:54:42 +08:00
|
|
|
|
sg = SG_Single(cama, filter_n = filter_n, filter_p = filter_p)
|
2016-04-03 00:08:31 +08:00
|
|
|
|
sg.plot(axes = ax1, kdata = kdata)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("sg_type only in ('CORSS', 'SINGLE')")
|
|
|
|
|
|
2016-04-18 19:54:42 +08:00
|
|
|
|
a = POS(block, query, SG_Flex(OP(AMA(n = 3)), 6))
|
2016-04-03 00:08:31 +08:00
|
|
|
|
a.name = "POS(3)"
|
|
|
|
|
a.plot(axes=ax2, color='b', marker='.', legend_on=True)
|
2016-04-18 19:54:42 +08:00
|
|
|
|
a = POS(block, query, SG_Flex(OP(AMA(n = 30)), 60))
|
2016-04-03 00:08:31 +08:00
|
|
|
|
a.name = "POS(30)"
|
|
|
|
|
a.plot(axes=ax2, color='g', marker='.', legend_on=True)
|
2017-11-29 01:24:31 +08:00
|
|
|
|
|
|
|
|
|
total = len(kdata)
|
|
|
|
|
CVAL(0.8, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(0.2, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
#ax2.hlines(0.8,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax2.hlines(0.2,0,len(kdata),color='r',linestyle='--')
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
|
|
if ama1.name == "AMA":
|
|
|
|
|
cer = PRICELIST(cama, 1)
|
|
|
|
|
label = "ER(%s)" % cer[-1]
|
|
|
|
|
cer.plot(axes=ax3, color='b', marker='.', label=label,
|
|
|
|
|
legend_on=False, text_on=True)
|
2017-11-29 01:24:31 +08:00
|
|
|
|
CVAL(0.8, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(-0.6, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(-0.8, total).plot(axes=ax2,color='r',linestyle='--')
|
|
|
|
|
CVAL(0, total).plot(axes=ax2,color='k',linestyle='-')
|
|
|
|
|
#ax3.hlines(0.8,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax3.hlines(-0.6,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax3.hlines(-0.8,0,len(kdata),color='r',linestyle='--')
|
|
|
|
|
#ax3.hlines(0,0,len(kdata))
|
2016-04-03 00:08:31 +08:00
|
|
|
|
else:
|
|
|
|
|
ax_draw_macd(ax2, kdata)
|
|
|
|
|
#ax2.set_ylim(-1, 1)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
ax1.set_xlim((0, len(kdata)))
|
|
|
|
|
ax_set_locator_formatter(ax1, kdata.getDatetimeList(), query.kType)
|
2017-11-29 01:24:31 +08:00
|
|
|
|
adjust_axes_show([ax1, ax2])
|
|
|
|
|
return show_gcf()
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|