2020-07-15 22:50:22 +08:00
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
|
|
|
from hikyuu.util.slice import list_getitem
|
2024-01-26 04:46:44 +08:00
|
|
|
from hikyuu.core import (
|
2023-12-29 17:19:14 +08:00
|
|
|
System, SystemPart, ConditionBase, EnvironmentBase, MoneyManagerBase,
|
2021-11-18 22:44:01 +08:00
|
|
|
ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase
|
2020-07-15 22:50:22 +08:00
|
|
|
)
|
|
|
|
|
2024-02-18 00:01:24 +08:00
|
|
|
|
|
|
|
def part_iter(self):
|
|
|
|
for i in range(len(self)):
|
|
|
|
yield self[i]
|
|
|
|
|
|
|
|
|
|
|
|
ConditionBase.__iter__ = part_iter
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# System
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
System.Part = SystemPart
|
2020-07-15 22:50:22 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
|
|
|
# condition
|
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的系统有效条件
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# environment
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的市场环境判断策略
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# moneymanager
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的资金管理策略
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# profitgoal
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的盈利目标策略
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# signal
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的信号指示器
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-08-31 23:51:24 +08:00
|
|
|
# Selector
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-08-31 23:51:24 +08:00
|
|
|
def se_add_stock_list(self, stk_list, proto_sys):
|
|
|
|
result = True
|
|
|
|
for stk in stk_list:
|
|
|
|
success = self.add_stock(stk, proto_sys)
|
|
|
|
if not success:
|
|
|
|
result = False
|
|
|
|
break
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
SelectorBase.add_stock_list = se_add_stock_list
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# slippage
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的移滑价差算法
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|
|
|
|
|
|
|
|
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
# stoploss
|
2023-12-28 02:21:36 +08:00
|
|
|
# ------------------------------------------------------------------
|
2020-07-15 22:50:22 +08:00
|
|
|
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'):
|
|
|
|
"""
|
|
|
|
快速创建自定义不带私有属性的止损/止盈策略
|
2023-12-28 02:21:36 +08:00
|
|
|
|
2020-07-15 22:50:22 +08:00
|
|
|
: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)
|