hikyuu2/hikyuu/trade_sys/trade_sys.py

367 lines
11 KiB
C++
Raw Normal View History

2020-07-15 22:50:22 +08:00
# -*- coding: utf8 -*-
from hikyuu.util.slice import list_getitem
from hikyuu.cpp.core import SYS_Simple as cpp_SYS_Simple
from hikyuu.cpp.core import (
System, SystemList, SystemWeight, SystemWeightList, ConditionBase, EnvironmentBase,
MoneyManagerBase, ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase
)
#------------------------------------------------------------------
# System
#------------------------------------------------------------------
System.Part.__doc__ = """
/
:py:class:`TradeRecord`
使使 System.ENVIRONMENT System.Part.ENVIRONMENT
- System.Part.ENVIRONMENT -
- System.Part.CONDITION -
- System.Part.SIGNAL -
- System.Part.STOPLOSS -
- System.Part.TAKEPROFIT -
- System.Part.MONEYMANAGER -
- System.Part.PROFITGOAL -
- System.Part.SLIPPAGE -
- System.Part.INVALID -
"""
System.ENVIRONMENT = System.Part.ENVIRONMENT
System.CONDITION = System.Part.CONDITION
System.SIGNAL = System.Part.SIGNAL
System.STOPLOSS = System.Part.STOPLOSS
System.TAKEPROFIT = System.Part.TAKEPROFIT
System.MONEYMANAGER = System.Part.MONEYMANAGER
System.PROFITGOAL = System.Part.PROFITGOAL
System.SLIPPAGE = System.Part.SLIPPAGE
System.INVALID = System.Part.INVALID
SystemList.__getitem__ = list_getitem
SystemList.__str__ = lambda self: str(list(self))
SystemList.__repr__ = lambda self: repr(list(self))
def SYS_Simple(tm=None, mm=None, ev=None, cn=None, sg=None, st=None, tp=None, pg=None, sp=None):
"""
(run方法
run时没有任何输出
tmsgmm使 run ::
#创建模拟交易账户进行回测初始资金30万
my_tm = crtTM(initCash = 300000)
#创建信号指示器以5日EMA为快线5日EMA自身的10日EMA最为慢线快线向上穿越
#慢线时买入,反之卖出)
my_sg = SG_Flex(OP(EMA(n=5)), slow_n=10)
#固定每次买入1000股
my_mm = MM_FixedCount(1000)
#创建交易系统并运行
sys = SYS_Simple(tm = my_tm, sg = my_sg, mm = my_mm)
sys.run(sm['sz000001'], Query(-150))
:param TradeManager tm:
:param MoneyManager mm:
:param EnvironmentBase ev:
:param ConditionBase cn:
:param SignalBase sg:
:param StoplossBase st:
:param StoplossBase tp:
:param ProfitGoalBase pg:
:param SlippageBase sp:
:return: system实例
"""
sys_ins = cpp_SYS_Simple()
if tm:
sys_ins.tm = tm
if mm:
sys_ins.mm = mm
if ev:
sys_ins.ev = ev
if cn:
sys_ins.cn = cn
if sg:
sys_ins.sg = sg
if st:
sys_ins.st = st
if tp:
sys_ins.tp = tp
if pg:
sys_ins.pg = pg
if sp:
sys_ins.sp = sp
return sys_ins
#------------------------------------------------------------------
# allocatefunds
#------------------------------------------------------------------
SystemWeightList.__getitem__ = list_getitem
SystemWeightList.__str__ = lambda self: str(list(self))
SystemWeightList.__repr__ = lambda self: repr(list(self))
#------------------------------------------------------------------
# condition
#------------------------------------------------------------------
ConditionBase.__init__.__doc__ = """
__init__(self[, name="ConditionBase"])
:param str name:
"""
def cn_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtCN(func, params={}, name='crtCN'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ConditionBase, ), {'__init__': cn_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# environment
#------------------------------------------------------------------
EnvironmentBase.__init__.__doc__ = """
__init__(self[, name='EnvironmentBase'])
:param str name:
"""
def ev_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtEV(func, params={}, name='crtEV'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (EnvironmentBase, ), {'__init__': ev_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# moneymanager
#------------------------------------------------------------------
MoneyManagerBase.__init__.__doc__ = """
__init__(self[, name="MoneyManagerBase])
:param str name:
"""
def mm_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtMM(func, params={}, name='crtMM'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (MoneyManagerBase, ), {'__init__': mm_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# profitgoal
#------------------------------------------------------------------
ProfitGoalBase.__init__.__doc__ = """
__init__(self[, name="ProfitGoalBase"])
:param str name:
"""
def pg_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtPG(func, params={}, name='crtPG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ProfitGoalBase, ), {'__init__': pg_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# selector
#------------------------------------------------------------------
SelectorBase.__init__.__doc__ = """
__init__(self[, name="SelectorBase])
:param str name:
"""
#------------------------------------------------------------------
# signal
#------------------------------------------------------------------
SignalBase.__init__.__doc__ = """
__init__(self[, name="SignalBase"])
:param str name:
"""
def sig_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
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)
#------------------------------------------------------------------
# slippage
#------------------------------------------------------------------
SlippageBase.__init__.__doc__ = """
__init__(self[, name="SlippageBase"])
:param str name:
"""
def sl_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtSL(func, params={}, name='crtSL'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SlippageBase, ), {'__init__': sl_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# stoploss
#------------------------------------------------------------------
StoplossBase.__init__.__doc__ = """
__init__(self[, name="StoplossBase"])
:param str name:
"""
def st_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
2020-07-17 00:26:03 +08:00
self.set_param(k, v)
2020-07-15 22:50:22 +08:00
def crtST(func, params={}, name='crtST'):
"""
/
:param func: /
:param {} params:
:param str name:
:return: /
"""
meta_x = type(name, (StoplossBase, ), {'__init__': st_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)