量化交易之One Piece篇 - signals - SMA.h & SMA.cc
  W17cxBGSpUYm 2023年11月02日 107 0


// 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;
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
W17cxBGSpUYm