不爱多说,具体看下面代码:
class TRMANAGERSHARED_EXPORT CTrManager
{
public:
CTrManager();
// 交易账户登录 单独登录账号 适配穿透式监管API
void TrUserLogin(QString BrokerID, QString UserID, QString Password, QString Front, QString AppID, QString AuthCode);
// 交易账户登出 单独登出账号
void TrUserLogout();
// 添加接收器
void TrAddReceiver(CTrReceiver* receiver);
// 删除接收器
void TrDelReceiver(CTrReceiver* receiver);
// 查询持仓
void TrRspPosition();
// 查询资金
void TrRspTradingAccount();
// 查询交易合约
void TrRspInsrument();
// 开多仓
void TrBuy(TrRtnOrderField order);
// 开空仓
void TrSell(TrRtnOrderField order);
// 平多仓
void TrShort(TrRtnOrderField order);
// 平空仓
void TrCover(TrRtnOrderField order);
// 撤单
void TrRevoke(CThostFtdcOrderField order);
// 获取合约信息
CThostFtdcInstrumentField TrInstrument(QString Instrument);
// 账号状态
bool TrbReady();
// 释放所有
void TrRelease();
// 版本信息
static QString TrVersion();
// 设置行情前置机地址
void TrSetFront(QString Front);
// 新增合约订阅
void TrAddSubscribe(QString Instrument);
// 开始订阅行情
void TrStart();
// 停止订阅行情
void TrStop();
// 行情连接状态
bool TrMarketStatus();
// 交易连接状态
bool TrTradeStatus();
};
上面是封装的TrManager.h
使用时引入上面的头文件,然后创建一个类继承于TrGlobal.h里的CTrReceiver类,实现OnReceiveData方法就可以直接使用了,下面是一个简单的例子:
头文件
#ifndef CRECEVIER_H
#define CRECEVIER_H
#include "include/TrGlobal.h"
class CRecevier : public CTrReceiver
{
public:
CRecevier();
virtual void OnReceiveData(Tr_ResponseID rId, void* wParam, void* lParam) override;
};
#endif // CRECEVIER_H
cpp
#include "CRecevier.h"
CRecevier::CRecevier()
{
}
void CRecevier::OnReceiveData(Tr_ResponseID rId, void* wParam, void* lParam)
{
Q_UNUSED(lParam)
switch (rId)
{
case Tr_ResponseID::TrOnRtnDepthMarketData:
{
CThostFtdcDepthMarketDataField *pDepthMarketData = reinterpret_cast<CThostFtdcDepthMarketDataField *>(wParam);
CThostFtdcDepthMarketDataField DepthMarketData;
DepthMarketData = static_cast<CThostFtdcDepthMarketDataField>(*pDepthMarketData);
qDebug() << DepthMarketData.InstrumentID << DepthMarketData.LastPrice << DepthMarketData.UpdateTime << DepthMarketData.UpdateMillisec;
break;
}
case Tr_ResponseID::TrOnFrontConnected:
{
qDebug() << "Tr_ResponseID::TrOnFrontConnected";
break;
}
case Tr_ResponseID::TrMarketUserLogin:
{
qDebug() << "Tr_ResponseID::TrMarketUserLogin";
break;
}
case Tr_ResponseID::TrOnRspUserLogin:
{
CThostFtdcRspUserLoginField *pRspUserLogin = reinterpret_cast<CThostFtdcRspUserLoginField *>(wParam);
qDebug() << "Tr_ResponseID::TrOnRspUserLogin" << pRspUserLogin->UserID << pRspUserLogin->SessionID;
break;
}
case Tr_ResponseID::TrOnRspQryTradingAccount:
{
CThostFtdcTradingAccountField *pTradingAccount = reinterpret_cast<CThostFtdcTradingAccountField *>(wParam);
qDebug() << "OnRspQryTradingAccount" << pTradingAccount->Available;
break;
}
case Tr_ResponseID::TrOnInvestorPositions:
{
QVector<CThostFtdcInvestorPositionField>* pVecPosition = reinterpret_cast<QVector<CThostFtdcInvestorPositionField>*>(wParam);
if(pVecPosition->size() == 0) return;
qDebug() << pVecPosition->size();
break;
}
default:
break;
}
}
main.cpp
#include <QCoreApplication>
#include "include/TrManager.h"
#include "CRecevier.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CTrManager * g_pManager = new CTrManager;
g_pManager->TrSetFront("tcp://180.168.146.187:10131");
g_pManager->TrAddSubscribe("rb2105");
g_pManager->TrStart();
CRecevier *pRecevier = new CRecevier;
g_pManager->TrAddReceiver(pRecevier);
// 支持最新CTP穿透式监管
g_pManager->TrUserLogin("9999", "xxxxxx", "xxxxx", "tcp://180.168.146.187:10130", "AppID", "AuthCode");
return a.exec();
}
资源下载:
https://download.youkuaiyun.com/download/XLEdoo/13745730
原创首发感谢支持!
这篇博客介绍了如何使用QT库封装国内商品期货的CTP接口,通过创建一个类继承自CTrReceiver,并实现OnReceiveData方法,使得交易和行情接口的使用变得便捷。提供了头文件和示例代码,用户可以参考进行快速集成。
984

被折叠的 条评论
为什么被折叠?



