开发示例
信息采集API使用demo
#include <stdio.h>
#include "exportSecApi.h"
int main()
{
char *systemInfo = new char[1024];
char *errmsg = new char [256];
memset(systemInfo, 0x00, sizeof(systemInfo));
memset(errmsg, 0x00, sizeof(errmsg));
int nRet = QDGetLocalSystemInfo(systemInfo, errmsg, true);
if (nRet > 0 && NULL != systemInfo)
{
printf("sysinfo:%s, nRet:%d\n", systemInfo, nRet);
if (NULL != errmsg)
{
printf("errmsg:%s\n", errmsg);
}
}
else
{
printf("QDGetLocalSystemInfo return:%d\n", nRet);
}
if (NULL != systemInfo)
{
delete systemInfo;
systemInfo = NULL;
}
if (NULL != errmsg)
{
delete errmsg;
errmsg = NULL;
}
return 0;
}
普通Tcp API使用demo
#include <stdio.h>
#include "QdpFtdcTraderApi.h"
// 经纪公司代码
TQdpFtdcBrokerIDType g_chBrokerID;
// 交易用户代码
TQdpFtdcUserIDType g_chUserID;
//用户本地最大报单号
int g_UserOrderLocalID = 1;
class CSimpleHandler : public CQdpFtdcTraderSpi
{
public:
// 构造函数,需要一个有效的指向CQdpFtdcMduserApi实例的指针
CSimpleHandler(CQdpFtdcTraderApi *pUserApi) :
m_pUserApi(pUserApi) {}
~CSimpleHandler() {}
// 发出报单录入请求
int SendOrder()
{
CQdpFtdcInputOrderField ord;
memset(&ord, 0, sizeof(ord));
//经纪公司代码
strcpy(ord.BrokerID, g_chBrokerID);
//交易所
strcpy(ord.ExchangeID, "GFEX");
//投资者代码
strcpy(ord.InvestorID, "guofugqs0001");
// 用户代码
strcpy(ord.UserID, g_chUserID);
// 合约代码
strcpy(ord.InstrumentID, "lc2307");
//本地报单号
ord.UserOrderLocalID = g_UserOrderLocalID;
//sprintf(ord.UserOrderLocalID,"%012d", 91000001);
//报单类型
ord.OrderPriceType = QDP_FTDC_OPT_LimitPrice;
// 买卖方向
//strcpy(&(ord.Direction) , "1");
ord.Direction = QDP_FTDC_D_Buy;
// 开平标志
strcpy(&(ord.OffsetFlag), "0");
// 投机套保标志
ord.HedgeFlag = QDP_FTDC_CHF_Speculation;
// 价格
ord.LimitPrice = 228550;
// 数量
ord.Volume = 1;
// 有效期类型
//strcpy(&(ord.TimeCondition),"3" );
ord.TimeCondition = QDP_FTDC_TC_GFD;
//成交量类型
ord.VolumeCondition = QDP_FTDC_VC_AV;
//触发条件
//ord = QDP_FTDC_CC_Immediately
//强平原因
ord.ForceCloseReason = QDP_FTDC_FCR_NotForceClose;
// 自动挂起标志
ord.IsAutoSuspend = 0;
int ret = m_pUserApi->ReqOrderInsert(&ord, 0);
return ret;
}
// 发出撤单请求
void SendOrderAction()
{
CQdpFtdcOrderActionField ord;
memset(&ord, 0, sizeof(CQdpFtdcOrderActionField));
//经纪公司代码
strcpy(ord.BrokerID, "guofu");
//交易所
strcpy(ord.ExchangeID, "GFEX");
//投资者代码
strcpy(ord.InvestorID, "guofugqs0001");
// 用户代码
strcpy(ord.UserID, "gqs001");
strncpy(ord.OrderSysID, "60000221", sizeof(ord.OrderSysID));
// 合约代码
//strcpy(ord.InstrumentID, "lc2307");
//本地报单号
//ord.UserOrderActionLocalID = ++g_UserOrderLocalID;
//ord.UserOrderLocalID = 200;
ord.ActionFlag = QDP_FTDC_AF_Delete;
m_pUserApi->ReqOrderAction(&ord, 0);
}
// 合约查询请求
void QryInstrument()
{
CQdpFtdcQryInstrumentField tReq;
memset(&tReq, 0, sizeof(CQdpFtdcQryInstrumentField));
strncpy(tReq.ExchangeID, "CZCE", sizeof(tReq.ExchangeID));
//strncpy(tReq.InstrumentID, "IPS CF001&SR001", sizeof(tReq.InstrumentID));
strncpy(tReq.InstrumentID, "SPD CF011&CF103", sizeof(tReq.InstrumentID));
m_pUserApi->ReqQryInstrument(&tReq, 0);
}
// 持仓查询请求
void QryPosition()
{
CQdpFtdcQryInvestorPositionField tReq;
memset(&tReq, 0, sizeof(CQdpFtdcQryInvestorPositionField));
strncpy(tReq.BrokerID, g_chBrokerID, sizeof(tReq.BrokerID));
strncpy(tReq.UserID, g_chUserID, sizeof(tReq.UserID));
m_pUserApi->ReqQryInvestorPosition(&tReq, 0);
}
// 登录请求
void Login()
{
strcpy(g_chBrokerID, "guofu");
strcpy(g_chUserID, "m001");
CQdpFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.BrokerID, g_chBrokerID);
strcpy(reqUserLogin.UserID, g_chUserID);
strcpy(reqUserLogin.Password, "1");
// 发出登陆请求
m_pUserApi->ReqUserLogin(&reqUserLogin, 0);
}
// 登录认证请求
int ReqAuth()
{
strcpy(g_chBrokerID, "guofu");
strcpy(g_chUserID, "m001");
CQdpFtdcAuthenticateField tReq;
memset(&tReq, 0, sizeof(CQdpFtdcAuthenticateField));
strncpy(tReq.AppID, "client_test_1.0", sizeof(tReq.AppID));
strncpy(tReq.AuthCode, "123456", sizeof(tReq.AuthCode));
strncpy(tReq.BrokerID, g_chBrokerID, sizeof(tReq.BrokerID));
strncpy(tReq.UserID, g_chUserID, sizeof(tReq.UserID));
return m_pUserApi->ReqAuthenticate(&tReq, 0);
}
// 双边报价请求
int SendQuoteOrder()
{
CQdpFtdcInputQuoteField quoteord;
memset("eord, 0, sizeof(CQdpFtdcInputQuoteField));
//经纪公司代码
strcpy(quoteord.BrokerID, "guofu");
//交易所
strcpy(quoteord.ExchangeID, "DCE");
//投资者代码
strcpy(quoteord.InvestorID, "guofu0001");
// 用户代码
strcpy(quoteord.UserID, g_chUserID);
// 合约代码
strcpy(quoteord.InstrumentID, "a2205");
quoteord.AskPrice = 7588;
quoteord.AskVolume = 3;
quoteord.AskOffsetFlag = QDP_FTDC_OF_Close;
quoteord.AskHedgeFlag = QDP_FTDC_CHF_Speculation;
quoteord.BidPrice = 7590;
quoteord.BidVolume = 3;
quoteord.BidOffsetFlag = QDP_FTDC_OF_Open;
quoteord.BidHedgeFlag = QDP_FTDC_CHF_Speculation;
quoteord.UserOrderLocalID = g_UserOrderLocalID;
quoteord.BidOrderRef = ++g_UserOrderLocalID;
quoteord.AskOrderRef = ++g_UserOrderLocalID;
int iRet = m_pUserApi->ReqQuoteInsert("eord, 0);
return iRet;
}
// 当客户端与量投科技建立起通信连接,客户端需要进行登录
virtual void OnFrontConnected()
{
printf("OnFrontConnected\n");
/*int nRet = ReqAuth();
printf("ReqAuthenticate:%d\n", nRet);*/
Login(); //无需认证的情况下可以直接登录
}
// 当客户端与量投科技通信连接断开时,该方法被调用
virtual void OnFrontDisconnected(int nReason)
{
// 当发生这个情况后,API会自动重新连接,客户端可不做处理
printf("OnFrontDisconnected, Reason=[%d].\n", nReason);
}
// 当客户端发出登录请求之后,该方法会被调用,通知客户端登录是否成功
virtual void OnRspUserLogin(CQdpFtdcRspUserLoginField *pRspUserLogin,
CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
printf("OnRspUserLogin:\n");
printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast);
printf("maxlocalid=[%d]\n", pRspUserLogin->MaxOrderLocalID);
if (pRspInfo->ErrorID != 0)
{
// 端登失败,客户端需进行错误处理
printf("Failed to login, errorcode=%d errormsg=%s requestid=%d chain=%d",
pRspInfo->ErrorID, pRspInfo->ErrorMsg, nRequestID, bIsLast);
}
//准备就绪 QDP_TERT_PRIVATE 私有流; QDP_TERT_PUBLIC 公有流;
CQdpFtdcFlowStatusField ftdField1;
ftdField1.SequenceSeries = QDP_TERT_PRIVATE;
ftdField1.bReady = true;
m_pUserApi->ReqReady(&ftdField1, 0);
ftdField1.SequenceSeries = QDP_TERT_PUBLIC;
ftdField1.bReady = true;
m_pUserApi->ReqReady(&ftdField1, 0);
//QryPosition();
//QryInstrument();
//SendQuoteOrder();
SendOrder();
//SendOrderAction();
}
///登录认证应答
virtual void OnRspAuthenticate(CQdpFtdcRtnAuthenticateField *pRtnAuthenticate, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (NULL == pRtnAuthenticate)
{
printf("NULL == pRtnAuthenticate\n");
return;
}
if (NULL == pRspInfo)
{
printf("NULL == pRspInfo\n");
return;
}
printf("OnRspAuthenticate: ErrorID=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
if (0 != pRspInfo->ErrorID)
{
return;
}
Login();
}
void OnRspQryInstrument(CQdpFtdcRspInstrumentField *pRspInstrument, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pRspInstrument)
{
printf("OnRspQryInstrument===>[%s],LowerLimitPrice=[%f],UpperLimitPrice=[%f]\n", pRspInstrument->InstrumentID, pRspInstrument->LowerLimitPrice, pRspInstrument->UpperLimitPrice);
}
}
void OnRspQuoteInsert(CQdpFtdcInputQuoteField *pInputQuote, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (NULL == pRspInfo)
{
printf("NULL == pRspInfo\n");
return;
}
printf("OnRspQuoteInsert: ErrorID=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
}
// 报单录入应答
virtual void OnRspOrderInsert(CQdpFtdcInputOrderField *pInputOrder,
CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
if (NULL == pRspInfo)
{
printf("NULL == pRspInfo\n");
return;
}
// 输出报单录入结果
printf("OnRspOrderInsert ErrorCode=[%d], ErrorMsg=[%s]", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
if (pRspInfo->ErrorID != 0)
{
printf("\n");
return;
}
if (NULL == pInputOrder)
{
printf("\nNULL == pInputOrder\n");
return;
}
printf(", orderSysID=[%s]\n", pInputOrder->OrderSysID);
}
virtual void OnRspOrderAction(CQdpFtdcOrderActionField *pOrderAction, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (NULL == pRspInfo)
{
printf("NULL == pRspInfo\n");
return;
}
printf("OnRspOrderAction ErrorCode=[%d], ErrorMsg=[%s]", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
if (pRspInfo->ErrorID != 0)
{
printf("\n");
return;
}
if (NULL == pOrderAction)
{
printf("\nNULL == pOrderAction\n");
return;
}
printf(", orderSysID=[%s]\n", pOrderAction->OrderSysID);
}
///报单回报
void OnRtnOrder(CQdpFtdcOrderField *pOrder)
{
if (NULL == pOrder)
{
printf("NULL == pOrder\n");
}
printf("OnRtnOrder->OrderSysID=[%s], OrderStatus=[%c]\n", pOrder->OrderSysID, pOrder->OrderStatus);
}
void OnRtnTrade(CQdpFtdcTradeField *pTrade)
{
if (NULL == pTrade)
{
printf("NULL == pTrade\n");
}
printf("OnRtnTrade->OrderSysID=[%s], tradeID=[%s]\n", pTrade->OrderSysID, pTrade->TradeID);
}
virtual void OnRtnInstrumentStatus(CQdpFtdcInstrumentStatusField *pInstrumentStatus)
{
if (NULL == pInstrumentStatus)
{
printf("NULL == pInstrumentStatus\n");
return;
}
printf("OnRtnInstrumentStatus:ExchangeID=[%s],InstrumentID=[%s],InstrumentStatus=[%c]\n",
pInstrumentStatus->ExchangeID, pInstrumentStatus->InstrumentID, pInstrumentStatus->InstrumentStatus);
}
void OnRspQryInvestorPosition(CQdpFtdcRspInvestorPositionField *pRspInvestorPosition, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pRspInvestorPosition)
{
printf("OnRspQryInvestorPosition:InvestorID=[%s],ExchangeID=[%s],InsturmentID=[%s],Position=[%d]\n",
pRspInvestorPosition->InvestorID, pRspInvestorPosition->ExchangeID, pRspInvestorPosition->InstrumentID, pRspInvestorPosition->Position);
}
}
// 针对用户请求的出错通知
virtual void OnRspError(CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
printf("OnRspError:\n");
printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast);
// 客户端需进行错误处理
{
//客户端的错误处理
}
}
private:
// 指向CQdpFtdcMduserApi实例的指针
CQdpFtdcTraderApi *m_pUserApi;
};
int main()
{
// 产生一个CQdpFtdcTraderApi实例
CQdpFtdcTraderApi *pUserApi = CQdpFtdcTraderApi::CreateFtdcTraderApi();
// 产生一个事件处理的实例
CSimpleHandler sh(pUserApi);
// 注册一事件处理的实例
pUserApi->RegisterSpi(&sh);
// 订阅私有流
// Qdp_TERT_RESTART:从本交易日开始重传
// Qdp_TERT_RESUME:从上次收到的续传
// Qdp_TERT_QUICK:只传送登录后私有流的内容
pUserApi->SubscribePrivateTopic(QDP_TERT_RESUME);
// 订阅公共流
// Qdp_TERT_RESTART:从本交易日开始重传
// Qdp_TERT_RESUME:从上次收到的续传
// Qdp_TERT_QUICK:只传送登录后公共流的内容
pUserApi->SubscribePublicTopic(QDP_TERT_RESUME);
// 设置量投科技服务的地址,可以注册多个地址备用
pUserApi->RegisterFront("tcp://112.94.162.109:30005");
// pUserApi->SetHeartbeatTimeout(240);
// 使客户端开始与后台服务建立连接
pUserApi->Init();
pUserApi->Join();
// 释放API实例
pUserApi->Release();
return 0;
}
Udp或者Shm API使用demo
// 一个简单的例子,介绍CQdpFtdcTraderApi和CQdpFtdcTraderSpi接口的使用。
// 本例将演示一个报单录入操作的过程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <cstdlib>
#include "QdpFtdcTraderApi.h"
#include <iostream>
#include <fstream>
#include <thread>
#include "QdpShmTraderApi.h"
#ifdef WIN32
#define WINDOWS
#endif
using namespace std;
#ifdef WINDOWS
#include <windows.h>
typedef HANDLE THREAD_HANDLE ;
#define SLEEP(ms) Sleep(ms)
#else
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
typedef pthread_t THREAD_HANDLE ;
#define SLEEP(ms) sleep((ms)/1000)
typedef unsigned int DWORD;
#endif
int g_UserOrderLocalID = 1;
class CThread
{
public:
/**构造函数
*/
CThread()
{
m_hThread = (THREAD_HANDLE)0;
m_IDThread = 0;
}
/**析构函数
*/
virtual ~CThread() {}
/**创建一个线程
* @return true:创建成功 false:创建失败
*/
virtual bool Create()
{
if (m_hThread != (THREAD_HANDLE)0)
{
return true;
}
bool ret = true;
#ifdef WIN32
m_hThread = ::CreateThread(NULL,0,_ThreadEntry,this,0,&m_IDThread);
if(m_hThread==NULL)
{
ret = false;
}
#else
ret = (::pthread_create(&m_hThread,NULL,&_ThreadEntry , this) == 0);
#endif
return ret;
}
void ExitThread()
{
#ifdef WIN32
::ExitThread(0);
#endif
}
private:
#ifdef WIN32
static DWORD WINAPI _ThreadEntry(LPVOID pParam)
#else
static void * _ThreadEntry(void *pParam)
#endif
{
CThread *pThread = (CThread *)pParam;
if(pThread->InitInstance())
{
pThread->Run();
}
pThread->ExitInstance();
//20140613 xuzh 如果设置为0,join部分就无法join了,导致资源无法释放
//pThread->m_hThread = (THREAD_HANDLE)0;
return NULL;
}
/**虚函数,子类可做一些实例化工作
* @return true:创建成功 false:创建失败
*/
virtual bool InitInstance()
{
return true;
}
/**虚函数,子类清楚实例
*/
virtual void ExitInstance() {}
/**线程开始运行,纯虚函数,子类必须继承实现
*/
virtual void Run() = 0;
private:
THREAD_HANDLE m_hThread; /**< 线程句柄 */
DWORD m_IDThread;
};
/////////////////////////////////////////////////////////////
class CMyWorkThread:public CThread
{
public:
CMyWorkThread(CQdpShmTraderApi *pSuperApi);
~CMyWorkThread(){};
virtual void Run();
public:
CQdpShmTraderApi *m_pSuperApi;
};
CMyWorkThread::CMyWorkThread(CQdpShmTraderApi *pSuperApi)
{
m_pSuperApi = pSuperApi;
}
//该线程一定不能阻塞,需要一直从Udp或者Shm通道获取数据
void CMyWorkThread::Run()
{
while(1)
{
if (NULL != m_pSuperApi)
{
int nRet = m_pSuperApi->ReadFromSession();
// printf("ReadFromSession ret:%d\n", nRet);
SLEEP(100);
}
else
printf("m_pSuperApi is NULL\n");
}
}
class CSupapiHandler : public CQdpShmTraderSpi
{
public:
// 构造函数,需要一个有效的指向CQdpFtdcMduserApi实例的指针
CSupapiHandler(CQdpShmTraderApi *pUserApi, char *chBrokerID, char *chUserID, char *chPasswd):m_pUserApi (pUserApi)
{
m_nRequestID = 0;
m_bLogin = false;
strcpy(m_chBrokerID, chBrokerID);
strcpy(m_chUserID, chUserID);
strcpy(m_chPasswd, chPasswd);
}
~CSupapiHandler () {}
void ReqLogin()
{
CQdpFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.UserProductInfo, "test");
strcpy(reqUserLogin.BrokerID, m_chBrokerID);
strcpy(reqUserLogin.UserID, m_chUserID);
strcpy(reqUserLogin.Password, m_chPasswd);
// 发出登陆请求
int iRet = m_pUserApi->ReqUserLogin(&reqUserLogin, 0);
printf("ReqUserLogin ret:%d\n", iRet);
}
void ReqAuth()
{
CQdpFtdcAuthenticateField reqField;
strcpy(reqField.BrokerID, m_chBrokerID);
strcpy(reqField.UserID, m_chUserID);
strncpy(reqField.AppID, "quantdo_InfiniV8_1", sizeof(reqField.AppID));
strncpy(reqField.AuthCode, "0f8596818807c41f71cbcc2338e973dd", sizeof(reqField.AuthCode));
//生产的话需要填具体信息
int iRet = m_pUserApi->ReqAuthenticate(&reqField, 0);
printf("ReqAuthenticate ret:%d\n", iRet);
}
// 当客户端与量投科技建立起通信连接,客户端需要进行登录
virtual void OnFrontConnected()
{
//如果是Udp连接,可以先断开之前的连接
ReqUserLogout();
printf("OnFrontConnected\n");
ReqAuth();
// ReqLogin(); //无需认证的情况下可以直接登录
}
// 当客户端与量投科技通信连接断开时,该方法被调用
virtual void OnFrontDisconnected(int nReason)
{
// 当发生这个情况后,API会自动重新连接,客户端可不做处理
printf("OnFrontDisconnected.\n");
}
///登录认证应答
virtual void OnRspAuthenticate(CQdpFtdcRtnAuthenticateField *pRtnAuthenticate, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
printf("OnRspAuthenticate: ");
if (NULL != pRspInfo && NULL != pRtnAuthenticate)
{
printf("UserID=[%s], ErrID=[%d], ErrMsg=[%s]\n",
pRtnAuthenticate->UserID, pRspInfo->ErrorID, pRspInfo->ErrorMsg);
if (0 != pRspInfo->ErrorID)
{
return;
}
}
ReqLogin();
}
// 当客户端发出登录请求之后,该方法会被调用,通知客户端登录是否成功
virtual void OnRspUserLogin(CQdpFtdcRspUserLoginField *pRspUserLogin,
CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
printf("OnRspUserLogin:\n");
printf("ErrorCode=[%d], ErrorMsg=[%s], UserID=[%s]\n",
pRspInfo->ErrorID, pRspInfo->ErrorMsg, pRspUserLogin->UserID);
printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast);
printf("maxlocalid=[%s]\n",pRspUserLogin->MaxOrderLocalID);
if (pRspInfo->ErrorID != 0)
{
if (pRspInfo->ErrorID != 106 )
{
// 端登失败,客户端需进行错误处理
printf("Failed to login, errorcode=%d errormsg=%s requestid=%d chain=%d",pRspInfo->ErrorID, pRspInfo->ErrorMsg, nRequestID, bIsLast);
exit(-1);
}
}
//用户最大本地报单号
g_UserOrderLocalID=atoi(pRspUserLogin->MaxOrderLocalID)+2;
printf("maxlocalid = [%s]\n",pRspUserLogin->MaxOrderLocalID);
printf("g_UserOrderLocalID = [%d]\n",g_UserOrderLocalID);
m_bLogin = true;
//直接退出登录, 测试使用
// ReqUserLogout();
// printf("reqUserLogout successfully!\n");
}
int SendOrder(string strExchangeID, string strInvestorID, string strInstrumentID, double LimitPrice, int Volume)
{
// 端登成功,发出报单录入请求
CQdpFtdcInputOrderField ord;
memset(&ord, 0, sizeof(ord));
//经纪公司代码
strcpy(ord.BrokerID, m_chBrokerID);
//交易所
strcpy(ord.ExchangeID, strExchangeID.c_str());
//投资者代码
strcpy(ord.InvestorID, strInvestorID.c_str());
// 用户代码
strcpy(ord.UserID, m_chUserID);
// 合约代码
strcpy(ord.InstrumentID, strInstrumentID.c_str());
//本地报单号8
sprintf(ord.UserOrderLocalID, "%012d", g_UserOrderLocalID++);
//报单类型
ord.OrderPriceType = QDP_FTDC_OPT_LimitPrice;
// 买卖方向
ord.Direction = QDP_FTDC_D_Buy;
// 开平标志
strcpy(&(ord.OffsetFlag), "0");
// 投机套保标志
strcpy(&(ord.HedgeFlag), "1");
// 价格
ord.LimitPrice = LimitPrice;
// 数量
ord.Volume = Volume;
// 有效期类型
ord.TimeCondition = QDP_FTDC_TC_GFD;
//成交量类型
ord.VolumeCondition = QDP_FTDC_VC_AV;
//强平原因
ord.ForceCloseReason = QDP_FTDC_FCR_NotForceClose;
// 自动挂起标志
ord.IsAutoSuspend = 0;
int ret = m_pUserApi->ReqOrderInsert(&ord, ++m_nRequestID);
return ret;
}
virtual void OnRspOrderInsert( CQdpFtdcInputOrderField *pInputOrder,
CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
// 输出报单录入结果
if(NULL != pRspInfo)
{
printf("OnRspOrderInsert ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
}
}
int SendOrderAction()
{
CQdpFtdcOrderActionField Quote;
memset(&Quote, 0, sizeof(Quote));
//经纪公司代码
strcpy(Quote.BrokerID, m_chBrokerID);
//交易所
strcpy(Quote.ExchangeID, "CFFEX");
//投资者代码
strcpy(Quote.InvestorID, "guofu000003");
// 用户代码
strcpy(Quote.UserID, m_chUserID);
//报单操作标志
Quote.ActionFlag = '0';
//撤单本地报单号
Quote.UserActionOrderLocalID == g_UserOrderLocalID++;
//系统报单编号
sprintf(Quote.OrderSysID, "10001");
int ret = m_pUserApi->ReqOrderAction(&Quote, 0);
return ret;
}
///报单回报
virtual void OnRtnOrder(CQdpFtdcOrderField *pOrder)
{
printf("OnRtnOrder->OrderSysID=[%s], OrderStatus=[%c]\n", pOrder->OrderSysID, pOrder->OrderStatus);
}
virtual void OnRspOrderAction(CQdpFtdcOrderActionField *pOrderAction, CQdpFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
printf("OnRspOrderAction ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
}
// 针对用户请求的出错通知
virtual void OnRspError(CQdpFtdcRspInfoField *pRspInfo,
int nRequestID,
bool bIsLast)
{
printf("OnRspError:\n");
printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast);
// 客户端需进行错误处理
{
//客户端的错误处理
}
}
///成交回报
virtual void OnRtnTrade(CQdpFtdcTradeField *pTrade)
{
printf("OnRtnTrade: OrderSysID=[%s], TradeID=[%s]\n", pTrade->OrderSysID, pTrade->TradeID);
}
///用户退出请求
void ReqUserLogout()
{
CQdpFtdcReqUserLogoutField field;
strcpy(field.BrokerID, m_chBrokerID);
strcpy(field.UserID, m_chUserID);
m_pUserApi->ReqUserLogout(&field,0);
}
virtual void OnRtnAck(int nRequestID)
{
printf("OnRtnAck: nRequestID=[%d]\n", nRequestID);
};
private:
// 指向CQdpFtdcMduserApi实例的指针
CQdpShmTraderApi *m_pUserApi;
int m_nRequestID;
TQdpFtdcBrokerIDType m_chBrokerID;
TQdpFtdcUserIDType m_chUserID;
TQdpFtdcPasswordType m_chPasswd;
public:
bool m_bLogin;
};
void CreateShmApi1()
{
CQdpShmTraderApi *pSupUserApi = CQdpShmTraderApi::CreateShmTraderApi();
// 产生一个事件处理的实例
CSupapiHandler supSh(pSupUserApi, "guofu", "m003", "1");
// 注册一事件处理的实例
pSupUserApi->RegisterSpi(&supSh);
//udp 模式
pSupUserApi->RegisterFront("udp://192.168.92.12:6099");
//共享内存模式
//pSupUserApi->RegisterFront("shm://65432:20");
pSupUserApi->Connect();
CMyWorkThread myCMyWorkThread1(pSupUserApi);
myCMyWorkThread1.Create();
while (!supSh.m_bLogin)
{
SLEEP(1000);
}
int iCount = 0;
for (int i=0;i< 10;i++)
{
//SLEEP(1000);
int iRet = supSh.SendOrder("CZCE", "guofuzcs0001", "CF001", 15000, 1);
if (iRet == 0)
{
iCount++;
}
}
printf("SendOrder iCount= %d\n",iCount);
SLEEP(6000);
supSh.ReqUserLogout();
printf("CreateShmApi1 End\n");
}
void CreateShmApi2()
{
CQdpShmTraderApi *pSupUserApi = CQdpShmTraderApi::CreateShmTraderApi();
// 产生一个事件处理的实例
CSupapiHandler supSh(pSupUserApi, "guofu", "m004", "1");
// 注册一事件处理的实例
pSupUserApi->RegisterSpi(&supSh);
//udp 模式
pSupUserApi->RegisterFront("udp://192.168.92.12:6099+1");
//共享内存模式
//pSupUserApi->RegisterFront("shm://65432:20");
pSupUserApi->Connect();
CMyWorkThread myCMyWorkThread2(pSupUserApi);
myCMyWorkThread2.Create();
while (!supSh.m_bLogin)
{
SLEEP(1000);
}
int iCount = 0;
for (int i=0;i< 10;i++)
{
//SLEEP(1000);
int iRet = supSh.SendOrder("CZCE", "guofuzce0002", "CF001", 15500, 1);
if (iRet == 0)
{
iCount++;
}
}
printf("SendOrder2 iCount= %d\n",iCount);
SLEEP(6000);
supSh.ReqUserLogout();
printf("CreateShmApi2 End\n");
}
int main()
{
std::thread threads[1];
int num = 1;
threads[0] = std::thread(CreateShmApi1);
//threads[1] = std::thread(CreateShmApi2);
for (int i = 0; i < num; i++)
{
threads[i].join();
}
return 0;
}