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

namespace AI {

SMA::SMA(int period) :
    SimpleIndicator(period), sum_(0.0)
{
    assert(period >= 1);
}

void SMA::operator()(double value)
{
    sum_ -= buffer_.back();
    buffer_.pop_back();

    sum_ += value;
    buffer_.push_front(value);
}

double SMA::operator()() const
{
    return sum_ / period_;
}

void SMA::clear()
{
    clearBuffer();
    sum_ = 0.0;
}

}
