add EXP Indicator

This commit is contained in:
fasiondog 2019-04-03 02:18:42 +08:00
parent 84b664222e
commit ed352c38c6
10 changed files with 230 additions and 2 deletions

View File

@ -90,7 +90,15 @@
:param int n: 计算均值的周期窗口必须为大于0的整数
:rtype: Indicator
.. py:function:: EXP([data])
EXP(X)为e的X次幂
:param Indicator data: 输入数据
:rtype: Indicator
.. py:function:: HHV([data, n=20])
N日内最高价N=0则从第一个有效值开始。

View File

@ -24,6 +24,7 @@
* :py:func:`CVAL` - 创建指定长度的固定数值指标
* :py:func:`COUNT` - 统计满足条件的周期数
* :py:func:`DIFF` - 差分指标即data[i] - data[i-1]
* :py:func:`EXP` - e的X次幂
* :py:func:`HHV` - N日内最高价
* :py:func:`IF` - 根据条件求不同的值
* :py:func:`LLV` - N日内最低价

View File

@ -122,6 +122,16 @@ EMA([data, n=22])
"""
EXP.__doc__ = """
EXP([data])
EXP(X)e的X次幂
:param Indicator data:
:rtype: Indicator
"""
HHV.__doc__ = """
HHV([data, n=20])

View File

@ -17,6 +17,7 @@
#include "crt/CVAL.h"
#include "crt/DIFF.h"
#include "crt/EMA.h"
#include "crt/EXP.h"
#include "crt/HHV.h"
#include "crt/HSL.h"
#include "crt/LLV.h"

View File

@ -0,0 +1,34 @@
/*
* EXP.h
*
* Created on: 2019-4-3
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_EXP_H_
#define INDICATOR_CRT_EXP_H_
#include "../Indicator.h"
namespace hku {
/**
* , EXP(X)e的X次幂
* @ingroup Indicator
*/
Indicator HKU_API EXP();
/**
* , EXP(X)e的X次幂
* @param ind
* @ingroup Indicator
*/
Indicator EXP(const Indicator& ind);
inline Indicator EXP(const Indicator& ind) {
return EXP()(ind);
}
} /* namespace */
#endif /* INDICATOR_CRT_EXP_H_ */

View File

@ -0,0 +1,54 @@
/*
* IExp.cpp
*
* Created on: 201943
* Author: fasiondog
*/
#include "IExp.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IExp)
#endif
namespace hku {
IExp::IExp() : IndicatorImp("EXP", 1) {
}
IExp::~IExp() {
}
bool IExp::check() {
return true;
}
void IExp::_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) {
price_t x = std::exp(data[i]);
if (isinf(x)) {
_set(Null<price_t>(), i);
} else {
_set(std::exp(data[i]), i);
}
}
}
Indicator HKU_API EXP() {
return Indicator(make_shared<IExp>());
}
} /* namespace hku */

View File

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

View File

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

@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE( test_SUM ) {
BOOST_AUTO_TEST_CASE( test_SUM_export ) {
StockManager& sm = StockManager::instance();
string filename(sm.tmpdir());
filename += "SUM.xml";
filename += "/SUM.xml";
Stock stock = sm.getStock("sh000001");
KData kdata = stock.getKData(KQuery(-20));

View File

@ -114,6 +114,9 @@ Indicator (*NOT_2)(const Indicator&) = NOT;
Indicator (*SGN_1)() = SGN;
Indicator (*SGN_2)(const Indicator&) = SGN;
Indicator (*EXP_1)() = EXP;
Indicator (*EXP_2)(const Indicator&) = EXP;
void export_Indicator_build_in() {
def("KDATA", KDATA1);
@ -211,6 +214,9 @@ void export_Indicator_build_in() {
def("SGN", SGN_1);
def("SGN", SGN_2);
def("EXP", EXP_1);
def("EXP", EXP_2);
}