hikyuu2/hikyuu/trade_sys/trade_sys.py

226 lines
7.9 KiB
C++
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-16 04:24:31 +08:00
ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase, AllocateFundsBase
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
2024-03-16 04:24:31 +08:00
def part_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.set_param(k, v)
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
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtCN(func, params={}, name='crtCN', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (ConditionBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtEV(func, params={}, name='crtEV', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (EnvironmentBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtMM(func, params={}, name='crtMM', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (MoneyManagerBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# profitgoal
2023-12-28 02:21:36 +08:00
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtPG(func, params={}, name='crtPG', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (ProfitGoalBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtSG(func, params={}, name='crtSG', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (SignalBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# ------------------------------------------------------------------
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
2024-03-16 04:24:31 +08:00
2024-03-20 18:22:30 +08:00
def crtSE(calculate, get_selected, is_match_af=None, params={}, name='crtSE', clone=None):
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:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2024-03-16 04:24:31 +08:00
:return:
"""
meta_x = type(name, (SelectorBase, ), {'__init__': part_init})
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
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else clone
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
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtAF(allocate_weight_func, params={}, name='crtAF', clone=None):
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:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2024-03-16 04:24:31 +08:00
:return:
"""
meta_x = type(name, (AllocateFundsBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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-16 04:24:31 +08:00
# ------------------------------------------------------------------
# slippage
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtSL(func, params={}, name='crtSL', clone=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
2020-07-15 22:50:22 +08:00
:param func:
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return:
"""
2024-03-16 04:24:31 +08:00
meta_x = type(name, (SlippageBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else 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
# stoploss
2023-12-28 02:21:36 +08:00
# ------------------------------------------------------------------
2024-03-16 16:56:37 +08:00
def crtST(func, params={}, name='crtST', clone=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
2020-07-15 22:50:22 +08:00
:param func: /
:param {} params:
:param str name:
2024-03-16 16:56:37 +08:00
:param clone: clone函数
2020-07-15 22:50:22 +08:00
:return: /
"""
2024-03-16 16:56:37 +08:00
meta_x = type(name, (StoplossBase, ), {'__init__': part_init})
2024-03-27 17:48:21 +08:00
meta_x._clone = (lambda self: meta_x(self._name, self._params)) if clone is None else clone
2020-07-15 22:50:22 +08:00
meta_x._calculate = func
return meta_x(name, params)