hikyuu2/hikyuu_cpp/hikyuu/indicator/imp/Atr.cpp

60 lines
1.1 KiB
C++
Raw Normal View History

2019-03-20 02:47:02 +08:00
/*
* Atr.cpp
*
* Created on: 201654
* Author: Administrator
*/
#include "Atr.h"
namespace hku {
Atr::Atr() : IndicatorImp("ATR", 1){
setParam<int>("n", 14);
}
Atr::~Atr() {
}
bool Atr::check() {
if (getParam<int>("n") < 1) {
HKU_ERROR("Invalid param! (n>=1) "
<< m_params << " [Atr::check]");
return false;
}
return true;
}
void Atr::_calculate(const Indicator& indicator) {
size_t total = indicator.size();
m_discard = indicator.discard();
2019-03-23 18:54:10 +08:00
if (m_discard >= total) {
m_discard = total;
2019-03-20 02:47:02 +08:00
return;
}
2019-03-23 18:54:10 +08:00
int n = getParam<int>("n");
2019-03-20 02:47:02 +08:00
size_t startPos = discard();
price_t ema = indicator[startPos];
_set(ema, startPos);
price_t multiplier = 2.0 / (n + 1);
for (size_t i = startPos + 1; i < total; ++i) {
ema = (indicator[i] - ema) * multiplier + ema;
_set(ema, i);
}
}
Indicator HKU_API ATR(int n) {
IndicatorImpPtr p = make_shared<Atr>();
p->setParam<int>("n", n);
return Indicator(p);
}
Indicator HKU_API ATR(const Indicator& data, int n) {
return ATR(n)(data);
}
} /* namespace hku */