1、完善Python部分的指标帮助

2、修改CVAL指标,增加指定长度和抛弃点数创建常量指标函数
This commit is contained in:
fasiondog 2017-09-22 00:36:18 +08:00
parent d3a424276e
commit 92764c16a9
7 changed files with 298 additions and 77 deletions

View File

@ -17,7 +17,7 @@ namespace hku {
* @param n 210
* @param fast_n N2
* @param slow_n EMA线的N值3060
* @ingroup Indicator
* @ingroup Indicator 2result(0)AMAresult(1)ER
*/
Indicator HKU_API AMA(int n = 10, int fast_n = 2, int slow_n = 30);

View File

@ -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

View File

@ -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);
}

View File

@ -18,6 +18,7 @@ class ConstantValue: public IndicatorImp {
public:
ConstantValue();
ConstantValue(double value, size_t len, size_t discard);
virtual ~ConstantValue();
};

View File

@ -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);

View File

@ -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);
}
}
/** @} */

View File

@ -87,11 +87,12 @@ OP = Operand
def PRICELIST(data, result_num=0, discard=0):
"""
data转化为Indicator
data
result_numdata为Indicator实例时Indicator的第几个结果集
discard data Indicator类型时无效
listtupleIndicator Indicator
:param data: listtupleIndicator
: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.Kaufman2006 广
n: 210
slow_n: EMA线的N值3060
CVAL.__doc__ = """
CVAL(data[, value=0.0])
data Indicator data valuediscard和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-EMAEMA平滑时间窗
(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(XA) A周期前的X值
 REF(CLOSE1) 线
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 6IndicatorKDATA生成的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-EMAEMA平滑时间窗
:return: Indicator
* result(0): MACD_BARMACD直柱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(XA) A周期前的X值
:param Indicator data:
:param int n: n周期前的值n位
:return: Indicator
"""
SAFTYLOSS.__doc__ += """\n
.
2007 .(Alexander Elder) P202
1020穿穿
线线
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
1020穿穿线线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 6Indicator
: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