/* * ISma.cpp * * Copyright (c) 2019 hikyuu.org * * Created on: 2019-5-15 * Author: fasiondog */ #include "ISma.h" #if HKU_SUPPORT_SERIALIZATION BOOST_CLASS_EXPORT(hku::ISma) #endif namespace hku { ISma::ISma(): IndicatorImp("SMA", 1) { setParam("n", 22); setParam("m", 2.0); } ISma::~ISma() { } bool ISma::check() { int n = getParam("n"); if (n < 1) { HKU_ERROR("Invalid param! (n >= 1) [ISma::check]"); return false; } return true; } void ISma::_calculate(const Indicator& ind) { size_t total = ind.size(); m_discard = ind.discard(); if (m_discard >= total) { m_discard = total; return; } double n = getParam("n"); double m = getParam("m"); double p = n - m; _set(ind[m_discard], m_discard); for (size_t i = m_discard + 1; i < total; i++) { _set((m * ind[i] + p * get(i-1)) / n, i); } } Indicator HKU_API SMA(int n, double m) { IndicatorImpPtr p = make_shared(); p->setParam("n", n); p->setParam("m", m); return Indicator(p); } } /* namespace hku */