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


// VWAP.h

#pragma once

#include <onepiece/models/TickData.h>
#include <queue>

class VWAP
{
public:
    VWAP() = delete;

    VWAP(unsigned int windows);

    ~VWAP();

    void UpdateTick(TickSPtr tickSPtr);

    double Value();

    bool Check();

private:
    unsigned int m_windows;

    double m_sumLastPriceValue;
    double m_sumSizeValue;

    queue<TickSPtr> m_ticks;

    bool m_isOk;
};
// VWAP.cc

#include <onepiece/signals/VWAP.h>


VWAP::VWAP(unsigned int windows): m_windows(windows), m_sumLastPriceValue(0), m_sumSizeValue(0), m_isOk(false) {

}

VWAP::~VWAP() {
    while (!this->m_ticks.empty()) {
        this->m_ticks.pop();
    }
}

void VWAP::UpdateTick(TickSPtr tickSPtr) {
    if (this->m_ticks.size() < this->m_windows) {
        this->m_ticks.push(tickSPtr);
        this->m_sumLastPriceValue += (tickSPtr->LastPrice() * tickSPtr->Volume());
        this->m_sumSizeValue += tickSPtr->Volume();

        this->m_isOk = false;
        return;
    }

    this->m_ticks.push(tickSPtr);
    this->m_sumLastPriceValue += (tickSPtr->LastPrice() * tickSPtr->Volume());
    this->m_sumSizeValue += tickSPtr->Volume();

    TickSPtr firstTickSPtr = this->m_ticks.front();

    this->m_sumLastPriceValue -= (firstTickSPtr->LastPrice() * firstTickSPtr->Volume());
    this->m_sumSizeValue -= firstTickSPtr->Volume();
    this->m_ticks.pop();

    this->m_isOk = true;
}


double VWAP::Value() {
    return (0 == this->m_sumSizeValue) ? 0 : (this->m_sumLastPriceValue / this->m_sumSizeValue);
}

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

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

暂无评论

推荐阅读
W17cxBGSpUYm