MA 支持动态参数

This commit is contained in:
fasiondog 2022-02-11 00:22:57 +08:00
parent a68c39cb74
commit d28a48a74a
8 changed files with 62 additions and 16 deletions

View File

@ -550,7 +550,7 @@
简单移动平均
:param Indicator data: 输入数据
:param int n: 时间窗口
:param int|Indicator n: 时间窗口
:rtype: Indicator

View File

@ -21,6 +21,8 @@ namespace hku {
*/
Indicator HKU_API MA(const Indicator& data, int n = 22);
Indicator HKU_API MA(int n = 22);
Indicator HKU_API MA(const IndParam& n);
Indicator HKU_API MA(const Indicator& data, const Indicator& n);
} // namespace hku

View File

@ -47,14 +47,32 @@ void IMa::_calculate(const Indicator& indicator) {
}
}
void IMa::_dyn_run_one_step(const Indicator& ind, size_t start, size_t curPos) {
price_t sum = 0.0;
for (size_t i = start; i <= curPos; i++) {
sum += ind[i];
}
_set(sum / (curPos - start + 1), curPos);
}
Indicator HKU_API MA(int n) {
IndicatorImpPtr p = make_shared<IMa>();
p->setParam<int>("n", n);
return Indicator(p);
}
Indicator HKU_API MA(const IndParam& n) {
IndicatorImpPtr p = make_shared<IMa>();
p->setIndParam("n", n);
return Indicator(p);
}
Indicator HKU_API MA(const Indicator& ind, int n) {
return MA(n)(ind);
}
Indicator HKU_API MA(const Indicator& ind, const Indicator& n) {
return MA(IndParam(n))(ind);
}
} /* namespace hku */

View File

@ -14,7 +14,7 @@
namespace hku {
class IMa : public IndicatorImp {
INDICATOR_IMP(IMa)
INDICATOR_IMP_SUPPORT_IND_PARAM(IMa)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:

View File

@ -96,15 +96,19 @@ TEST_CASE("test_HHV_dyn") {
CHECK_EQ(expect[i], result[i]);
}
// Stock stock = StockManager::instance().getStock("sh000001");
Stock stock = StockManager::instance().getStock("sh000001");
KData kdata = stock.getKData(KQuery(-100));
// KData kdata = stock.getKData(KQuery(0, Null<size_t>(), KQuery::MIN));
// Indicator c = CLOSE(kdata);
// expect = HHV(c, 50);
// result = HHV(c, CVAL(c, 50));
// CHECK_EQ(expect.size(), result.size());
// for (size_t i = 0; i < expect.size(); i++) {
// CHECK_EQ(expect[i], result[i]);
// }
Indicator c = CLOSE(kdata);
expect = HHV(c, 50);
result = HHV(c, CVAL(c, 50));
CHECK_EQ(expect.size(), result.size());
for (size_t i = 0; i < expect.size(); i++) {
if (i >= result.discard()) {
CHECK(!isnan(result[i]));
}
CHECK_EQ(expect[i], result[i]);
}
}
//-----------------------------------------------------------------------------

View File

@ -8,6 +8,7 @@
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/MA.h>
#include <hikyuu/indicator/crt/CVAL.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
@ -131,6 +132,23 @@ TEST_CASE("test_MA") {
}
}
/** @par 检测点 */
TEST_CASE("test_MA_dyn") {
Stock stock = StockManager::instance().getStock("sh000001");
KData kdata = stock.getKData(KQuery(-30));
// KData kdata = stock.getKData(KQuery(0, Null<size_t>(), KQuery::MIN));
Indicator c = CLOSE(kdata);
Indicator expect = MA(c, 10);
Indicator result = MA(c, CVAL(c, 10));
CHECK_EQ(expect.size(), result.size());
for (size_t i = 0; i < expect.size(); i++) {
if (i >= result.discard()) {
CHECK(!isnan(result[i]));
}
CHECK_EQ(expect[i], doctest::Approx(result[i]));
}
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------

View File

@ -49,7 +49,9 @@ Indicator (*DIFF_1)() = DIFF;
Indicator (*DIFF_2)(const Indicator&) = DIFF;
Indicator (*MA_1)(int) = MA;
Indicator (*MA_2)(const Indicator&, int) = MA;
Indicator (*MA_2)(const IndParam&) = MA;
Indicator (*MA_3)(const Indicator&, int) = MA;
Indicator (*MA_4)(const Indicator&, const Indicator&) = MA;
Indicator (*SMA_1)(int, double) = SMA;
Indicator (*SMA_2)(const Indicator&, int, double) = SMA;
@ -392,12 +394,14 @@ void export_Indicator_build_in() {
:rtype: Indicator)");
def("MA", MA_1, (arg("n") = 22));
def("MA", MA_2, (arg("data"), arg("n") = 22), R"(MA([data, n=22])
def("MA", MA_2, (arg("n")));
def("MA", MA_3, (arg("data"), arg("n")));
def("MA", MA_4, (arg("data"), arg("n") = 22), R"(MA([data, n=22])
:param Indicator data:
:param int n:
:param int|Indicator n:
:rtype: Indicator)");
def("AMA", AMA_1, (arg("n") = 10, arg("fast_n") = 2, arg("slow_n") = 30));

View File

@ -267,13 +267,13 @@ def test(all, compile, verbose, mode, case, j):
if all:
os.system("xmake -j {} -b {} unit-test".format(
j, "-v -D" if verbose else ""))
os.system("xmake r unit-test {}".format('' if case ==
'' else '-tc {}'.format(case)))
os.system("xmake r unit-test {}".format(
'' if case == '' else '--test-case={}'.format(case)))
else:
os.system("xmake -j {} -b {} small-test".format(
j, "-v -D" if verbose else ""))
os.system("xmake r small-test {}".format(
'' if case == '' else '-tc {}'.format(case)))
'' if case == '' else '--test-case={}'.format(case)))
@click.command()