add TAN indicator

This commit is contained in:
fasiondog 2019-05-01 19:04:18 +08:00
parent def1cc6599
commit 0d85726ca8
9 changed files with 237 additions and 2 deletions

View File

@ -538,6 +538,14 @@
:rtype: Indicator
.. py:function:: TAN([data])
正切值
:param Indicator data: 输入数据
:rtype: Indicator
.. py:function:: VIGOR([kdata, n=2])
亚历山大.艾尔德力度指数 [BOOK2]_

View File

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

View File

@ -571,6 +571,16 @@ SUM([data, n=20])
"""
TAN.__doc__ = """
TAN([data])
:param Indicator data:
:rtype: Indicator
"""
VIGOR.__doc__ = """
VIGOR([kdata, n=2])

View File

@ -56,6 +56,7 @@
#include "crt/STDEV.h"
#include "crt/STDP.h"
#include "crt/SUM.h"
#include "crt/TAN.h"
#include "crt/VIGOR.h"
#endif /* INDICATOR_BUILD_IN_H_ */

View File

@ -0,0 +1,35 @@
/*
* TAN.h
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-5-1
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_TAN_H_
#define INDICATOR_CRT_TAN_H_
#include "CVAL.h"
namespace hku {
/**
*
* @ingroup Indicator
*/
Indicator HKU_API TAN();
Indicator TAN(price_t);
Indicator TAN(const Indicator& ind);
inline Indicator TAN(const Indicator& ind) {
return TAN()(ind);
}
inline Indicator TAN(price_t val) {
return TAN(CVAL(val));
}
}
#endif /* INDICATOR_CRT_TAN_H_ */

View File

@ -0,0 +1,50 @@
/*
* ITan.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-5-1
* Author: fasiondog
*/
#include "ITan.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::ITan)
#endif
namespace hku {
ITan::ITan() : IndicatorImp("TAN", 1) {
}
ITan::~ITan() {
}
bool ITan::check() {
return true;
}
void ITan::_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::tan(data[i]), i);
}
}
Indicator HKU_API TAN() {
return Indicator(make_shared<ITan>());
}
} /* namespace hku */

View File

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

View File

@ -0,0 +1,95 @@
/*
* test_TAN.cpp
*
* Copyright (c) 2019 hikyuu.org
*
* Created on: 2019-5-1
* 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/TAN.h>
#include <hikyuu/indicator/crt/KDATA.h>
#include <hikyuu/indicator/crt/PRICELIST.h>
using namespace hku;
/**
* @defgroup test_indicator_TAN test_indicator_TAN
* @ingroup test_hikyuu_indicator_suite
* @{
*/
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_TAN ) {
Indicator result;
PriceList a;
for (int i = 0; i < 10; ++i) {
a.push_back(-i);
}
Indicator data = PRICELIST(a);
result = TAN(data);
BOOST_CHECK(result.name() == "TAN");
BOOST_CHECK(result.discard() == 0);
for (int i = 0; i <10; ++i) {
BOOST_CHECK(result[i] == std::tan(data[i]));
}
result = TAN(-11);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == std::tan(-11));
}
//-----------------------------------------------------------------------------
// test export
//-----------------------------------------------------------------------------
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
BOOST_AUTO_TEST_CASE( test_TAN_export ) {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "/TAN.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));
Indicator x1 = TAN(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(x2.name() == "TAN");
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

@ -211,6 +211,10 @@ Indicator (*ACOS_1)() = ACOS;
Indicator (*ACOS_2)(const Indicator&) = ACOS;
Indicator (*ACOS_3)(price_t) = ACOS;
Indicator (*TAN_1)() = TAN;
Indicator (*TAN_2)(const Indicator&) = TAN;
Indicator (*TAN_3)(price_t) = TAN;
void export_Indicator_build_in() {
def("IKDATA", KDATA1);
@ -400,9 +404,13 @@ void export_Indicator_build_in() {
def("COS", COS_1);
def("COS", COS_2);
def("COS", COS_2);
def("COS", COS_3);
def("ACOS", ACOS_1);
def("ACOS", ACOS_2);
def("ACOS", ACOS_2);
def("ACOS", ACOS_3);
def("TAN", TAN_1);
def("TAN", TAN_2);
def("TAN", TAN_3);
}