add STDP indicator

This commit is contained in:
fasiondog 2019-04-18 23:14:58 +08:00
parent a7fd6596da
commit 301e4f575c
10 changed files with 268 additions and 1 deletions

View File

@ -444,6 +444,15 @@
:rtype: Indicator
.. py:function:: STDP([data, n=10])
总体标准差STDP(X,N)为X的N日总体标准差
:param data: 输入数据
:param int n: 时间窗口
:rtype: Indicator
.. py:function:: SUM([data, n=20])
求总和。SUM(X,N),统计N周期中X的总和,N=0则从第一个有效值开始。

View File

@ -45,6 +45,7 @@
* :py:func:`SQRT` - 开平方
* :py:func:`STD` - 估算标准差,同 STDEV
* :py:func:`STDEV` - 计算N周期内样本标准差
* :py:func:`STDP` - 总体标准差
* :py:func:`WEAVE` - 将两个ind的结果合并到一个ind中

View File

@ -459,6 +459,17 @@ STDEV([data, n=10])
"""
STDP.__doc__ = """
STDP([data, n=10])
STDP(X,N)X的N日总体标准差
:param data:
:param int n:
:rtype: Indicator
"""
SUM.__doc__ = """
SUM([data, n=20])

View File

@ -46,6 +46,7 @@
#include "crt/SMA.h"
#include "crt/SQRT.h"
#include "crt/STDEV.h"
#include "crt/STDP.h"
#include "crt/SUM.h"
#include "crt/VIGOR.h"

View File

@ -0,0 +1,30 @@
/*
* STD.h
*
* Created on: 2013-4-18
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_STDP_H_
#define INDICATOR_CRT_STDP_H_
#include "../Indicator.h"
namespace hku {
/**
* N周期内总体标准差
* @param n N日时间窗口
* @ingroup Indicator
*/
Indicator HKU_API STDP(int n = 10);
Indicator STDP(const Indicator& data, int n = 10);
inline Indicator STDP(const Indicator& data, int n) {
return STDP(n)(data);
}
} /* namespace */
#endif /* INDICATOR_CRT_STDP_H_ */

View File

@ -0,0 +1,67 @@
/*
* IStdp.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-4-18
* Author: fasiondog
*/
#include "IStdp.h"
#include "../crt/MA.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IStdp)
#endif
namespace hku {
IStdp::IStdp(): IndicatorImp("STDP", 1) {
setParam<int>("n", 10);
}
IStdp::~IStdp() {
}
bool IStdp::check() {
int n = getParam<int>("n");
if (n < 2) {
HKU_ERROR("Invalid param[n] ! (n >= 2) " << m_params
<< " [StdDeviation::calculate]");
return false;
}
return true;
}
void IStdp::_calculate(const Indicator& data) {
size_t total = data.size();
int n = getParam<int>("n");
m_discard = data.discard() + n - 1;
if (m_discard >= total) {
m_discard = total;
return;
}
Indicator ma = MA(data, n);
for (size_t i = discard(); i < total; ++i) {
price_t mean = ma[i];
price_t sum = 0.0;
for (size_t j = i + 1 - n; j <= i; ++j) {
sum += std::pow(data[j] - mean, 2);
}
_set(std::sqrt(sum/n), i);
}
}
Indicator HKU_API STDP(int n) {
IndicatorImpPtr p = make_shared<IStdp>();
p->setParam<int>("n", n);
return Indicator(p);
}
} /* namespace hku */

View File

@ -0,0 +1,31 @@
/*
* IStdp.h
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-4-18
* Author: fasiondog
*/
#ifndef INDICATOR_IMP_ISTDP_H_
#define INDICATOR_IMP_ISTDP_H_
#include "../Indicator.h"
namespace hku {
/*
* N周期内总体标准差
* n: N日时间窗口
*/
class IStdp: public hku::IndicatorImp {
INDICATOR_IMP(IStdp)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:
IStdp();
virtual ~IStdp();
};
} /* namespace hku */
#endif /* INDICATOR_IMP_ISTDP_H_ */

View File

@ -0,0 +1,111 @@
/*
* test_STDP.cpp
*
* Created on: 2019-4-18
* 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/KDATA.h>
#include <hikyuu/indicator/crt/STDEV.h>
#include <hikyuu/indicator/crt/STDP.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
using namespace hku;
/**
* @defgroup test_indicator_STDP test_indicator_STDP
* @ingroup test_hikyuu_indicator_suite
* @{
*/
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_STDP ) {
/** @arg n > 1 的正常情况 */
PriceList d;
for (size_t i = 0; i < 15; ++i) {
d.push_back(i+1);
}
d[5] = 4.0;
d[7] = 4.0;
d[11] = 6.0;
Indicator ind = PRICELIST(d);
Indicator dev = STDP(ind, 10);
BOOST_CHECK(dev.name() == "STDP");
BOOST_CHECK(dev.size() == 15);
BOOST_CHECK(dev[8] == Null<price_t>());
BOOST_CHECK(std::fabs(dev[9] - 2.77308) < 0.00001 );
BOOST_CHECK(std::fabs(dev[10] - 2.98161) < 0.00001 );
BOOST_CHECK(std::fabs(dev[11] - 2.68514) < 0.00001 );
BOOST_CHECK(std::fabs(dev[12] - 3.1) < 0.00001 );
BOOST_CHECK(std::fabs(dev[13] - 3.46554) < 0.00001 );
BOOST_CHECK(std::fabs(dev[14] - 3.79605) < 0.00001 );
/** @arg n = 1时 */
dev = STDP(ind, 1);
BOOST_CHECK(dev.name() == "STDP");
BOOST_CHECK(dev.size() == 15);
for (size_t i = 0; i < dev.size(); ++i) {
BOOST_CHECK(dev[i] == Null<price_t>());
}
/** @arg operator() */
Indicator expect = STDP(ind, 10);
dev = STDP(10);
Indicator result = dev(ind);
BOOST_CHECK(result.size() == expect.size());
for (size_t i = 0; i < expect.size(); ++i) {
BOOST_CHECK(result[i] == expect[i]);
}
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_STDP_export ) {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "/STDP.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));
Indicator ma1 = STDP(CLOSE(kdata), 10);
{
std::ofstream ofs(filename);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(ma1);
}
Indicator ma2;
{
std::ifstream ifs(filename);
boost::archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(ma2);
}
BOOST_CHECK(ma2.name() == "STDP");
BOOST_CHECK(ma1.size() == ma2.size());
BOOST_CHECK(ma1.discard() == ma2.discard());
BOOST_CHECK(ma1.getResultNumber() == ma2.getResultNumber());
for (size_t i = 0; i < ma1.size(); ++i) {
BOOST_CHECK_CLOSE(ma1[i], ma2[i], 0.00001);
}
}
#endif /* #if HKU_SUPPORT_SERIALIZATION */
/** @} */

View File

@ -76,6 +76,9 @@ Indicator (*SAFTYLOSS_2)(const Indicator&, int n1, int n2, double p) = SAFTYLOSS
Indicator (*STDEV_1)(int) = STDEV;
Indicator (*STDEV_2)(const Indicator&, int) = STDEV;
Indicator (*STDP_1)(int) = STDP;
Indicator (*STDP_2)(const Indicator&, int) = STDP;
Indicator (*HHV_1)(int) = HHV;
Indicator (*HHV_2)(const Indicator&, int) = HHV;
@ -240,6 +243,9 @@ void export_Indicator_build_in() {
def("STDEV", STDEV_1, (arg("n")=10));
def("STDEV", STDEV_2, (arg("data"), arg("n")=10));
def("STDP", STDP_1, (arg("n")=10));
def("STDP", STDP_2, (arg("data"), arg("n")=10));
def("POS", POS, (arg("block"), arg("query"), arg("sg")));
def("HHV", HHV_1, (arg("n")=20));

View File

@ -17,7 +17,7 @@ function main(target)
os.exec("xcopy /S /Q /Y /I hikyuu %s", os.args(installdir))
end
if is_plat("linux") then
os.exec("cp -f -r -T hikyuu %s" ,os.args(installdir))
os.exec("cp -f -r -T hikyuu %s", os.args(installdir))
end
if is_plat("macosx") then
os.exec("cp -f -r hikyuu/* %s", os.args(installdir))