CTP下载地址:http://www.sfit.com.cn/5_1_DocumentDown.htm
源码下载:https://github.com/wuhaizhi/CTPProject
测试账号注册地址http://www.simnow.com.cn/
我的库文件放在了根目录下面的 TradeApi\win32下面
添中库文件完成后 .pro 中会自动生成如下代码
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/TradeApi/win32/ -lthostmduserapi_se
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/TradeApi/win32/ -lthostmduserapi_sed
else:unix: LIBS += -L$$PWD/TradeApi/win32/ -lthostmduserapi_se
INCLUDEPATH += $$PWD/TradeApi/win32
DEPENDPATH += $$PWD/TradeApi/win32
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/TradeApi/win32/ -lthosttraderapi_se
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/TradeApi/win32/ -lthosttraderapi_sed
else:unix: LIBS += -L$$PWD/TradeApi/win32/ -lthosttraderapi_se
INCLUDEPATH += $$PWD/TradeApi/win32
DEPENDPATH += $$PWD/TradeApi/win32
主窗体需要实现CTP的API接口类
class MainWindow : public QMainWindow, public CThostFtdcTraderSpi, public CThostFtdcMdSpi
{
Q_OBJECT
private:
CThostFtdcTraderApi* m_ptraderapi;
TThostFtdcBrokerIDType m_BrokerID;
TThostFtdcUserIDType m_UserID;
TThostFtdcInvestorIDType m_InvestorID;
TThostFtdcPasswordType m_Password;
TThostFtdcAuthCodeType m_AuthCode;
TThostFtdcAppIDType m_AppID;
void InitCTP();
int UserLogin();
int Authenticate();
// 重写CThostFtdcTraderSpi方法
void OnFrontConnected();
void OnFrontDisconnected(int nReason);
void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnRspUserLogin(CThostFtdcRspUserLoginField * pRspUserLogin, CThostFtdcRspInfoField * pRspInfo, int nRequestID, bool bIsLast);
void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField * pSettlementInfoConfirm, CThostFtdcRspInfoField * pRspInfo, int nRequestID, bool bIsLast);
void OnRspQryInstrument(CThostFtdcInstrumentField * pInstrument, CThostFtdcRspInfoField * pRspInfo, int nRequestID, bool bIsLast);
void OnRspOrderInsert(CThostFtdcInputOrderField * pInputOrder, CThostFtdcRspInfoField * pRspInfo, int nRequestID, bool bIsLast);
void OnRtnOrder(CThostFtdcOrderField * pOrder);
void OnRtnTrade(CThostFtdcTradeField * pTrade);
void OnRspError(CThostFtdcRspInfoField * pRspInfo, int nRequestID, bool bIsLast);
void OnRspQrySettlementInfo(CThostFtdcSettlementInfoField* pSettlementInfo, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast);
void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnErrRtnOrderAction(CThostFtdcOrderActionField *pOrderAction, CThostFtdcRspInfoField *pRspInfo);
void OnRspOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnErrRtnOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo);
void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
int ReqQryTradingAccount();
int qryInstrument(QString sInstrumentID);
int qryInvestorPositon();
int QryOrder();
int CancelOrder(CThostFtdcOrderField AOrder);
void ReqSettlementInfoConfirm();
void AppendLog(QString msg);
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_login_clicked();
void on_btn_qry_Order_clicked();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_AppendMsg(QString msg);
signals:
void send_AppendMsg(QString msg);
private:
Ui::MainWindow *ui;
};
开发中中遇到的问题:
1、CTP回调函数中直接操作UI控件会出现错误或控件刷新不及时如下图。
可以通过信号与槽的方式处理(请看代码中的实现)
例如:向textedit中添加,日志信息
private slots:
void on_AppendMsg(QString msg);
signals:
void send_AppendMsg(QString msg);
connect(this, SIGNAL(send_AppendMsg(QString)), this, SLOT(on_AppendMsg(QString)));
void MainWindow::OnFrontConnected(){
qDebug("OnFrontConnected");
emit send_AppendMsg("OnFrontConnected");
}
void MainWindow::on_AppendMsg(QString msg){
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");
ui->edt_log->append(current_date + "-> " + msg);
}
运行界面如图:
简单的实现了几个接口,如果需要可以自己去实现。