HHV 支持动态参数

This commit is contained in:
fasiondog 2022-02-10 23:08:49 +08:00
parent ea9ef4e98d
commit ec3e9ea49b
6 changed files with 47 additions and 5 deletions

View File

@ -382,7 +382,7 @@
N日内最高价N=0则从第一个有效值开始。
:param Indicator data: 输入数据
:param int n: N日时间窗口
:param int|Indicator n: N日时间窗口
:rtype: Indicator

View File

@ -19,6 +19,7 @@ namespace hku {
* @ingroup Indicator
*/
Indicator HKU_API HHV(int n = 20);
Indicator HKU_API HHV(const IndParam& n);
/**
* N日内最高价, N=0
@ -27,6 +28,7 @@ Indicator HKU_API HHV(int n = 20);
* @ingroup Indicator
*/
Indicator HKU_API HHV(const Indicator& ind, int n = 20);
Indicator HKU_API HHV(const Indicator& ind, const Indicator& n);
} // namespace hku

View File

@ -86,14 +86,34 @@ void IHighLine::_calculate(const Indicator& ind) {
}
}
void IHighLine::_dyn_run_one_step(const Indicator& ind, size_t start, size_t curPos) {
price_t max_val = ind[start];
for (size_t i = start + 1; i <= curPos; i++) {
if (ind[i] > max_val) {
max_val = ind[i];
}
}
_set(max_val, curPos);
}
Indicator HKU_API HHV(int n = 20) {
IndicatorImpPtr p = make_shared<IHighLine>();
p->setParam<int>("n", n);
return Indicator(p);
}
Indicator HKU_API HHV(const IndParam& n) {
IndicatorImpPtr p = make_shared<IHighLine>();
p->setIndParam("n", n);
return Indicator(p);
}
Indicator HKU_API HHV(const Indicator& ind, int n = 20) {
return HHV(n)(ind);
}
Indicator HKU_API HHV(const Indicator& ind, const Indicator& n) {
return HHV(IndParam(n))(ind);
}
} /* namespace hku */

View File

@ -20,7 +20,7 @@ namespace hku {
* n: N日时间窗口
*/
class IHighLine : public IndicatorImp {
INDICATOR_IMP(IHighLine)
INDICATOR_IMP_SUPPORT_IND_PARAM(IHighLine)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:

View File

@ -81,6 +81,22 @@ TEST_CASE("test_HHV") {
}
}
/** @par 检测点 */
TEST_CASE("test_HHV_dyn") {
PriceList a;
for (int i = 0; i < 10; ++i) {
a.push_back(i);
}
Indicator data = PRICELIST(a);
Indicator expect = HHV(data, 3);
Indicator result = HHV(data, CVAL(data, 3));
CHECK_EQ(expect.size(), result.size());
for (size_t i = 0; i < expect.size(); i++) {
CHECK_EQ(expect[i], result[i]);
}
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------

View File

@ -77,7 +77,9 @@ Indicator (*STDP_1)(int) = STDP;
Indicator (*STDP_2)(const Indicator&, int) = STDP;
Indicator (*HHV_1)(int) = HHV;
Indicator (*HHV_2)(const Indicator&, int) = HHV;
Indicator (*HHV_2)(const IndParam&) = HHV;
Indicator (*HHV_3)(const Indicator&, const Indicator&) = HHV;
Indicator (*HHV_4)(const Indicator&, int) = HHV;
Indicator (*LLV_1)(int) = LLV;
Indicator (*LLV_2)(const Indicator&, int) = LLV;
@ -503,12 +505,14 @@ void export_Indicator_build_in() {
def("POS", POS, (arg("block"), arg("query"), arg("sg")));
def("HHV", HHV_1, (arg("n") = 20));
def("HHV", HHV_2, (arg("data"), arg("n") = 20), R"(HHV([data, n=20])
def("HHV", HHV_2, (arg("n")));
def("HHV", HHV_3, (arg("data"), arg("n")));
def("HHV", HHV_4, (arg("data"), arg("n") = 20), R"(HHV([data, n=20])
N日内最高价N=0
:param Indicator data:
:param int n: N日时间窗口
:param int|Indicator n: N日时间窗口
:rtype: Indicator)");
def("LLV", LLV_1, (arg("n") = 20));