// SMA.h
#pragma once
#include <onepiece/models/TickData.h>
#include <queue>
class SMA
{
public:
SMA() = delete;
SMA(unsigned int windows);
~SMA();
void UpdateTick(TickSPtr tickSPtr);
double Value();
bool Check();
private:
unsigned int m_windows;
double m_sumValue;
queue<TickSPtr> m_ticks;
bool m_isOk;
};
// SMA.cc
#include <onepiece/signals/SMA.h>
SMA::SMA(unsigned int windows): m_windows(windows), m_sumValue(0), m_isOk(false) {
}
SMA::~SMA() {
while (!this->m_ticks.empty()) {
this->m_ticks.pop();
}
}
void SMA::UpdateTick(TickSPtr tickSPtr) {
if (this->m_ticks.size() < this->m_windows) {
this->m_ticks.push(tickSPtr);
this->m_sumValue += tickSPtr->LastPrice();
this->m_isOk = false;
return;
}
this->m_ticks.push(tickSPtr);
this->m_sumValue += tickSPtr->LastPrice();
TickSPtr firstTickSPtr = this->m_ticks.front();
this->m_sumValue -= firstTickSPtr->LastPrice();
this->m_ticks.pop();
this->m_isOk = true;
}
double SMA::Value() {
return (0 == m_windows) ? 0 : (this->m_sumValue / m_windows);
}
bool SMA::Check() {
return this->m_isOk;
}