add ABS Indicator

This commit is contained in:
fasiondog 2019-04-02 01:49:40 +08:00
parent 12d3443d5c
commit b19dc758be
11 changed files with 223 additions and 3 deletions

View File

@ -4,6 +4,15 @@
内建技术指标
============
.. py:function:: ABS([data])
求绝对值
:param Indicator data: 输入数据
:rtype: Indicator
.. py:function:: AMA([data, n=10, fast_n=2, slow_n=30])
佩里.J 考夫曼Perry J.Kaufman自适应移动平均 [BOOK1]_

View File

@ -27,6 +27,16 @@
from .indicator import *
ABS.__doc__ = """
ABS([data])
:param Indicator data:
:rtype: Indicator
"""
AMA.__doc__ = """
AMA([data, n=10, fast_n=2, slow_n=30])

View File

@ -10,6 +10,7 @@
#include "Indicator.h"
#include "crt/KDATA.h"
#include "crt/ABS.h"
#include "crt/AMA.h"
#include "crt/ATR.h"
#include "crt/COUNT.h"

View File

@ -0,0 +1,34 @@
/*
* ABS.h
*
* Created on: 2019-4-2
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_ABS_H_
#define INDICATOR_CRT_ABS_H_
#include "../Indicator.h"
namespace hku {
/**
*
* @ingroup Indicator
*/
Indicator HKU_API ABS();
/**
*
* @param ind
* @ingroup Indicator
*/
Indicator ABS(const Indicator& ind);
inline Indicator ABS(const Indicator& ind) {
return ABS()(ind);
}
} /* namespace */
#endif /* INDICATOR_CRT_ABS_H_ */

View File

@ -0,0 +1,49 @@
/*
* IAbs.cpp
*
* Created on: 201942
* Author: fasiondog
*/
#include "IAbs.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IAbs)
#endif
namespace hku {
IAbs::IAbs() : IndicatorImp("ABS", 1) {
}
IAbs::~IAbs() {
}
bool IAbs::check() {
return true;
}
void IAbs::_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::abs(data[i]), i);
}
}
Indicator HKU_API ABS() {
return Indicator(make_shared<IAbs>());
}
} /* namespace hku */

View File

@ -0,0 +1,25 @@
/*
* IAbs.h
*
* Created on: 2019-4-2
* Author: fasiondog
*/
#ifndef INDICATOR_IMP_IABS_H_
#define INDICATOR_IMP_IABS_H_
#include "../Indicator.h"
namespace hku {
class IAbs: public IndicatorImp {
INDICATOR_IMP(IAbs)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:
IAbs();
virtual ~IAbs();
};
} /* namespace hku */
#endif /* INDICATOR_IMP_IABS_H_ */

View File

@ -14,7 +14,7 @@ BOOST_CLASS_EXPORT(hku::ISum)
namespace hku {
ISum::ISum() : IndicatorImp("ISum", 1) {
ISum::ISum() : IndicatorImp("SUM", 1) {
setParam<int>("n", 20);
}

View File

@ -0,0 +1,88 @@
/*
* test_ABS.cpp
*
* Created on: 201942
* 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/HHV.h>
#include <hikyuu/indicator/crt/ABS.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
using namespace hku;
/**
* @defgroup test_indicator_ABS test_indicator_ABS
* @ingroup test_hikyuu_indicator_suite
* @{
*/
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_ABS ) {
Indicator result;
PriceList a;
for (int i = 0; i < 10; ++i) {
a.push_back(-i);
}
Indicator data = PRICELIST(a);
result = ABS(data);
BOOST_CHECK(result.name() == "ABS");
BOOST_CHECK(result.discard() == 0);
for (int i = 0; i <10; ++i) {
BOOST_CHECK(result[i] == -data[i]);
}
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_ABS_export ) {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "/ABS.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));
Indicator x1 = ABS(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

@ -14,7 +14,6 @@
#include <fstream>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/crt/LLV.h>
#include <hikyuu/indicator/crt/SUM.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>

View File

@ -20,7 +20,7 @@
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <hikyuu/StockManager.h>
#include <hikyuu/indicator/build_in.h>
#include <hikyuu/indicator/crt/KDATA.h>
using namespace hku;

View File

@ -105,6 +105,9 @@ Indicator (*COUNT_2)(const Indicator&, int) = COUNT;
Indicator (*SUM_1)(int) = SUM;
Indicator (*SUM_2)(const Indicator&, int) = SUM;
Indicator (*ABS_1)() = ABS;
Indicator (*ABS_2)(const Indicator&) = ABS;
void export_Indicator_build_in() {
def("KDATA", KDATA1);
def("KDATA", KDATA3);
@ -193,6 +196,8 @@ void export_Indicator_build_in() {
def("SUM", SUM_1, (arg("n")=20));
def("SUM", SUM_2, (arg("data"), arg("n")=20));
def("ABS", ABS_1);
def("ABS", ABS_2);
}