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


    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. MarketCTP::MarketCTP(AccountSPtr accountPtr): m_api(nullptr), m_nRequestID(0), m_accountPtr(accountPtr), m_coreListener(nullptr) {
    8. string fileName = string("./log/") + string("marketctp") + string(".log");
    9. this->m_marketLogger = std::make_shared("MarketCTPLogger", std::make_shared(fileName.c_str()));
    10. this->m_marketLogger->set_level(spdlog::level::info);
    11. this->m_marketLogger->flush_on(spdlog::level::info);
    12. }
    13. MarketCTP::~MarketCTP() {
    14. this->Disconnect();
    15. if (this->m_accountPtr)
    16. this->m_accountPtr.reset();
    17. this->m_coreListener = nullptr;
    18. if (this->m_marketLogger)
    19. this->m_marketLogger.reset();
    20. }
    21. bool MarketCTP::Connect() {
    22. this->m_marketLogger->info("MarketCTP::Connect.");
    23. string marketRuntimeFold = this->m_accountPtr->MarketRuntimeFold()->empty() ? "./runtime_md_ctp/" : this->m_accountPtr->MarketRuntimeFold()->data();
    24. if (!boost::filesystem::exists(marketRuntimeFold) && mkdir(marketRuntimeFold.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
    25. this->m_marketLogger->error("[MarketCTP::Connect]: runtime_md is not exist, and make runtime_md dir failed.");
    26. return false;
    27. }
    28. this->m_api = CThostFtdcMdApi::CreateFtdcMdApi(marketRuntimeFold.c_str(), false, false);
    29. this->m_api->RegisterSpi(this);
    30. // string md_url = "tcp://218.17.194.3:41205"; // 招商期货.
    31. // string md_url = "tcp://180.168.146.187:10131"; // simnow 全天站点
    32. this->m_api->RegisterFront((char *)this->m_accountPtr->MarketFrontAddress()->data());
    33. this->m_api->Init();
    34. return true;
    35. }
    36. bool MarketCTP::Disconnect() {
    37. if (!this->m_api) {
    38. this->m_marketLogger->error("[MarketCTP::Disconnect]: this->m_api is nullptr.");
    39. return false;
    40. }
    41. this->m_api->RegisterSpi(NULL);
    42. this->m_api->Release();
    43. this->m_api = nullptr;
    44. return true;
    45. }
    46. void MarketCTP::Subscribe(const shared_ptr> instruments) {
    47. if (!instruments) {
    48. this->m_marketLogger->error("[MarketCTP::Subscribe]: instruments is nullptr.");
    49. return;
    50. }
    51. this->m_marketLogger->info("[MarketCTP::Subscribe]: instruments.size: {0}.", instruments->size());
    52. if (!this->m_api) {
    53. this->m_marketLogger->error("[MarketCTP::Subscribe]: this->m_api is nullptr.");
    54. return;
    55. }
    56. // 订阅合约
    57. // vector instrumentsFormat = {"rb2310"}; // for test.
    58. vector<char*> instrumentsFormat;
    59. for (size_t i = 0; i < instruments->size(); i++) {
    60. instrumentsFormat.push_back(const_cast<char*>(instruments->at(i).c_str()));
    61. }
    62. this->m_api->SubscribeMarketData(instrumentsFormat.data(), instrumentsFormat.size());
    63. instrumentsFormat.clear(), instrumentsFormat.shrink_to_fit();
    64. }
    65. void MarketCTP::Unsubscribe(const shared_ptr> instruments) {
    66. // 退订合约
    67. }
    68. bool MarketCTP::RegisterListener(ICoreListener* coreListener) {
    69. if (!coreListener) {
    70. this->m_marketLogger->error("[MarketCTP::RegisterListener]: coreListener is nullptr.");
    71. return false;
    72. }
    73. this->m_coreListener = coreListener;
    74. return true;
    75. }
    76. void MarketCTP::OnFrontConnected() {
    77. this->m_marketLogger->info("MarketCTP::OnFrontConnected.");
    78. if (!this->m_api) {
    79. this->m_marketLogger->error("[MarketCTP::OnFrontConnected]: this->m_api is nullptr.");
    80. return;
    81. }
    82. // login
    83. CThostFtdcReqUserLoginField req = {};
    84. this->m_api->ReqUserLogin(&req, MakeRequestID());
    85. }
    86. void MarketCTP::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
    87. this->m_marketLogger->info("MarketCTP::OnRspUserLogin.");
    88. if (bIsLast && this->m_coreListener)
    89. this->m_coreListener->OnMarketReady(make_shared(0, ""));
    90. }
    91. void MarketCTP::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {
    92. if (pDepthMarketData && this->m_coreListener) {
    93. TickSPtr tickSPtr = make_shared(pDepthMarketData);
    94. ContractSPtr contractSPtr = this->m_coreListener->GetSysContractSPtr(tickSPtr->InstrumentID()->data());
    95. if (!contractSPtr) {
    96. this->m_marketLogger->error("[MarketCTP::OnRtnDepthMarketData]: contract({0}) not in sys.", tickSPtr->InstrumentID()->c_str());
    97. return;
    98. }
    99. tickSPtr->setExchangeInstrument(contractSPtr->ExchangeInstrument());
    100. tickSPtr->setPriceTick(contractSPtr->PriceTick());
    101. tickSPtr->setVolumeMultiple(contractSPtr->VolumeMultiple());
    102. if (this->m_coreListener)
    103. this->m_coreListener->HandleTick(tickSPtr);
    104. }
    105. }
    106. void MarketCTP::OnFrontDisconnected(int nReason) {
    107. this->m_marketLogger->error("MarketCTP::OnFrontDisconnected(int nReason): {0}.", this->GetDisconnectedReason(nReason));
    108. }
    109. void MarketCTP::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
    110. this->m_marketLogger->error("MarketCTP::OnRspError.");
    111. }
    112. void MarketCTP::OnHeartBeatWarning(int nTimeLapse) {
    113. this->m_marketLogger->info("MarketCTP::OnHeartBeatWarning nTimeLapse: {0}.", nTimeLapse);
    114. }

  • 相关阅读:
    MySQL安装及应用合集(6):MySQL函数快速验证方法
    Android端ReactNative环境搭建——下
    NodeMcu arduino ESP8266 搭建mqtt服务(然也物联)以及使用
    《架构风清扬-Java面试系列第27讲》Java中如何正确优雅关闭线程?
    Intel Trust Domain Extensions(TDX)介绍
    【Kali】简单记录
    【小笔记】复杂模型小数据可能会造成过拟合还是欠拟合?
    内存函数的介绍和模拟实现
    在UE4(Unreal Engine4)中安装 Quixel Bridge Megascans bridget插件
    什么是Lua模块?你会如何使用NGINX的Lua模块来定制请求处理流程?
  • 原文地址:https://blog.csdn.net/Michael_234198652/article/details/132797260