hikyuu2/hikyuu/trade_sys/signal.py
2019-05-18 22:16:10 +08:00

359 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#
# 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.
from ._trade_sys import (SignalBase,
SG_Bool,
SG_Single,
SG_Single2,
SG_Cross,
SG_CrossGold,
SG_Flex)
from hikyuu.util.unicode import (unicodeFunc, reprFunc)
SignalBase.__unicode__ = unicodeFunc
SignalBase.__repr__ = reprFunc
def sig_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k,v in params.items():
self.setParam(k, v)
def crtSG(func, params={}, name='crtSG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SignalBase,), {'__init__': sig_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
SignalBase.__doc__ = """
alternate (bool|True) 线
线
SignalBase._calculate() -
SignalBase._clone() -
SignalBase._reset() -
1:
from hikyuu.trade_sys.signal import SignalBase
from hikyuu.indicator import HHV, LLV, CLOSE, REF
class TurtleSignal(SignalBase):
def __init__(self, n = 20):
super(TurtleSignal, self).__init__("TurtleSignal")
self.setParam("n", 20)
def _clone(self):
return TurtleSignal()
def _calculate(self):
n = self.getParam("n")
k = self.getTO()
c = CLOSE(k)
h = REF(HHV(c, n), 1) #n日高点
L = REF(LLV(c, n), 1) #n日低点
for i in range(h.discard, len(k)):
if (c[i] >= h[i]):
self._addBuySignal(k[i].datetime)
elif (c[i] <= L[i]):
self._addSellSignal(k[i].datetime)
if __name__ == "__main__":
from examples_init import *
sg = TurtleSignal()
s = getStock("sh000001")
k = s.getKData(Query(-500))
#只有设置交易对象时,才会开始实际计算
sg.setTO(k)
dates = k.getDatetimeList()
for d in dates:
if (sg.shouldBuy(d)):
print("买入:%s" % d)
elif (sg.shouldSell(d)):
print("卖出: %s" % d)
2:
class SignalPython(SignalBase):
def __init__(self):
super(SignalPython, self).__init__("SignalPython")
self._x = 0 #
self.setParam("test", 30)
def _reset(self):
self._x = 0
def _clone(self):
p = SignalPython()
p._x = self._x
return p
def _calculate(self):
self._addBuySignal(Datetime(201201210000))
self._addSellSignal(Datetime(201201300000))
"""
SignalBase.name.__doc__ = """名称"""
SignalBase.__init__.__doc__ = """
__init__(self[, name="SignalBase"])
:param str name:
"""
SignalBase.getParam.__doc__ = """
getParam(self, name)
:param str name:
:return:
:raises out_of_range:
"""
SignalBase.setParam.__doc__ = """
setParam(self, name, value)
:param str name:
:param value:
:type value: int | bool | float | string
:raises logic_error: Unsupported type!
"""
SignalBase.setTO.__doc__ = """
setTO(self, k)
:param KData k:
"""
SignalBase.getTO.__doc__ = """
getTO(self)
:return:
:rtype: KData
"""
SignalBase.shouldBuy.__doc__ = """
shouldBuy(self, datetime)
:param Datetime datetime:
:rtype: bool
"""
SignalBase.shouldSell.__doc__ = """
shouldSell(self, datetime)
:param Datetime datetime:
:rtype: bool
"""
SignalBase.getBuySignal.__doc__ = """
getBuySignal(self)
:rtype: DatetimeList
"""
SignalBase.getSellSignal.__doc__ = """
getSellSignal(self)
:rtype: DatetimeList
"""
SignalBase._addBuySignal.__doc__ = """
_addBuySignal(self, datetime)
_calculate中调用
:param Datetime datetime:
"""
SignalBase._addSellSignal.__doc__ = """
_addSellSignal(self, datetime)
_calculate中调用
:param Datetime datetime:
"""
SignalBase.reset.__doc__ = """
reset(self)
"""
SignalBase.clone.__doc__ = """
clone(self)
"""
SignalBase._calculate.__doc__ = """
_calculate(self)
"""
SignalBase._reset.__doc__ = """
_reset(self)
"""
SignalBase._clone.__doc__ = """
_clone(self)
"""
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------
SG_Flex.__doc__ = """
SG_Flex(ind, slow_n[, kpart = 'CLOSE'])
使EMA(slow_n)线线线穿线线穿线
:param Indicator ind:
:param int slow_n: 线EMA周期
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return:
"""
SG_Cross.__doc__ = """
SG_Cross(fast, slow[, kpart = "CLOSE"])
线线穿线线穿线5MA上穿10日MA时买入5MA线下穿MA10日线时卖出::
SG_Cross(OP(MA(n=10)), OP(MA(n=30)))
:param Indicator fast: 线
:param Indicator slow: 线
:param string kpart: OPEN|HIGH|LOW|CLOSE|AMO|VOL|KDATA
:return:
"""
SG_CrossGold.__doc__ = """
SG_CrossGold(fast, slow[, kpart = "CLOSE"])
线穿线线线
线穿线线线::
SG_CrossGold(OP(MA(n=10)), OP(MA(n=30)))
:param Indicator fast: 线
:param Indicator slow: 线
:param string kpart: OPEN|HIGH|LOW|CLOSE|AMO|VOL|KDATA
:return:
"""
SG_Single.__doc__ = """
SG_Single(ind[, filter_n = 10, filter_p = 0.1, kpart='CLOSE'])
线使 [BOOK1]_ 线线::
filter = percentage * STDEV((AMA-AMA[1], N)
Buy When AMA - AMA[1] > filter
or Buy When AMA - AMA[2] > filter
or Buy When AMA - AMA[3] > filter
:param Indicator ind:
:param int filer_n: N日周期
:param float filter_p:
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return:
"""
SG_Single2.__doc__ = """
SG_Single2(ind[, filter_n = 10, filter_p = 0.1, kpart='CLOSE'])
线2 [BOOK1]_::
filter = percentage * STDEV((AMA-AMA[1], N)
Buy When AMA - @lowest(AMA,n) > filter
Sell When @highest(AMA, n) - AMA > filter
:param Indicator ind:
:param int filer_n: N日周期
:param float filter_p:
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return:
"""
SG_Bool.__doc__ = """
SG_Bool(buy, sell[, kpart='CLOSE'])
使bool数组的Indicator分别作为买入
:param Indicator buy: Indicator中相应位置>0
:param Indicator sell: Indicator中相应位置>0
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return:
"""