add SQRT indicator

This commit is contained in:
fasiondog 2019-04-14 14:29:50 +08:00
parent 960041a809
commit df94d53d38
9 changed files with 252 additions and 0 deletions

View File

@ -344,6 +344,18 @@
:param int n: 时间窗口
:rtype: Indicator
.. py:function:: SQRT([data])
开平方
用法SQRT(X)为X的平方根
例如SQRT(CLOSE)收盘价的平方根
:param data: 输入数据
:rtype: Indicator
.. py:function:: STDEV([data, n=10])

View File

@ -36,6 +36,7 @@
* :py:func:`POW` - 乘幂
* :py:func:`REF` - 向前引用 (即右移),引用若干周期前的数据
* :py:func:`SGN` - 求符号值
* :py:func:`SQRT` - 开平方
* :py:func:`STDEV` - 计算N周期内样本标准差
* :py:func:`WEAVE` - 将两个ind的结果合并到一个ind中

View File

@ -399,6 +399,20 @@ SMA([data, n=22])
"""
SQRT.__doc__ = """
SQRT([data])
SQRT(X)X的平方根
SQRT(CLOSE)
:param data:
:rtype: Indicator
"""
STDEV.__doc__ = """
STDEV([data, n=10])

View File

@ -38,6 +38,7 @@
#include "crt/SAFTYLOSS.h"
#include "crt/SGN.h"
#include "crt/SMA.h"
#include "crt/SQRT.h"
#include "crt/STDEV.h"
#include "crt/SUM.h"
#include "crt/VIGOR.h"

View File

@ -0,0 +1,40 @@
/*
* SQRT.h
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-4-14
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_SQRT_H_
#define INDICATOR_CRT_SQRT_H_
#include "CVAL.h"
namespace hku {
/**
*
* @details
* <pre>
* SQRT(X)X的平方根
* SQRT(CLOSE)
* </pre>
* @ingroup Indicator
*/
Indicator HKU_API SQRT();
Indicator SQRT(price_t);
Indicator SQRT(const Indicator& ind);
inline Indicator SQRT(const Indicator& ind) {
return SQRT()(ind);
}
inline Indicator SQRT(price_t val) {
return SQRT(CVAL(val));
}
} /* namespace */
#endif /* INDICATOR_CRT_SQRT_H_ */

View File

@ -0,0 +1,49 @@
/*
* ISqrt.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-04-14
* Author: fasiondog
*/
#include "ISqrt.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::ISqrt)
#endif
namespace hku {
ISqrt::ISqrt() : IndicatorImp("SQRT", 1) {
}
ISqrt::~ISqrt() {
}
bool ISqrt::check() {
return true;
}
void ISqrt::_calculate(const Indicator& data) {
size_t total = data.size();
m_discard = data.discard();
if (m_discard >= total) {
m_discard = total;
return;
}
for (size_t i = m_discard; i < total; ++i) {
_set(std::sqrt(data[i]), i);
}
}
Indicator HKU_API SQRT() {
return Indicator(make_shared<ISqrt>());
}
} /* namespace hku */

View File

@ -0,0 +1,30 @@
/*
* ISqrt.h
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-4-14
* Author: fasiondog
*/
#ifndef INDICATOR_IMP_ISQRT_H_
#define INDICATOR_IMP_ISQRT_H_
#include "../Indicator.h"
namespace hku {
/**
*
*/
class ISqrt: public IndicatorImp {
INDICATOR_IMP(ISqrt)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:
ISqrt();
virtual ~ISqrt();
};
} /* namespace hku */
#endif /* INDICATOR_IMP_ISQRT_H_ */

View File

@ -0,0 +1,96 @@
/*
* test_SQRT.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-4-14
* Author: fasiondog
*/
#ifdef TEST_ALL_IN_ONE
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_MODULE test_hikyuu_indicator_suite
#include <boost/test/unit_test.hpp>
#endif
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/POW.h>
#include <hikyuu/indicator/crt/SQRT.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
using namespace hku;
/**
* @defgroup test_indicator_SQRT test_indicator_SQRT
* @ingroup test_hikyuu_indicator_suite
* @{
*/
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_SQRT ) {
Indicator result;
PriceList a;
for (int i = 0; i < 10; ++i) {
a.push_back(i);
}
Indicator data = PRICELIST(a);
result = SQRT(data);
BOOST_CHECK(result.name() == "SQRT");
BOOST_CHECK(result.discard() == 0);
for (int i = 0; i <10; ++i) {
BOOST_CHECK(result[i] == std::sqrt(data[i]));
}
result = SQRT(4);
BOOST_CHECK(result.name() == "SQRT");
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == std::sqrt(4));
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_SQRT_export ) {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "/SQRT.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));
Indicator x1 = SQRT(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);
}
BOOST_CHECK(x1.size() == x2.size());
BOOST_CHECK(x1.discard() == x2.discard());
BOOST_CHECK(x1.getResultNumber() == x2.getResultNumber());
for (size_t i = 0; i < x1.size(); ++i) {
BOOST_CHECK_CLOSE(x1[i], x2[i], 0.00001);
}
}
#endif /* #if HKU_SUPPORT_SERIALIZATION */
/** @} */

View File

@ -152,6 +152,11 @@ Indicator (*POW_1)(int) = POW;
Indicator (*POW_2)(const Indicator&, int) = POW;
Indicator (*POW_3)(price_t, int) = POW;
Indicator (*SQRT_1)() = SQRT;
Indicator (*SQRT_2)(const Indicator&) = SQRT;
Indicator (*SQRT_3)(price_t) = SQRT;
void export_Indicator_build_in() {
def("KDATA", KDATA1);
def("KDATA", KDATA3);
@ -286,4 +291,8 @@ void export_Indicator_build_in() {
def("POW", POW_1, (arg("n")));
def("POW", POW_2, (arg("data"), arg("n")));
def("POW", POW_3), (arg("data"), arg("n"));
def("SQRT", SQRT_1);
def("SQRT", SQRT_2);
def("SQRT", SQRT_3);
}