#include "cci.hh"
#include <cassert>

namespace AI {

CCI::CCI(int period) :
    typical_(0.0), sma_(period), dev_(period)
{
    assert(period >= 2);
}

void CCI::operator()(double value)
{
    typical_ = value;
    sma_(value);
    dev_(value);
}

double CCI::operator()() const
{
    double dev = dev_();

    if (dev == 0.0)
        return 0;

    return (typical_ - sma_()) / (0.015 * dev_());
}

void CCI::clear()
{
    sma_.clear();
    dev_.clear();
}

}
