hikyuu2/hikyuu/trade_sys/trade_sys.py

233 lines
7.8 KiB
Python
Raw Normal View History

2020-07-15 22:50:22 +08:00
# -*- coding: utf8 -*-
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,
2024-03-30 14:51:36 +08:00
ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase, AllocateFundsBase,
MultiFactorBase
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
def part_init(self, name='', params={}):
2024-03-16 04:24:31 +08:00
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.set_param(k, v)
def part_clone(self):
cloned = self.__class__.__new__(self.__class__)
self.__class__.__init__(cloned, self)
cloned.__dict__.update(self.__dict__)
return cloned
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
# ------------------------------------------------------------------
def crtCN(func, params={}, name='crtCN'):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建系统有效条件
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__': part_init, '_clone': part_clone})
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtEV(func, params={}, name='crtEV'):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建市场环境判断策略
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__': part_init, '_clone': part_clone})
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtMM(get_buy_num, get_sell_num, params={}, name='crtMM', buy_notify=None, sell_notify=None):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建资金管理策略
2023-12-28 02:21:36 +08:00
:param get_buy_num: 买入数量接口
:param sell_buy_num: 卖出数量接口
2020-07-15 22:50:22 +08:00
:param {} params: 参数字典
:param str name: 自定义名称
:param buy_notify: 接收买入交易记录通知
:param sell_notify: 接收卖出交易记录通知
2020-07-15 22:50:22 +08:00
:return: 自定义资金管理策略实例
"""
meta_x = type(name, (MoneyManagerBase, ), {'__init__': part_init, '_clone': part_clone})
meta_x._get_buy_num = get_buy_num
meta_x._get_sell_num = get_sell_num
if buy_notify is not None:
meta_x._buy_notify = buy_notify
if sell_notify is not None:
meta_x._sell_notify = sell_notify
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtPG(func, params={}, name='crtPG'):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建盈利目标策略
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__': part_init, '_clone': part_clone})
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtSG(func, params={}, name='crtSG'):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建信号指示器
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__': part_init, '_clone': part_clone})
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtSE(calculate, get_selected, is_match_af=None, params={}, name='crtSE'):
2024-03-16 04:24:31 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建交易对象选择算法
2024-03-16 04:24:31 +08:00
2024-03-16 16:56:37 +08:00
:param calculate function: 计算函数
:param get_selected_on_close function: 收盘时刻选择算法
:param get_selected_on_open function: 开盘时刻选择算法
2024-03-16 04:24:31 +08:00
:param {} params: 参数字典
:param str name: 自定义名称
:return: 自定义交易对象选择算法实例
"""
meta_x = type(name, (SelectorBase, ), {'__init__': part_init, '_clone': part_clone})
2024-03-16 16:56:37 +08:00
meta_x._calculate = calculate
2024-03-20 18:22:30 +08:00
meta_x.get_selected = get_selected
2024-03-27 17:48:21 +08:00
meta_x.is_match_af = (lambda self, af: True) if is_match_af is None else is_match_af
2024-03-16 04:24:31 +08:00
return meta_x(name, params)
2023-12-28 02:21:36 +08:00
# ------------------------------------------------------------------
2024-03-16 04:24:31 +08:00
# allocatefunds
2023-12-28 02:21:36 +08:00
# ------------------------------------------------------------------
def crtAF(allocate_weight_func, params={}, name='crtAF'):
2024-03-16 04:24:31 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建资产分配算法
2024-03-16 04:24:31 +08:00
2024-03-16 16:56:37 +08:00
:param allocate_weight_func: 资产分配算法
2024-03-16 04:24:31 +08:00
:param {} params: 参数字典
:param str name: 自定义名称
:return: 自定义资产分配算法实例
"""
meta_x = type(name, (AllocateFundsBase, ), {'__init__': part_init, '_clone': part_clone})
2024-03-16 16:56:37 +08:00
meta_x._allocate_weight = allocate_weight_func
2024-03-16 04:24:31 +08:00
return meta_x(name, params)
2020-07-15 22:50:22 +08:00
2024-03-30 14:51:36 +08:00
# ------------------------------------------------------------------
# multi_factor
# ------------------------------------------------------------------
def crtMF(calculate_func, params={}, name='crtMF'):
"""
快速多因子合成算法
:param calculate_func: 合成算法
:param {} params: 参数字典
:param str name: 自定义名称
:return: 自定义多因子合成算法实例
"""
meta_x = type(name, (MultiFactorBase, ), {'__init__': part_init, '_clone': part_clone})
meta_x._calculate = calculate_func
return meta_x(name, params)
2024-03-16 04:24:31 +08:00
# ------------------------------------------------------------------
# slippage
# ------------------------------------------------------------------
def crtSP(get_real_buy_price, get_real_sell_price, params={}, name='crtSP', calculate=None):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建移滑价差算法
2023-12-28 02:21:36 +08:00
:param get_real_buy_price: 移滑价差算法接口计算实际买入价格
:param get_real_sell_price: 移滑价差算法接口计算实际买入价格
2020-07-15 22:50:22 +08:00
:param {} params: 参数字典
:param str name: 自定义名称
:param calculate: 预处理函数
2020-07-15 22:50:22 +08:00
:return: 移滑价差算法实例
"""
meta_x = type(name, (SlippageBase, ), {'__init__': part_init, '_clone': part_clone})
meta_x.get_real_buy_price = get_real_buy_price
meta_x.get_real_sell_price = get_real_sell_price
if calculate is not None:
meta_x._calculate = calculate
2020-07-15 22:50:22 +08:00
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
# ------------------------------------------------------------------
def crtST(func, params={}, name='crtST'):
2020-07-15 22:50:22 +08:00
"""
2024-03-16 16:56:37 +08:00
快速创建止损/止盈策略
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__': part_init, '_clone': part_clone})
2020-07-15 22:50:22 +08:00
meta_x._calculate = func
return meta_x(name, params)