Skip to content

Commit

Permalink
Merge pull request #192 from fasiondog/feature/factor
Browse files Browse the repository at this point in the history
add IC/IR/ICIR/ZSCORE 指标; 调整 CORR/SPEARMN 实现
  • Loading branch information
fasiondog committed Mar 10, 2024
2 parents d51bfa6 + eb029b9 commit debee37
Show file tree
Hide file tree
Showing 32 changed files with 1,402 additions and 342 deletions.
63 changes: 58 additions & 5 deletions docs/source/indicator/indicator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
:rtype: Indicator


.. py:function:: ALIGN(data, ref[, use_null=True])
.. py:function:: ALIGN(data, ref[, fill_null=True])
按指定的参考日期对齐

:param Indicator data: 输入数据
:param DatetimeList|Indicator|KData ref: 指定做为日期参考的 DatetimeList、Indicator 或 KData
:param bool use_null: 缺失数据使用 nan 填充; 否则使用小于对应日期且最接近对应日期的数据
:param bool fill_null: 缺失数据使用 nan 填充; 否则使用小于对应日期且最接近对应日期的数据
:retype: Indicator

.. py:function:: AMA([data, n=10, fast_n=2, slow_n=30])
Expand Down Expand Up @@ -207,7 +207,7 @@

:param Indicator ind1: 指标1
:param Indicator ind2: 指标2
:param int n: 按指定 n 的长度计算两个 ind 直接数据相关系数
:param int n: 按指定 n 的长度计算两个 ind 直接数据相关系数。如果为0,使用输入的ind长度。
:rtype: Indicator


Expand Down Expand Up @@ -448,6 +448,46 @@
:param KData kdata: k线数据
:rtype: Indicator

.. py:function:: IC(ind, stks, query, n, ref_stk)
计算指定的因子相对于参考证券的 IC (实际为 RankIC)

:param sequence(stock)|Block stks 证券组合
:param Query query: 查询条件
:param int n: 时间窗口
:param Stock ref_stk: 参照证券,通常使用 sh000300 沪深300
:rtype: Indicator


.. py:function:: ICIR(ic[,n])
计算 IC 因子 IR = IC的多周期均值/IC的标准方差

:param Indicator: ic 已经计算出的 ic 值
:param int n: 时间窗口
:rtype: Indicator


.. py:function:: IR(p, b[, n=100])
信息比率(Information Ratio,IR)

公式: (P-B) / TE
P: 组合收益率
B: 比较基准收益率
TE: 投资周期中每天的 p 和 b 之间的标准差
实际使用时,P 一般为 TM 的资产曲线,B 为沪深 3000 收盘价,如:
ref_k = sm["sh000300"].get_kdata(query)
funds = my_tm.get_funds_curve(ref_k.get_datetime.list())
ir = IR(PRICELIST(funds), ref_k.close, 0)

如果希望计算因子 IC 的 IR 值,请使用 ICIR 指标

:param Indicator p:
:param Indicator b:
:param int n: 时间窗口(默认100),如果只想使用最后的值,可以使用 0, 或 len(p),len(b) 指定
:rtype: Indicator


.. py:function:: IF(x, a, b)
Expand Down Expand Up @@ -869,7 +909,7 @@

:param Indicator ind1: 输入参数1
:param Indicator ind2: 输入参数2
:param int n: 指定窗口
:param int n: 滚动窗口(大于2 或 等于0),等于0时,代表 n 实际使用 ind 的长度


.. py:function:: SQRT([data])
Expand Down Expand Up @@ -1041,4 +1081,17 @@
获取10年期中国国债收益率

:param DatetimeList|KDate|Indicator data: 输入的日期参考,优先使用上下文中的日期
:param float default_val: 如果输入的日期早于已有国债数据的最早记录,则使用此默认值
:param float default_val: 如果输入的日期早于已有国债数据的最早记录,则使用此默认值


.. py:function:: ZSCORE([data, out_extreme, nsigma, recursive])
对数据进行标准化(归一),可选进行极值排除

注:非窗口滚动,如需窗口滚动的标准化,直接 (x - MA(x, n)) / STDEV(x, n) 即可。

:param Indicator data: 待剔除异常值的数据
:param bool outExtreme: 指示剔除极值,默认 False
:param float nsigma: 剔除极值时使用的 nsigma 倍 sigma ,默认 3.0
:param bool recursive: 是否进行递归剔除极值, 默认 False
:rtype: Indicator
7 changes: 7 additions & 0 deletions docs/source/indicator/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* :py:func:`DROPNA` - 删除 nan 值
* :py:func:`PRICELIST` - 将PriceList或Indicator的结果集包装为Indicator,同名 VALUE
* :py:func:`WEAVE` - 将两个ind的结果合并到一个ind中
* :py:func:`ZSCORE` - ZScore 标准化


**行情指标**
Expand Down Expand Up @@ -132,6 +133,12 @@
* :py:func:`HOUR` - 取得该周期的小时数
* :py:func:`MINUTE` - 取得该周期的分钟数

**因子类指标**

* :py:func:`IC` - 计算因子 IC 值
* :py:func:`IR` - 用于计算账户收益与参照收益的IR
* :py:func:`ICIR` - 计算因子 IC 的 IR 值


**Ta-lib指标**

Expand Down
20 changes: 0 additions & 20 deletions hikyuu_cpp/hikyuu/indicator/Indicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,4 @@ Indicator HKU_API IF(const Indicator& x, Indicator::value_t a, Indicator::value_
return IF(x, CVAL(x, a), CVAL(x, b));
}

Indicator HKU_API CORR(const Indicator& ind1, const Indicator& ind2, int n) {
HKU_ERROR_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator(),
"ind1 or ind2 is Null Indicator!");
HKU_ERROR_IF_RETURN(n < 2, Indicator(), "Invalid param n: {} (need >= 2)", n);
IndicatorImpPtr p = make_shared<IndicatorImp>("CORR");
p->setParam<int>("n", n);
p->add(IndicatorImp::CORR, ind1.getImp(), ind2.getImp());
return p->calculate();
}

Indicator HKU_API SPEARMAN(const Indicator& ind1, const Indicator& ind2, int n) {
HKU_ERROR_IF_RETURN(!ind1.getImp() || !ind2.getImp(), Indicator(),
"ind1 or ind2 is Null Indicator!");
HKU_ERROR_IF_RETURN(n < 2, Indicator(), "Invalid param n: {} (need >= 2)", n);
IndicatorImpPtr p = make_shared<IndicatorImp>("SPEARMAN");
p->setParam<int>("n", n);
p->add(IndicatorImp::SPEARMAN, ind1.getImp(), ind2.getImp());
return p->calculate();
}

} /* namespace hku */
16 changes: 0 additions & 16 deletions hikyuu_cpp/hikyuu/indicator/Indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,6 @@ Indicator HKU_API IF(const Indicator& x, Indicator::value_t a, const Indicator&
Indicator HKU_API IF(const Indicator& x, const Indicator& a, Indicator::value_t b);
Indicator HKU_API IF(const Indicator& x, Indicator::value_t a, Indicator::value_t b);

/**
* 计算样本相关系数与协方差。返回的结果集中,第一个为相关系数,第二个为协方差
* @param ind1 指标1
* @param ind2 指标2
* @ingroup Indicator
*/
Indicator HKU_API CORR(const Indicator& ind1, const Indicator& ind2, int n);

/**
* Spearman 相关系数
* @param ind1 指标1
* @param ind2 指标2
* @ingroup Indicator
*/
Indicator HKU_API SPEARMAN(const Indicator& ind1, const Indicator& ind2, int n);

} /* namespace hku */

#if FMT_VERSION >= 90000
Expand Down

0 comments on commit debee37

Please sign in to comment.