mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-30 19:08:48 +08:00
1、完善Python部分的指标帮助
2、修改CVAL指标,增加指定长度和抛弃点数创建常量指标函数
This commit is contained in:
parent
d3a424276e
commit
92764c16a9
@ -17,7 +17,7 @@ namespace hku {
|
||||
* @param n 计算均值的周期窗口,必须为大于2的整数,默认为10天
|
||||
* @param fast_n 对应快速周期N,默认为2
|
||||
* @param slow_n 对应慢速EMA线的N值,默认为30,不过当超过60左右该指标会收敛不会有太大的影响
|
||||
* @ingroup Indicator
|
||||
* @ingroup Indicator 具有2个结果集,result(0)为AMA,result(1)为ER
|
||||
*/
|
||||
Indicator HKU_API AMA(int n = 10, int fast_n = 2, int slow_n = 30);
|
||||
|
||||
|
@ -13,11 +13,13 @@
|
||||
namespace hku {
|
||||
|
||||
/**
|
||||
* 创建一个常数指标
|
||||
* 创建一个指定长度的常数指标
|
||||
* @param value 常量
|
||||
* @param len 长度
|
||||
* @param discard 抛弃数量,默认0
|
||||
* @ingroup Indicator
|
||||
*/
|
||||
Indicator HKU_API CVAL(double value = 0.0);
|
||||
Indicator HKU_API CVAL(double value=0.0, size_t len=0, size_t discard=0);
|
||||
|
||||
/**
|
||||
* 创建一个常数指标,其长度和输入的ind相同,其值固定为指定value
|
||||
|
@ -13,6 +13,20 @@ ConstantValue::ConstantValue() : IndicatorImp("CVAL", 1) {
|
||||
setParam<double>("value", 0.0);
|
||||
}
|
||||
|
||||
ConstantValue::ConstantValue(double value, size_t len, size_t discard)
|
||||
:IndicatorImp("CVAL", 1) {
|
||||
m_discard = discard > len ? len : discard;
|
||||
setParam<double>("value", value);
|
||||
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
_readyBuffer(len, m_result_num);
|
||||
for (size_t i = m_discard; i < len; ++i) {
|
||||
_set(value, i);
|
||||
}
|
||||
}
|
||||
|
||||
ConstantValue::~ConstantValue() {
|
||||
|
||||
}
|
||||
@ -32,9 +46,8 @@ void ConstantValue::_calculate(const Indicator& data) {
|
||||
}
|
||||
}
|
||||
|
||||
Indicator HKU_API CVAL(double value) {
|
||||
IndicatorImpPtr p = make_shared<ConstantValue>();
|
||||
p->setParam<double>("value", value);
|
||||
Indicator HKU_API CVAL(double value, size_t len, size_t discard) {
|
||||
IndicatorImpPtr p = make_shared<ConstantValue>(value, len, discard);
|
||||
return Indicator(p);
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ class ConstantValue: public IndicatorImp {
|
||||
|
||||
public:
|
||||
ConstantValue();
|
||||
ConstantValue(double value, size_t len, size_t discard);
|
||||
virtual ~ConstantValue();
|
||||
};
|
||||
|
||||
|
@ -97,7 +97,7 @@ Indicator (*VIGOR_3)(const Indicator&, int) = VIGOR;
|
||||
Indicator (*WEAVE_1)() = WEAVE;
|
||||
Indicator (*WEAVE_2)(const Indicator&) = WEAVE;
|
||||
|
||||
Indicator (*CVAL_1)(double) = CVAL;
|
||||
Indicator (*CVAL_1)(double, size_t, size_t) = CVAL;
|
||||
Indicator (*CVAL_2)(const Indicator&, double) = CVAL;
|
||||
|
||||
Indicator (*IND_AND1)(const Indicator&, const Indicator&) = IND_AND;
|
||||
@ -109,6 +109,8 @@ Indicator (*IND_OR2)(const Indicator&, price_t) = IND_OR;
|
||||
Indicator (*IND_OR3)(price_t, const Indicator&) = IND_OR;
|
||||
|
||||
void export_Indicator_build_in() {
|
||||
docstring_options doc_options(false);
|
||||
|
||||
def("KDATA", KDATA1);
|
||||
def("KDATA", KDATA2);
|
||||
def("KDATA", KDATA3);
|
||||
@ -191,8 +193,8 @@ void export_Indicator_build_in() {
|
||||
def("WEAVE", WEAVE_1);
|
||||
def("WEAVE", WEAVE_2);
|
||||
|
||||
def("CVAL", CVAL_1);
|
||||
def("CVAL", CVAL_2);
|
||||
def("CVAL", CVAL_1, (arg("value")=0.0, arg("len")=0, arg("discard")=0));
|
||||
def("CVAL", CVAL_2, (arg("data"), arg("value")=0.0));
|
||||
|
||||
def("IND_AND", IND_AND1);
|
||||
def("IND_AND", IND_AND2);
|
||||
|
@ -70,6 +70,31 @@ BOOST_AUTO_TEST_CASE( test_CVAL ) {
|
||||
BOOST_CHECK(result.empty() == true);
|
||||
BOOST_CHECK(result.discard() == 0);
|
||||
BOOST_CHECK(result.getResultNumber() == 1);
|
||||
|
||||
/** @arg 生成指定长度的常量Indicator, discard=0 */
|
||||
result = CVAL(100, 5);
|
||||
BOOST_CHECK(result.getParam<double>("value") == 100);
|
||||
BOOST_CHECK(result.size() == 5);
|
||||
BOOST_CHECK(result.empty() == false);
|
||||
BOOST_CHECK(result.discard() == 0);
|
||||
BOOST_CHECK(result.getResultNumber() == 1);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
BOOST_CHECK(result[i] == 100);
|
||||
}
|
||||
|
||||
/** @arg 生成指定长度的常量Indicator, discard=2 */
|
||||
result = CVAL(100, 5, 2);
|
||||
BOOST_CHECK(result.getParam<double>("value") == 100);
|
||||
BOOST_CHECK(result.size() == 5);
|
||||
BOOST_CHECK(result.empty() == false);
|
||||
BOOST_CHECK(result.discard() == 2);
|
||||
BOOST_CHECK(result.getResultNumber() == 1);
|
||||
BOOST_CHECK(result[0] == Null<price_t>());
|
||||
BOOST_CHECK(result[1] == Null<price_t>());
|
||||
for (int i = 2; i < 5; i++) {
|
||||
BOOST_CHECK(result[i] == 100);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
@ -87,11 +87,12 @@ OP = Operand
|
||||
|
||||
def PRICELIST(data, result_num=0, discard=0):
|
||||
"""
|
||||
将data转化为Indicator
|
||||
参数:
|
||||
data:待转化的数据
|
||||
result_num:当data为Indicator实例时,指示Indicator的第几个结果集
|
||||
discard:抛弃的数量,在 data 为 Indicator类型时无效
|
||||
将 list、tuple、Indicator 转化为普通的 Indicator
|
||||
|
||||
:param data: 输入数据,可以为 list、tuple、Indicator
|
||||
:param int result_num: 当data为Indicator实例时,指示Indicator的第几个结果集
|
||||
:param int discard: 在 data 为 Indicator类型时无效。表示前端抛弃的数据点数,抛弃的值使用 constant.null_price 填充
|
||||
:return: Indicator
|
||||
"""
|
||||
from . import _indicator as ind
|
||||
if isinstance(data, ind.Indicator):
|
||||
@ -109,6 +110,7 @@ try:
|
||||
return np.array(indicator, dtype='d')
|
||||
|
||||
def indicator_to_df(indicator):
|
||||
"""转化为pandas.DataFrame"""
|
||||
if indicator.getResultNumber() == 1:
|
||||
return pd.DataFrame(indicator_to_np(indicator), columns=[indicator.name])
|
||||
|
||||
@ -126,91 +128,267 @@ try:
|
||||
except:
|
||||
print("warning:can't import numpy or pandas lib, ",
|
||||
"you can't use method Inidicator.to_np() and to_df!")
|
||||
|
||||
|
||||
#==============================================================================
|
||||
#
|
||||
# 注释帮助
|
||||
#
|
||||
#==============================================================================
|
||||
|
||||
AMA.__doc__ = """
|
||||
AMA([data, n=10, fast_n=2, slow_n=30])
|
||||
|
||||
佩里.J 考夫曼(Perry J.Kaufman)自适应移动平均 [BOOK1]_
|
||||
|
||||
|
||||
MA.__doc__ += """\n
|
||||
移动平均线
|
||||
n: 计算均值的周期窗口,必须为大于0的整数
|
||||
type: 'SMA' | 'EMA' | 'AMA'
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 计算均值的周期窗口,必须为大于2的整数
|
||||
:param int fast_n: 对应快速周期N
|
||||
:param int slow_n: 对应慢速EMA线的N值
|
||||
:return: Indicator
|
||||
|
||||
* result(0): AMA
|
||||
* result(1): ER
|
||||
"""
|
||||
|
||||
SMA.__doc__ += """\n
|
||||
简单移动平均线
|
||||
n: 计算均值的周期窗口,必须为大于0的整数
|
||||
|
||||
AMO.__doc__ = """
|
||||
AMO([data])
|
||||
|
||||
获取成交金额,包装KData的成交金额成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
EMA.__doc__ += """\n
|
||||
指数移动平均线(Exponential Moving Average)
|
||||
n: 计算均值的周期窗口,必须为大于0的整数
|
||||
|
||||
CLOSE.__doc__ = """
|
||||
CLOSE([data])
|
||||
|
||||
获取收盘价,包装KData的收盘价成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
AMA.__doc__ += """\n
|
||||
佩里.J.考夫曼(Perry J.Kaufman)自适应移动平均,参见《精明交易者》(2006年 广东经济出版社)
|
||||
n: 计算均值的周期窗口,必须为大于2的整数,默认为10天
|
||||
slow_n: 对应慢速EMA线的N值,考夫曼一般设为30,不过当超过60左右该指标会收敛不会有太大的影响
|
||||
|
||||
CVAL.__doc__ = """
|
||||
CVAL(data[, value=0.0])
|
||||
|
||||
data 为 Indicator 实例,创建和 data 等长的常量指标,其值和为value,抛弃长度discard和data一样
|
||||
|
||||
CVAL([value=0.0, len=0, discard=0])
|
||||
|
||||
按指定的长度、抛弃数量创建常量指标
|
||||
|
||||
:param Indicator data: Indicator实例
|
||||
:param float value: 常数值
|
||||
:param int len: 长度
|
||||
:param int discard: 抛弃数量
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
DIFF.__doc__ += """\n
|
||||
差分指标,即a[i]-a[i-1]
|
||||
|
||||
DIFF.__doc__ = """
|
||||
DIFF([data])
|
||||
|
||||
差分指标,即data[i] - data[i-1]
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
MACD.__doc__ += """\n
|
||||
MACD平滑异同移动平均线
|
||||
参数:
|
||||
n1: 短期EMA时间窗
|
||||
n2: 长期EMA时间窗
|
||||
n3: (短期EMA-长期EMA)EMA平滑时间窗
|
||||
返回:(MADC_BAR, DIFF, DEA)
|
||||
|
||||
EMA.__doc__ = """
|
||||
EMA([data, n=22])
|
||||
|
||||
指数移动平均线(Exponential Moving Average)
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 计算均值的周期窗口,必须为大于0的整数
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
REF.__doc__ += """\n
|
||||
REF 向前引用 (即右移)
|
||||
引用若干周期前的数据。
|
||||
用法: REF(X,A) 引用A周期前的X值。
|
||||
例如: REF(CLOSE,1) 表示上一周期的收盘价,在日线上就是昨收。
|
||||
参数:
|
||||
n: 引用n周期前的值,即右移n位
|
||||
|
||||
HHV.__doc__ = """
|
||||
HHV([data, n=20])
|
||||
|
||||
N日内最高价
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: N日时间窗口
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
HIGH.__doc__ = """
|
||||
HIGH([data])
|
||||
|
||||
获取最高价,包装KData的最高价成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
KDATA.__doc__ = """
|
||||
KDATA([data])
|
||||
|
||||
包装KData成Indicator,用于其他指标计算
|
||||
|
||||
:param data: KData 或 具有6个返回结果的Indicator(如KDATA生成的Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
KDATA_PART.__doc__ = """
|
||||
KDATA_PART([data, kpart])
|
||||
|
||||
根据字符串选择返回指标KDATA/OPEN/HIGH/LOW/CLOSE/AMO/VOL,如:KDATA_PART("CLOSE")等同于CLOSE()
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
LLV.__doc__ = """
|
||||
LLV([data, n=20])
|
||||
|
||||
N日内最低价
|
||||
|
||||
:param data: 输入数据
|
||||
:param int n: N日时间窗口
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
LOW.__doc__ = """
|
||||
LOW([data])
|
||||
|
||||
获取最低价,包装KData的最低价成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
MA.__doc__ = """
|
||||
MA([data, n=22, type="SMA"])
|
||||
|
||||
移动平均数包装,默认为简单平均数
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 时间窗口
|
||||
:param string type: "EMA"|"SMA"|"AMA"
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
MACD.__doc__ = """
|
||||
MACD([data, n1=12, n2=26, n3=9])
|
||||
|
||||
平滑异同移动平均线
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n1: 短期EMA时间窗
|
||||
:param int n2: 长期EMA时间窗
|
||||
:param int n3: (短期EMA-长期EMA)EMA平滑时间窗
|
||||
:return: 具有三个结果集的 Indicator
|
||||
|
||||
* result(0): MACD_BAR:MACD直柱,即MACD快线-MACD慢线
|
||||
* result(1): DIFF: 快线,即(短期EMA-长期EMA)
|
||||
* result(2): DEA: 慢线,即快线的n3周期EMA平滑
|
||||
"""
|
||||
|
||||
|
||||
OPEN.__doc__ = """
|
||||
OPEN([data])
|
||||
|
||||
获取开盘价,包装KData的开盘价成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
REF.__doc__ = """
|
||||
REF([data, n])
|
||||
|
||||
向前引用 (即右移),引用若干周期前的数据。
|
||||
用法:REF(X,A) 引用A周期前的X值。
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 引用n周期前的值,即右移n位
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
SAFTYLOSS.__doc__ += """\n
|
||||
亚历山大.艾尔德安全地带止损
|
||||
参见《走进我的交易室》(2007年 地震出版社) 亚历山大.艾尔德(Alexander Elder) P202
|
||||
计算说明:在回溯周期内(一般为10到20天),将所有向下穿越的长度相加除以向下穿越的次数,
|
||||
得到噪音均值(即回溯期内所有最低价低于前一日最低价的长度除以次数),并用今日
|
||||
最低价减去(前日噪音均值乘以一个倍数)得到该止损线。为了抵消波动并且保证止损线的
|
||||
上移,在上述结果的基础上再取起N日(一般为3天)内的最高值
|
||||
注意:返回结果中前(回溯周期宽度+去最高值的宽度)个点是无效的
|
||||
参数:
|
||||
n1 整型,计算平均噪音的回溯时间窗口
|
||||
n2 整型,对初步止损线去n2日内的最高值
|
||||
p 噪音系数
|
||||
"""
|
||||
SAFTYLOSS.__doc__ = """
|
||||
SAFTYLOSS([data, n1=10, n2=3, p=2.0])
|
||||
|
||||
STDEV.__doc__ += """\n
|
||||
N日周期内样本标准差"""
|
||||
|
||||
VIGOR.__doc__ += """\n
|
||||
亚历山大.艾尔德力度指数
|
||||
参见《走进我的交易室》(2007年 地震出版社) (Alexander Elder) P131
|
||||
计算公式:(收盘价今-收盘价昨)*成交量今
|
||||
一般可以再使用EMA进行平滑, 参数n即为EMA平滑参数
|
||||
|
||||
VIGOR( (KData)arg1 ) -> Indicator
|
||||
"""
|
||||
亚历山大 艾尔德安全地带止损线,参见 [BOOK2]_
|
||||
|
||||
HHV.__doc__ += """\n
|
||||
N日内最高价
|
||||
参数:
|
||||
n:时间周期窗口
|
||||
计算说明:在回溯周期内(一般为10到20天),将所有向下穿越的长度相加除以向下穿越的次数,得到噪音均值(即回溯期内所有最低价低于前一日最低价的长度除以次数),并用今日最低价减去(前日噪音均值乘以一个倍数)得到该止损线。为了抵消波动并且保证止损线的上移,在上述结果的基础上再取起N日(一般为3天)内的最高值
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n1: 计算平均噪音的回溯时间窗口
|
||||
:param int n2: 对初步止损线去n2日内的最高值
|
||||
:param float p: 噪音系数
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
LLV.__doc__ += """\n
|
||||
N日内最低价
|
||||
参数:
|
||||
n:时间周期窗口
|
||||
|
||||
SMA.__doc__ = """
|
||||
SMA([data, n=22])
|
||||
|
||||
简单移动平均线
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 时间窗口
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
STDEV.__doc__ = """
|
||||
STDEV([data, n=10])
|
||||
|
||||
计算N周期内样本标准差
|
||||
|
||||
:param Indicator data: 输入数据
|
||||
:param int n: 时间窗口
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
VIGOR.__doc__ = """
|
||||
VIGOR(data[, n=2])
|
||||
|
||||
亚历山大.艾尔德力度指数 [BOOK2]_
|
||||
|
||||
计算公式:(收盘价今-收盘价昨)*成交量今
|
||||
|
||||
:param data: 输入数据(KData 或 具有6个结果集的Indicator)
|
||||
:param int n: EMA平滑窗口
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
VOL.__doc__ = """
|
||||
VOL([data])
|
||||
|
||||
获取成交量,包装KData的成交量成Indicator
|
||||
|
||||
:param data: 输入数据(KData 或 Indicator)
|
||||
:return: Indicator
|
||||
"""
|
||||
|
||||
|
||||
#==============================================================================
|
||||
#
|
||||
# Talib包装
|
||||
#
|
||||
#==============================================================================
|
||||
|
||||
try:
|
||||
import talib
|
||||
import talib.abstract as ta
|
||||
|
Loading…
Reference in New Issue
Block a user