add LN, LOG ind

This commit is contained in:
fasiondog 2019-04-11 02:10:41 +08:00
parent 3094960f7c
commit ce0e05bd98
20 changed files with 529 additions and 28 deletions

View File

@ -185,6 +185,22 @@
:param int n: N日时间窗口
:rtype: Indicator
.. py:function:: LN([data])
求自然对数, LN(X)以e为底的对数
:param data: 输入数据
:rtype: Indicator
.. py:function:: LOG([data])
以10为底的对数
:param data: 输入数据
:rtype: Indicator
.. py:function:: LOW([data])

View File

@ -222,6 +222,25 @@ LLV([data, n=20])
"""
LN.__doc__ = """
LN([data])
, LN(X)e为底的对数
:param data:
:rtype: Indicator
"""
LOG.__doc__ = """
LOG([data])
10
:param data:
:rtype: Indicator
"""
LOW.__doc__ = """
LOW([data])

View File

@ -21,8 +21,10 @@
#include "crt/EXP.h"
#include "crt/HHV.h"
#include "crt/HSL.h"
#include "crt/LLV.h"
#include "crt/LIUTONGPAN.h"
#include "crt/LLV.h"
#include "crt/LN.h"
#include "crt/LOG.h"
#include "crt/MA.h"
#include "crt/MACD.h"
#include "crt/MAX.h"

View File

@ -8,7 +8,7 @@
#ifndef INDICATOR_CRT_ABS_H_
#define INDICATOR_CRT_ABS_H_
#include "../Indicator.h"
#include "CVAL.h"
namespace hku {
@ -17,18 +17,17 @@ namespace hku {
* @ingroup Indicator
*/
Indicator HKU_API ABS();
/**
*
* @param ind
* @ingroup Indicator
*/
Indicator ABS(price_t);
Indicator ABS(const Indicator& ind);
inline Indicator ABS(const Indicator& ind) {
return ABS()(ind);
}
inline Indicator ABS(price_t val) {
return ABS(CVAL(val));
}
} /* namespace */
#endif /* INDICATOR_CRT_ABS_H_ */

View File

@ -8,7 +8,7 @@
#ifndef INDICATOR_CRT_EXP_H_
#define INDICATOR_CRT_EXP_H_
#include "../Indicator.h"
#include "CVAL.h"
namespace hku {
@ -17,18 +17,17 @@ namespace hku {
* @ingroup Indicator
*/
Indicator HKU_API EXP();
/**
* , EXP(X)e的X次幂
* @param ind
* @ingroup Indicator
*/
Indicator EXP(price_t);
Indicator EXP(const Indicator& ind);
inline Indicator EXP(const Indicator& ind) {
return EXP()(ind);
}
inline Indicator EXP(price_t val) {
return EXP(CVAL(val));
}
} /* namespace */
#endif /* INDICATOR_CRT_EXP_H_ */

View File

@ -0,0 +1,36 @@
/*
* LN.h
*
* Created on: 2019-4-11
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_LN_H_
#define INDICATOR_CRT_LN_H_
#include "CVAL.h"
namespace hku {
/**
*
* LN(X)e为底的对数
* LN(CLOSE)
* @ingroup Indicator
*/
Indicator HKU_API LN();
Indicator LN(price_t);
Indicator LN(const Indicator& ind);
inline Indicator LN(const Indicator& ind) {
return LN()(ind);
}
inline Indicator LN(price_t val) {
return LN(CVAL(val));
}
} /* namespace */
#endif /* INDICATOR_CRT_LN_H_ */

View File

@ -0,0 +1,34 @@
/*
* LOG.h
*
* Created on: 2019-4-11
* Author: fasiondog
*/
#ifndef INDICATOR_CRT_LOG_H_
#define INDICATOR_CRT_LOG_H_
#include "CVAL.h"
namespace hku {
/**
* 10
* LOG(X)X的对数
* @ingroup Indicator
*/
Indicator HKU_API LOG();
Indicator LOG(price_t);
Indicator LOG(const Indicator& ind);
inline Indicator LOG(const Indicator& ind) {
return LOG()(ind);
}
Indicator LOG(price_t val) {
return LOG(CVAL(val));
}
} /* namespace */
#endif /* INDICATOR_CRT_LOG_H_ */

View File

@ -8,7 +8,7 @@
#ifndef INDICATOR_CRT_SGN_H_
#define INDICATOR_CRT_SGN_H_
#include "../Indicator.h"
#include "CVAL.h"
namespace hku {
@ -17,18 +17,17 @@ namespace hku {
* @ingroup Indicator
*/
Indicator HKU_API SGN();
/**
* , SGN(X) X>0, X=0, X<0 1, 0, -1
* @param ind
* @ingroup Indicator
*/
Indicator SGN(price_t);
Indicator SGN(const Indicator& ind);
inline Indicator SGN(const Indicator& ind) {
return SGN()(ind);
}
inline Indicator SGN(price_t val) {
return SGN(CVAL(val));
}
}
#endif /* INDICATOR_CRT_SGN_H_ */

View File

@ -34,15 +34,15 @@ void IExp::_calculate(const Indicator& data) {
return;
}
price_t null_price = Null<price_t>();
for (size_t i = m_discard; i < total; ++i) {
price_t x = std::exp(data[i]);
if (std::isinf(x)) {
_set(Null<price_t>(), i);
_set(null_price, i);
} else {
_set(std::exp(data[i]), i);
}
}
}

View File

@ -0,0 +1,53 @@
/*
* ILn.cpp
*
* Created on: 2019411
* Author: fasiondog
*/
#include "ILn.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::ILn)
#endif
namespace hku {
ILn::ILn() : IndicatorImp("LN", 1) {
}
ILn::~ILn() {
}
bool ILn::check() {
return true;
}
void ILn::_calculate(const Indicator& data) {
size_t total = data.size();
m_discard = data.discard();
if (m_discard >= total) {
m_discard = total;
return;
}
price_t null_price = Null<price_t>();
for (size_t i = m_discard; i < total; ++i) {
if (data[i] < 0.0) {
_set(null_price, i);
} else {
_set(std::log(data[i]), i);
}
}
}
Indicator HKU_API LN() {
return Indicator(make_shared<ILn>());
}
} /* namespace hku */

View File

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

View File

@ -0,0 +1,53 @@
/*
* ILog.cpp
*
* Created on: 2019411
* Author: fasiondog
*/
#include "ILog.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::ILog)
#endif
namespace hku {
ILog::ILog() : IndicatorImp("LOG", 1) {
}
ILog::~ILog() {
}
bool ILog::check() {
return true;
}
void ILog::_calculate(const Indicator& data) {
size_t total = data.size();
m_discard = data.discard();
if (m_discard >= total) {
m_discard = total;
return;
}
price_t null_price = Null<price_t>();
for (size_t i = m_discard; i < total; ++i) {
if (data[i] < 0.0) {
_set(null_price, i);
} else {
_set(std::log10(data[i]), i);
}
}
}
Indicator HKU_API LOG() {
return Indicator(make_shared<ILog>());
}
} /* namespace hku */

View File

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

View File

@ -43,6 +43,11 @@ BOOST_AUTO_TEST_CASE( test_ABS ) {
for (int i = 0; i <10; ++i) {
BOOST_CHECK(result[i] == -data[i]);
}
result = ABS(-11);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == 11);
}

View File

@ -96,7 +96,6 @@ BOOST_AUTO_TEST_CASE( test_BETWEEN ) {
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == 0);
}

View File

@ -43,6 +43,11 @@ BOOST_AUTO_TEST_CASE( test_EXP ) {
for (int i = 0; i <10; ++i) {
BOOST_CHECK(result[i] == std::exp(data[i]));
}
result = EXP(1);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == std::exp(1));
}

View File

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

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

@ -44,6 +44,21 @@ BOOST_AUTO_TEST_CASE( test_SGN ) {
BOOST_CHECK(result[0] == -1);
BOOST_CHECK(result[1] == 0);
BOOST_CHECK(result[2] == 1);
result = SGN(-1);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == -1);
result = SGN(0);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == 0);
result = SGN(1);
BOOST_CHECK(result.size() == 1);
BOOST_CHECK(result.discard() == 0);
BOOST_CHECK(result[0] == 1);
}

View File

@ -106,16 +106,19 @@ Indicator (*SUM_1)(int) = SUM;
Indicator (*SUM_2)(const Indicator&, int) = SUM;
Indicator (*ABS_1)() = ABS;
Indicator (*ABS_2)(const Indicator&) = ABS;
Indicator (*ABS_2)(price_t) = ABS;
Indicator (*ABS_3)(const Indicator&) = ABS;
Indicator (*NOT_1)() = NOT;
Indicator (*NOT_2)(const Indicator&) = NOT;
Indicator (*SGN_1)() = SGN;
Indicator (*SGN_2)(const Indicator&) = SGN;
Indicator (*SGN_2)(price_t) = SGN;
Indicator (*SGN_3)(const Indicator&) = SGN;
Indicator (*EXP_1)() = EXP;
Indicator (*EXP_2)(const Indicator&) = EXP;
Indicator (*EXP_2)(price_t) = EXP;
Indicator (*EXP_3)(const Indicator&) = EXP;
Indicator (*MAX_1)(const Indicator&, const Indicator&) = MAX;
Indicator (*MAX_2)(const Indicator&, price_t) = MAX;
@ -134,6 +137,15 @@ Indicator (*BETWEEN_6)(price_t, const Indicator&, price_t) = BETWEEN;
Indicator (*BETWEEN_7)(price_t, price_t, const Indicator&) = BETWEEN;
Indicator (*BETWEEN_8)(price_t, price_t, price_t) = BETWEEN;
Indicator (*LN_1)() = LN;
Indicator (*LN_2)(price_t) = LN;
Indicator (*LN_3)(const Indicator&) = LN;
Indicator (*LOG_1)() = LOG;
Indicator (*LOG_2)(price_t) = LOG;
Indicator (*LOG_3)(const Indicator&) = LOG;
void export_Indicator_build_in() {
def("KDATA", KDATA1);
def("KDATA", KDATA3);
@ -224,15 +236,18 @@ void export_Indicator_build_in() {
def("ABS", ABS_1);
def("ABS", ABS_2);
def("ABS", ABS_3);
def("NOT", NOT_1);
def("NOT", NOT_2);
def("SGN", SGN_1);
def("SGN", SGN_2);
def("SGN", SGN_3);
def("EXP", EXP_1);
def("EXP", EXP_2);
def("EXP", EXP_3);
def("MAX", MAX_1);
def("MAX", MAX_2);
@ -250,6 +265,14 @@ void export_Indicator_build_in() {
def("BETWEEN", BETWEEN_6);
def("BETWEEN", BETWEEN_7);
def("BETWEEN", BETWEEN_8);
def("LN", LN_1);
def("LN", LN_2);
def("LN", LN_3);
def("LOG", LOG_1);
def("LOG", LOG_2);
def("LOG", LOG_3);
}