add some testcases

This commit is contained in:
fasiondog 2024-02-28 00:54:55 +08:00
parent 4d12141836
commit 97f86f0b51
4 changed files with 151 additions and 4 deletions

View File

@ -39,12 +39,12 @@ void IAd::_calculate(const Indicator& data) {
_readyBuffer(total, 1);
price_t ad = 0.0;
value_t ad = 0.0;
auto* dst = this->data();
dst[m_discard] = 0.0;
for (size_t i = m_discard + 1; i < total; i++) {
const KRecord& r = k[i];
price_t tmp = r.highPrice - r.lowPrice;
value_t tmp = r.highPrice - r.lowPrice;
if (tmp != 0.0) {
// 多空对比 = [(收盘价- 最低价) - (最高价 - 收盘价)] / (最高价 - 最低价)
ad += ((r.closePrice + r.closePrice - r.highPrice - r.lowPrice) / tmp) * r.transAmount;

View File

@ -7,7 +7,7 @@
* Author: fasiondog
*/
#include "doctest/doctest.h"
#include "../test_config.h"
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/ACOS.h>
@ -46,6 +46,27 @@ TEST_CASE("test_ACOS") {
CHECK_EQ(result[0], doctest::Approx(std::acos(-0.1)));
}
//-----------------------------------------------------------------------------
// benchmark
//-----------------------------------------------------------------------------
#if ENABLE_BENCHMARK_TEST
TEST_CASE("test_ACOS_benchmark") {
Stock stock = getStock("sh000001");
KData kdata = stock.getKData(KQuery(0));
Indicator c = kdata.close();
int cycle = 1000; // 测试循环次数
{
BENCHMARK_TIME_MSG(test_ACOS_benchmark, cycle, fmt::format("data len: {}", c.size()));
SPEND_TIME_CONTROL(false);
for (int i = 0; i < cycle; i++) {
Indicator ind = ACOS();
Indicator result = ind(c);
}
}
}
#endif
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------

View File

@ -5,7 +5,7 @@
* Author: fasiondog
*/
#include "doctest/doctest.h"
#include "../test_config.h"
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/AD.h>
@ -36,6 +36,26 @@ TEST_CASE("test_AD") {
CHECK_EQ(ad[5], doctest::Approx(30.77).epsilon(0.1));
}
//-----------------------------------------------------------------------------
// benchmark
//-----------------------------------------------------------------------------
#if ENABLE_BENCHMARK_TEST
TEST_CASE("test_AD_benchmark") {
Stock stock = getStock("sh000001");
KData kdata = stock.getKData(KQuery(0));
int cycle = 1000; // 测试循环次数
{
BENCHMARK_TIME_MSG(test_AD_benchmark, cycle, fmt::format("data len: {}", kdata.size()));
SPEND_TIME_CONTROL(false);
for (int i = 0; i < cycle; i++) {
Indicator ind = AD();
Indicator result = ind(kdata);
}
}
}
#endif
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------

View File

@ -0,0 +1,106 @@
/*
* test_ACOS.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-5-1
* Author: fasiondog
*/
#include "../test_config.h"
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/ATR.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
using namespace hku;
/**
* @defgroup test_indicator_ATR test_indicator_ATR
* @ingroup test_hikyuu_indicator_suite
* @{
*/
/** @par 检测点 */
TEST_CASE("test_ATR") {
Indicator result;
PriceList a;
for (int i = 0; i < 10; ++i) {
a.push_back(i / 2.);
}
vector<Indicator::value_t> expect = {0., 0.333333, 0.77778, 1.25926, 1.75309,
2.25103, 2.75034, 3.25011, 3.75004, 4.25001};
Indicator data = PRICELIST(a);
result = ATR(data, 2);
CHECK_EQ(result.name(), "ATR");
CHECK_EQ(result.discard(), 0);
CHECK_EQ(result.size(), data.size());
for (int i = 0, len = data.size(); i < len; ++i) {
CHECK_EQ(result[i], doctest::Approx(expect[i]));
}
}
//-----------------------------------------------------------------------------
// benchmark
//-----------------------------------------------------------------------------
#if ENABLE_BENCHMARK_TEST
TEST_CASE("test_ATR_benchmark") {
Stock stock = getStock("sh000001");
KData kdata = stock.getKData(KQuery(0));
Indicator c = kdata.close();
int cycle = 1000; // 测试循环次数
{
BENCHMARK_TIME_MSG(test_ATR_benchmark, cycle, fmt::format("data len: {}", c.size()));
SPEND_TIME_CONTROL(false);
for (int i = 0; i < cycle; i++) {
Indicator ind = ATR();
Indicator result = ind(c);
}
}
}
#endif
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
TEST_CASE("test_ATR_export") {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "/ATR.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));
Indicator x1 = ATR(CLOSE(kdata));
{
std::ofstream ofs(filename);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(x1);
}
Indicator x2;
{
std::ifstream ifs(filename);
boost::archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(x2);
}
CHECK_EQ(x2.name(), "ATR");
CHECK_EQ(x1.size(), x2.size());
CHECK_EQ(x1.discard(), x2.discard());
CHECK_EQ(x1.getResultNumber(), x2.getResultNumber());
for (size_t i = x1.discard(); i < x1.size(); ++i) {
CHECK_EQ(x1[i], doctest::Approx(x2[i]));
}
}
#endif /* #if HKU_SUPPORT_SERIALIZATION */
/** @} */