Skip to content

Commit

Permalink
Merge pull request #194 from fasiondog/feature/factor
Browse files Browse the repository at this point in the history
add MultiFactor
  • Loading branch information
fasiondog committed Mar 16, 2024
2 parents 33c252c + d62e2fb commit c60c2f4
Show file tree
Hide file tree
Showing 33 changed files with 1,823 additions and 100 deletions.
96 changes: 48 additions & 48 deletions hikyuu/trade_sys/trade_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hikyuu.util.slice import list_getitem
from hikyuu.core import (
System, SystemPart, ConditionBase, EnvironmentBase, MoneyManagerBase,
ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase
ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase, AllocateFundsBase
)


Expand All @@ -15,6 +15,14 @@ def part_iter(self):
ConditionBase.__iter__ = part_iter


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)


# ------------------------------------------------------------------
# System
# ------------------------------------------------------------------
Expand All @@ -33,14 +41,6 @@ def part_iter(self):
# ------------------------------------------------------------------
# condition
# ------------------------------------------------------------------
def cn_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)


def crtCN(func, params={}, name='crtCN'):
"""
快速创建自定义不带私有属性的系统有效条件
Expand All @@ -50,7 +50,7 @@ def crtCN(func, params={}, name='crtCN'):
:param str name: 自定义名称
:return: 自定义系统有效条件实例
"""
meta_x = type(name, (ConditionBase, ), {'__init__': cn_init})
meta_x = type(name, (ConditionBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand All @@ -59,14 +59,6 @@ def crtCN(func, params={}, name='crtCN'):
# ------------------------------------------------------------------
# environment
# ------------------------------------------------------------------
def ev_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)
def crtEV(func, params={}, name='crtEV'):
"""
快速创建自定义不带私有属性的市场环境判断策略
Expand All @@ -76,7 +68,7 @@ def crtEV(func, params={}, name='crtEV'):
:param str name: 自定义名称
:return: 自定义市场环境判断策略实例
"""
meta_x = type(name, (EnvironmentBase, ), {'__init__': ev_init})
meta_x = type(name, (EnvironmentBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand All @@ -85,14 +77,6 @@ def crtEV(func, params={}, name='crtEV'):
# ------------------------------------------------------------------
# moneymanager
# ------------------------------------------------------------------
def mm_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)
def crtMM(func, params={}, name='crtMM'):
"""
快速创建自定义不带私有属性的资金管理策略
Expand All @@ -102,7 +86,7 @@ def crtMM(func, params={}, name='crtMM'):
:param str name: 自定义名称
:return: 自定义资金管理策略实例
"""
meta_x = type(name, (MoneyManagerBase, ), {'__init__': mm_init})
meta_x = type(name, (MoneyManagerBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand All @@ -111,14 +95,6 @@ def crtMM(func, params={}, name='crtMM'):
# ------------------------------------------------------------------
# profitgoal
# ------------------------------------------------------------------
def pg_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)
def crtPG(func, params={}, name='crtPG'):
"""
快速创建自定义不带私有属性的盈利目标策略
Expand All @@ -128,7 +104,7 @@ def crtPG(func, params={}, name='crtPG'):
:param str name: 自定义名称
:return: 盈利目标策略实例
"""
meta_x = type(name, (ProfitGoalBase, ), {'__init__': pg_init})
meta_x = type(name, (ProfitGoalBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand All @@ -137,14 +113,6 @@ def crtPG(func, params={}, name='crtPG'):
# ------------------------------------------------------------------
# signal
# ------------------------------------------------------------------
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.set_param(k, v)
def crtSG(func, params={}, name='crtSG'):
"""
快速创建自定义不带私有属性的信号指示器
Expand All @@ -154,7 +122,7 @@ def crtSG(func, params={}, name='crtSG'):
:param str name: 自定义名称
:return: 自定义信号指示器实例
"""
meta_x = type(name, (SignalBase, ), {'__init__': sig_init})
meta_x = type(name, (SignalBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand All @@ -175,11 +143,43 @@ def se_add_stock_list(self, stk_list, proto_sys):
SelectorBase.add_stock_list = se_add_stock_list
def crtSE(func, params={}, name='crtSE'):
"""
快速创建自定义不带私有属性的交易对象选择算法

:param func: 交易对象选择算法
:param {} params: 参数字典
:param str name: 自定义名称
:return: 自定义交易对象选择算法实例
"""
meta_x = type(name, (SelectorBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
# ------------------------------------------------------------------
# slippage
# allocatefunds
# ------------------------------------------------------------------
def crtAF(func, params={}, name='crtAF'):
"""
快速创建自定义不带私有属性的资产分配算法

:param func: 资产分配算法
:param {} params: 参数字典
:param str name: 自定义名称
:return: 自定义资产分配算法实例
"""
meta_x = type(name, (AllocateFundsBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
# ------------------------------------------------------------------
# slippage
# ------------------------------------------------------------------
def sl_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
Expand All @@ -197,7 +197,7 @@ def crtSL(func, params={}, name='crtSL'):
:param str name: 自定义名称
:return: 移滑价差算法实例
"""
meta_x = type(name, (SlippageBase, ), {'__init__': sl_init})
meta_x = type(name, (SlippageBase, ), {'__init__': part_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
Expand Down
4 changes: 4 additions & 0 deletions hikyuu_cpp/hikyuu/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
* @details 交易系统框架
* @ingroup TradeSystem
*
* @defgroup MultiFactor MultiFactor 合成多因子
* @details 合成多因子
* @ingroup TradeSystem
*
* @defgroup SystemInstance SystemInstance 系统实例
* @details 系统实例
* @ingroup Hikyuu
Expand Down
15 changes: 15 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/Indicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ bool Indicator::alike(const Indicator& other) const {
return m_imp->alike(*other.m_imp);
}

bool Indicator::equal(const Indicator& other) const {
HKU_IF_RETURN(this == &other || m_imp == other.m_imp, true);
HKU_IF_RETURN(size() != other.size() || discard() != other.discard(), false);
auto const* d1 = this->data();
auto const* d2 = other.data();
for (size_t i = 0, total = size(); i < total; i++) {
HKU_IF_RETURN(
(std::isnan(d1[i]) && !std::isnan(d2[i])) || (!std::isnan(d1[i]) && std::isnan(d2[i])),
false);
HKU_IF_RETURN(
(!std::isnan(d1[i]) && !std::isnan(d2[i])) && (std::abs(d1[i] - d2[i]) >= 0.0001), false);
}
return true;
}

Indicator& Indicator::operator=(const Indicator& indicator) {
HKU_IF_RETURN(this == &indicator, *this);
m_imp = indicator.m_imp;
Expand Down
14 changes: 14 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/Indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ class HKU_API Indicator {
return m_imp ? m_imp->data(result_num) : nullptr;
}

/**
* 判断两个ind的值是否相等
* @note operator==重载生成新的新的Indicator,此函数用于对两个ind进行值比较
*/
bool equal(const Indicator& other) const;

/** 判断是否是同一个实例 */
bool isSame(const Indicator& other) const {
return !m_imp && m_imp == other.m_imp;
}

protected:
IndicatorImpPtr m_imp;

Expand All @@ -197,6 +208,9 @@ class HKU_API Indicator {
#endif /* HKU_SUPPORT_SERIALIZATION */
};

/** @ingroup Indicator */
typedef vector<Indicator> IndicatorList;

inline string Indicator::name() const {
return m_imp ? m_imp->name() : "IndicatorImp";
}
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/indicator/crt/ICIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace hku {
* @return Indicator
* @ingroup Indicator
*/
inline Indicator ICIR(const Indicator& ic, int n = 10) {
inline Indicator ICIR(const Indicator& ic, int n = 120) {
Indicator x = MA(ic, n) / STDEV(ic, n);
x.name("IR");
x.setParam<int>("n", n);
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/indicator/crt/ROCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace hku {

/**
* 变动率指标 (price - prePrice) / prevPrice
* 变动率指标 (price - prePrice) / prevPrice N 日收益率 (本金之外的盈利)
* @ingroup Indicator
*/
Indicator HKU_API ROCP(int n = 10);
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/indicator/crt/ROCR.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace hku {

/**
* 变动率指标 (price / prevPrice)
* 变动率指标 (price / prevPrice), N 日累积收益率 (包含本金)
* @ingroup Indicator
*/
Indicator HKU_API ROCR(int n = 10);
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/indicator/imp/IEma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BOOST_CLASS_EXPORT(hku::IEma)

namespace hku {

IEma::IEma() : IndicatorImp("IEma", 1) {
IEma::IEma() : IndicatorImp("EMA", 1) {
setParam<int>("n", 22);
}

Expand Down

0 comments on commit c60c2f4

Please sign in to comment.