量化交易之One Piece篇 - MarketCTP.cc

cpp 复制代码
#include <boost/filesystem.hpp>
#include <sys/stat.h>

#include <onepiece/marketctp/MarketCTP.h>
#include <onepiece/models/TickData.h>
#include <onepiece/models/StatusInfoFieldData.h>
#include <thread>

MarketCTP::MarketCTP(AccountSPtr accountPtr): m_api(nullptr), m_nRequestID(0), m_accountPtr(accountPtr), m_coreListener(nullptr) {
    string fileName = string("./log/") + string("marketctp") + string(".log");
    this->m_marketLogger = std::make_shared<spdlog::logger>("MarketCTPLogger", std::make_shared<spdlog::sinks::basic_file_sink_mt>(fileName.c_str()));

    this->m_marketLogger->set_level(spdlog::level::info);
    this->m_marketLogger->flush_on(spdlog::level::info);
}

MarketCTP::~MarketCTP() {
    this->Disconnect();

    if (this->m_accountPtr)
        this->m_accountPtr.reset();

    this->m_coreListener = nullptr;

    if (this->m_marketLogger)
        this->m_marketLogger.reset();
}


bool MarketCTP::Connect() {
    this->m_marketLogger->info("MarketCTP::Connect.");

    string marketRuntimeFold = this->m_accountPtr->MarketRuntimeFold()->empty() ? "./runtime_md_ctp/" : this->m_accountPtr->MarketRuntimeFold()->data();
    if (!boost::filesystem::exists(marketRuntimeFold) && mkdir(marketRuntimeFold.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
        this->m_marketLogger->error("[MarketCTP::Connect]: runtime_md is not exist, and make runtime_md dir failed.");
        return false;
    }
    
    this->m_api = CThostFtdcMdApi::CreateFtdcMdApi(marketRuntimeFold.c_str(), false, false);
    this->m_api->RegisterSpi(this);

    // string md_url = "tcp://218.17.194.3:41205"; // 招商期货.
    // string md_url = "tcp://180.168.146.187:10131"; // simnow 全天站点
    this->m_api->RegisterFront((char *)this->m_accountPtr->MarketFrontAddress()->data());
    this->m_api->Init();

    return true;
}

bool MarketCTP::Disconnect() {
    if (!this->m_api) {
        this->m_marketLogger->error("[MarketCTP::Disconnect]: this->m_api is nullptr.");
        return false;
    }

    this->m_api->RegisterSpi(NULL);
    this->m_api->Release();
    this->m_api = nullptr;

    return true;
}

void MarketCTP::Subscribe(const shared_ptr<vector<string>> instruments) {
    if (!instruments) {
        this->m_marketLogger->error("[MarketCTP::Subscribe]: instruments is nullptr.");
        return;
    }

    this->m_marketLogger->info("[MarketCTP::Subscribe]: instruments.size: {0}.", instruments->size());
    if (!this->m_api) {
        this->m_marketLogger->error("[MarketCTP::Subscribe]: this->m_api is nullptr.");
        return;
    }

    // 订阅合约
    // vector<char*> instrumentsFormat = {"rb2310"}; // for test.
    vector<char*> instrumentsFormat;
    for (size_t i = 0; i < instruments->size(); i++) {
        instrumentsFormat.push_back(const_cast<char*>(instruments->at(i).c_str()));
    }
    this->m_api->SubscribeMarketData(instrumentsFormat.data(), instrumentsFormat.size());

    instrumentsFormat.clear(), instrumentsFormat.shrink_to_fit();
}


void MarketCTP::Unsubscribe(const shared_ptr<vector<string>> instruments) {
    // 退订合约
}


bool MarketCTP::RegisterListener(ICoreListener* coreListener) {
    if (!coreListener) {
        this->m_marketLogger->error("[MarketCTP::RegisterListener]: coreListener is nullptr.");
        return false;
    } 
    
    this->m_coreListener = coreListener;

    return true;
}


void MarketCTP::OnFrontConnected() {
    this->m_marketLogger->info("MarketCTP::OnFrontConnected.");
    if (!this->m_api) {
        this->m_marketLogger->error("[MarketCTP::OnFrontConnected]: this->m_api is nullptr.");
        return;
    }

    // login
    CThostFtdcReqUserLoginField req = {};
    this->m_api->ReqUserLogin(&req, MakeRequestID());
}


void MarketCTP::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
    this->m_marketLogger->info("MarketCTP::OnRspUserLogin.");

    if (bIsLast && this->m_coreListener)
        this->m_coreListener->OnMarketReady(make_shared<StatusInfoFieldData>(0, ""));
}

void MarketCTP::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {
    if (pDepthMarketData && this->m_coreListener) {
        TickSPtr tickSPtr = make_shared<TickData>(pDepthMarketData);

        ContractSPtr contractSPtr = this->m_coreListener->GetSysContractSPtr(tickSPtr->InstrumentID()->data());
        if (!contractSPtr) {
            this->m_marketLogger->error("[MarketCTP::OnRtnDepthMarketData]: contract({0}) not in sys.", tickSPtr->InstrumentID()->c_str());
            return;
        }

        tickSPtr->setExchangeInstrument(contractSPtr->ExchangeInstrument());
        tickSPtr->setPriceTick(contractSPtr->PriceTick());
        tickSPtr->setVolumeMultiple(contractSPtr->VolumeMultiple());
        
        if (this->m_coreListener)
            this->m_coreListener->HandleTick(tickSPtr);
    }
}

void MarketCTP::OnFrontDisconnected(int nReason) {
    this->m_marketLogger->error("MarketCTP::OnFrontDisconnected(int nReason): {0}.", this->GetDisconnectedReason(nReason));
}

void MarketCTP::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
    this->m_marketLogger->error("MarketCTP::OnRspError.");
}

void MarketCTP::OnHeartBeatWarning(int nTimeLapse) {
    this->m_marketLogger->info("MarketCTP::OnHeartBeatWarning nTimeLapse: {0}.", nTimeLapse);
}
相关推荐
IT 小阿姨(数据库)16 分钟前
PgSQL监控死元组和自动清理状态的SQL语句执行报错ERROR: division by zero原因分析和解决方法
linux·运维·数据库·sql·postgresql·centos
THMAIL18 分钟前
量化股票从贫穷到财务自由之路 - 零基础搭建Python量化环境:Anaconda、Jupyter实战指南
linux·人工智能·python·深度学习·机器学习·金融
让子弹飞0236 分钟前
36.2Linux单总线驱动DS18B20实验(详细讲解代码)_csdn
linux·ubuntu·驱动的分离和分层
小欣加油1 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
Yana.nice1 小时前
yum list 和 repoquery的区别
linux
王璐WL1 小时前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae1 小时前
C++ -- 模板
开发语言·c++·笔记·其他
码出钞能力2 小时前
更换libc.so导致linux变砖,通过LD_PRELOAD挽救
linux·服务器
小马学嵌入式~2 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
小猪咪piggy2 小时前
【JavaEE】(24) Linux 基础使用和程序部署
linux·运维·服务器