量化交易之One Piece篇 - TickData.h & TickData.cc

TickData是一个C++类,用于存储和处理期货市场的Tick数据,包括最新价、成交量、持仓量等信息,并提供了访问不同价位档位的问价和投标价格及大小的方法。类初始化时从CThostFtdcDepthMarketDataField结构体中获取数据,并设置时间戳。
摘要由CSDN通过智能技术生成
// TickData.h

#pragma once

#include <onepiece/api/ctp/ThostFtdcUserApiStruct.h>

#include <string>
#include <memory>
#include <iomanip>
#include <chrono>

using namespace std;

#define MAX_GEAR_SIZE 10

class TickData
{
public:
    TickData() = delete;
    TickData(const CThostFtdcDepthMarketDataField *pDepthMarketData);
    virtual ~TickData();

    inline double LastPrice() { return this->m_lastPrice; }
    inline double PreClosePrice() { return this->m_preClosePrice; }
    inline double PreOpenInterest() { return this->m_preOpenInterest; }
    inline double OpenPrice() { return this->m_openPrice; }
    inline double HighestPrice() { return this->m_highestPrice; }
    inline double LowestPrice() { return this->m_lowestPrice; }
    inline double Volume() { return this->m_volume; }
    inline double Turnover() { return this->m_turnover; }
    inline double OpenInterest() { return this->m_openInterest; }
    inline double UpperLimitPrice() { return this->m_upperLimitPrice; }
    inline double LowerLimitPrice() { return this->m_lowerLimitPrice; }
    inline double PriceTick() { return this->m_priceTick; }
    inline int VolumeMultiple() { return this->m_volumeMultiple; }

    inline void setExchangeInstrument(const shared_ptr<string> exchangeInstrument) { this->m_exchangeInstrument = exchangeInstrument; }
    inline void setPriceTick(double priceTick) { this->m_priceTick = priceTick; }
    inline void setVolumeMultiple(int volumeMultiple) { this->m_volumeMultiple = volumeMultiple; }

    inline time_t Timestamp() { return this->m_timestamp; }

    inline const shared_ptr<string> InstrumentID() { return this->m_instrumentID; }
    inline const shared_ptr<string> ExchangeInstrument() { return this->m_exchangeInstrument; }

    inline double AskPrice(unsigned int gear = 0) { 
        if (gear < MAX_GEAR_SIZE) {
            return this->m_askPrice[gear]; 
        }
        return 0; 
    }
    inline double BidPrice(unsigned int gear = 0) { 
        if (gear < MAX_GEAR_SIZE) {
            return this->m_bidPrice[gear]; 
        }
        return 0; 
    }
    inline int AskSize(unsigned int gear = 0) {
        if (gear < MAX_GEAR_SIZE) {
            return this->m_askSize[gear]; 
        }
        return 0;
    }
    inline int BidSize(unsigned int gear = 0) {
        if (gear < MAX_GEAR_SIZE) {
            return this->m_bidSize[gear]; 
        }
        return 0;
    }

private:
    time_t GetFormatTimestamp(const string& actionDate, const string& time, const int updateMillisec);

    void InitOrderBook(const CThostFtdcDepthMarketDataField *pDepthMarketData);

private:
    double m_lastPrice; // 最新价
    double m_preClosePrice; // 昨收盘价
    double m_preOpenInterest; // 昨持仓量
    double m_openPrice; // 今开盘价
    double m_highestPrice; // 最高价
    double m_lowestPrice; // 最低价

    double m_volume; // 成交量
    double m_turnover; // 成交额
    double m_openInterest; // 持仓量
    double m_upperLimitPrice; // 涨停板价
    double m_lowerLimitPrice; // 跌停板价
    double m_priceTick; // 每跳价格;
    int m_volumeMultiple; // 合约乘数;

    time_t m_timestamp; // tick 时间戳;

    shared_ptr<string> m_instrumentID; // eg: rb2310
    shared_ptr<string> m_exchangeInstrument; // eg: SHFE.rb2310

    double m_askPrice[MAX_GEAR_SIZE] = {};
    double m_bidPrice[MAX_GEAR_SIZE] = {};

    int m_askSize[MAX_GEAR_SIZE] = {};
    int m_bidSize[MAX_GEAR_SIZE] = {};
};

using TickUPtr = unique_ptr<TickData>;
using TickSPtr = shared_ptr<TickData>;
// TickData.cc

#include <onepiece/models/TickData.h>

#include <iostream>
using namespace std;

TickData::TickData(const CThostFtdcDepthMarketDataField *pDepthMarketData)
    : m_lastPrice(pDepthMarketData->LastPrice),
    m_preClosePrice(pDepthMarketData->PreClosePrice),
    m_preOpenInterest(pDepthMarketData->PreOpenInterest),
    m_openPrice(pDepthMarketData->OpenPrice),
    m_highestPrice(pDepthMarketData->HighestPrice),
    m_lowestPrice(pDepthMarketData->LowestPrice),
    m_volume(pDepthMarketData->Volume),
    m_turnover(pDepthMarketData->Turnover),
    m_openInterest(pDepthMarketData->OpenInterest),
    m_upperLimitPrice(pDepthMarketData->UpperLimitPrice),
    m_lowerLimitPrice(pDepthMarketData->LowerLimitPrice),
    m_instrumentID(make_shared<string>(pDepthMarketData->InstrumentID))
 {
    this->InitOrderBook(pDepthMarketData);

    this->m_timestamp = this->GetFormatTimestamp(string(pDepthMarketData->ActionDay), string(pDepthMarketData->UpdateTime), pDepthMarketData->UpdateMillisec);
    
    {
        // check data.
        // cout << "------------------------------------------------------------------------" << endl;
        // cout << "TickData::TickData LastPrice: " << this->LastPrice() << endl;
        // cout << "TickData::TickData PreClosePrice: " << this->PreClosePrice() << endl;
        // cout << "TickData::TickData PreOpenInterest: " << this->PreOpenInterest() << endl;
        // cout << "TickData::TickData OpenPrice: " << this->OpenPrice() << endl;
        // cout << "TickData::TickData HighestPrice: " << this->HighestPrice() << endl;
        // cout << "TickData::TickData LowestPrice: " << this->LowestPrice() << endl;
        // cout << "TickData::TickData Volume: " << this->Volume() << endl;
        // cout << "TickData::TickData Turnover: " << this->Turnover() << endl;
        // cout << "TickData::TickData OpenInterest: " << this->OpenInterest() << endl;
        // cout << "TickData::TickData UpperLimitPrice: " << this->UpperLimitPrice() << endl;
        // cout << "TickData::TickData LowerLimitPrice: " << this->LowerLimitPrice() << endl;
        // cout << "TickData::TickData Timestamp: " << this->Timestamp() << endl;
        // cout << "TickData::TickData InstrumentID: " << this->InstrumentID()->data() << endl;
        // cout << "TickData::TickData AskPrice1: " << this->AskPrice() << endl;
        // cout << "TickData::TickData AskPrice2: " << this->AskPrice(1) << endl;
        // cout << "TickData::TickData AskPrice3: " << this->AskPrice(2) << endl;
        // cout << "TickData::TickData AskPrice4: " << this->AskPrice(3) << endl;
        // cout << "TickData::TickData AskPrice5: " << this->AskPrice(4) << endl;
        // cout << "TickData::TickData BidPrice1: " << this->AskPrice() << endl;
        // cout << "TickData::TickData BidPrice2: " << this->AskPrice(1) << endl;
        // cout << "TickData::TickData BidPrice3: " << this->AskPrice(2) << endl;
        // cout << "TickData::TickData BidPrice4: " << this->AskPrice(3) << endl;
        // cout << "TickData::TickData BidPrice5: " << this->AskPrice(4) << endl;
        // cout << "TickData::TickData AskSize1: " << this->AskSize() << endl;
        // cout << "TickData::TickData AskSize2: " << this->AskSize(1) << endl;
        // cout << "TickData::TickData AskSize3: " << this->AskSize(2) << endl;
        // cout << "TickData::TickData AskSize4: " << this->AskSize(3) << endl;
        // cout << "TickData::TickData AskSize5: " << this->AskSize(4) << endl;
        // cout << "TickData::TickData BidSize1: " << this->BidSize() << endl;
        // cout << "TickData::TickData BidSize2: " << this->BidSize(1) << endl;
        // cout << "TickData::TickData BidSize3: " << this->BidSize(2) << endl;
        // cout << "TickData::TickData BidSize4: " << this->BidSize(3) << endl;
        // cout << "TickData::TickData BidSize5: " << this->BidSize(4) << endl;
        // cout << "------------------------------------------------------------------------" << endl;
    }
}

TickData::~TickData() {
    if (this->m_instrumentID)
        this->m_instrumentID.reset();
    
    if (this->m_exchangeInstrument)
        this->m_exchangeInstrument.reset();
}


void TickData::InitOrderBook(const CThostFtdcDepthMarketDataField *pDepthMarketData) {
    this->m_askPrice[0] = pDepthMarketData->AskPrice1;
    this->m_bidPrice[0] = pDepthMarketData->BidPrice1;
    this->m_askSize[0] = pDepthMarketData->AskVolume1;
    this->m_bidSize[0] = pDepthMarketData->BidVolume1;

    this->m_askPrice[1] = pDepthMarketData->AskPrice2;
    this->m_bidPrice[1] = pDepthMarketData->BidPrice2;
    this->m_askSize[1] = pDepthMarketData->AskVolume2;
    this->m_bidSize[1] = pDepthMarketData->BidVolume2;

    this->m_askPrice[2] = pDepthMarketData->AskPrice3;
    this->m_bidPrice[2] = pDepthMarketData->BidPrice3;
    this->m_askSize[2] = pDepthMarketData->AskVolume3;
    this->m_bidSize[2] = pDepthMarketData->BidVolume3;

    this->m_askPrice[3] = pDepthMarketData->AskPrice4;
    this->m_bidPrice[3] = pDepthMarketData->BidPrice4;
    this->m_askSize[3] = pDepthMarketData->AskVolume4;
    this->m_bidSize[3] = pDepthMarketData->BidVolume4;

    this->m_askPrice[4] = pDepthMarketData->AskPrice5;
    this->m_bidPrice[4] = pDepthMarketData->BidPrice5;
    this->m_askSize[4] = pDepthMarketData->AskVolume5;
    this->m_bidSize[4] = pDepthMarketData->BidVolume5;
}


time_t TickData::GetFormatTimestamp(const string& actionDate, const string& time, const int updateMillisec) {
    tm dateTime = {};
    istringstream iss(actionDate + " " + time);
    iss >> get_time(&dateTime, "%Y%m%d %H:%M:%S");

    return (mktime(&dateTime) + 8 * 60 * 60) * 1000 + updateMillisec;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值