代码拉取完成,页面将自动刷新
同步操作将从 1ndu5try/QTctp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "tradespi.h"
#include <stdio.h>
#include <QDebug>
QString Hyarray[3000][4];
int k;
void CTraderSpi::OnFrontConnected()
{
cout << "交易建立网络连接成功" << endl;
Status = LSConnected;
foreach(CThostFtdcTraderSpi* traderSpi, m_SpiList)
{
traderSpi->OnFrontConnected();
}
qDebug("</OnFrontConnected>\n");
CThostFtdcReqAuthenticateField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID,"9999");
strcpy(req.UserID,"204925");
strcpy(req.AuthCode,"0000000000000000");
strcpy(req.AppID,"simnow_client_test");
pUserApi->ReqAuthenticate(&req,++iRequestID);
};
void CTraderSpi::init()
{
}
void CTraderSpi::RegisterSpi(CThostFtdcTraderSpi *pSpi){
m_SpiList.append(pSpi);
}
void CTraderSpi::UnReisterSpi(CThostFtdcTraderSpi *pSpi){
for (int i = 0; i < m_SpiList.size();i++) {
CThostFtdcTraderSpi* traderSpi = m_SpiList.at(i);
if (traderSpi == pSpi){
m_SpiList.removeAt(i);
break;
}
}
}
void CTraderSpi::OnFrontDisconnected(int nReason)
{
qDebug("<OnFrontDisconnected>\n");
qDebug("\tnReason [%d]\n", nReason);
Status = LSDisconnected;
foreach(CThostFtdcTraderSpi* traderSpi, m_SpiList)
{
traderSpi->OnFrontDisconnected(nReason);
}
qDebug("</OnFrontDisconnected>\n");
}
void CTraderSpi::OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
foreach(CThostFtdcTraderSpi* traderSpi, m_SpiList)
{
traderSpi->OnRspAuthenticate(pRspAuthenticateField, pRspInfo,nRequestID,bIsLast);
}
if (pRspInfo)
{
if (pRspInfo->ErrorID == 0){
Status = LSAuthenticate_successful;
cout<<"=LSAuthenticate_successful "<<endl;
}else
{
Status = LSAuthenticate_failed;
cout<<"LSAuthenticate_failed "<<endl;
}
}
}
void CTraderSpi::ReqQryInvestorPosition()
{
CThostFtdcQryInvestorPositionField req ;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID,"9999");
strcpy(req.InvestorID,"204925");
strcpy(req.InstrumentID,INSTRUMENT_ID);
pUserApi->ReqQryInvestorPosition(&req,++iRequestID);
}
void CTraderSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pRspInfo)
{
if (pRspInfo->ErrorID == 0){
Status = LSLogin_successful;
cout<<"login successful "<<endl;
FRONT_ID = pRspUserLogin->FrontID;
SESSION_ID = pRspUserLogin->SessionID;
ReqSettlementInfoConfirm();
}else
{
cout<<"login unsuccessful "<<endl;
Status = LSLogin_failed;
}
}
};
void CTraderSpi::OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
}
void CTraderSpi::ReqOrderAction(QString brokerid,QString wth,QString jys)
{
QByteArray bid = brokerid.toLatin1();
char *broid = bid.data();
QByteArray wtt = wth.toLatin1();
char *wthao = wtt.data();
QByteArray jyy = jys.toLatin1();
char *jysuo = jyy.data();
CThostFtdcInputOrderActionField req;
memset(&req,0,sizeof (req));
strcpy(req.BrokerID,broid);
strcpy(req.OrderSysID,wthao);
strcpy(req.ExchangeID,jysuo);
req.ActionFlag = THOST_FTDC_AF_Delete;
pUserApi->ReqOrderAction(&req,++iRequestID);
}
void CTraderSpi::ReqOrderInsert(QString dm,QString lx,int lots,double price)
{
CThostFtdcInputOrderField ord ;
memset(&ord, 0, sizeof(ord));
strcpy_s(ord.BrokerID, "9999");
strcpy_s(ord.InvestorID, "204925");
strcpy_s(ord.ExchangeID, "SHFE");
strcpy_s(ord.InstrumentID,dm.toStdString().data() );
sprintf(ORDER_REF,"%d",iRequestID);
strcpy_s(ord.OrderRef,ORDER_REF );
ord.OrderPriceType = THOST_FTDC_OPT_LimitPrice;//限价
if(lx =="kd"){
ord.Direction = THOST_FTDC_D_Buy;//买 DIRECTION
ord.CombOffsetFlag[0] = THOST_FTDC_OF_Open;//开
}
else if(lx =="pd"){
ord.Direction = THOST_FTDC_D_Buy;//买 DIRECTION
ord.CombOffsetFlag[0] = THOST_FTDC_OF_CloseToday;//开
}
else if(lx =="kk"){
ord.Direction = THOST_FTDC_D_Sell;//买 DIRECTION
ord.CombOffsetFlag[0] = THOST_FTDC_OF_Open;//开
}
else if(lx =="pk"){
ord.Direction = THOST_FTDC_D_Sell;//买 DIRECTION
ord.CombOffsetFlag[0] = THOST_FTDC_OF_CloseToday;//开
}
ord.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;//投机
ord.LimitPrice = price;
ord.VolumeTotalOriginal = lots;
ord.TimeCondition = THOST_FTDC_TC_GFD;///当日有效
ord.VolumeCondition = THOST_FTDC_VC_AV;///任意数量
ord.MinVolume = 1;
ord.ContingentCondition = THOST_FTDC_CC_Immediately;
ord.StopPrice = 0;
ord.ForceCloseReason = THOST_FTDC_FCC_NotForceClose;
ord.IsAutoSuspend = 0;
pUserApi->ReqOrderInsert(&ord, ++iRequestID);
};
void CTraderSpi::OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
};
void CTraderSpi::OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
qDebug("<OnRspQryInvestorPosition>\n");
if(pInvestorPosition == nullptr)return;
QString dm = pInvestorPosition->InstrumentID;
QString lx = (QString)pInvestorPosition->PosiDirection;
int lots = pInvestorPosition->Position;
double cb = pInvestorPosition->PositionCost/lots/hy(dm).hycs;
QString CCdata = dm +"," +lx +"," +QString::number(lots) +"," +QString("%1").arg(cb,0,'f',2);
emit sendDataCC(CCdata);
if(bIsLast&&!IsErrorRspInfo(pRspInfo)){
//ReqOrderInsert();
}
}
bool CTraderSpi::IsErrorRspInfo(CThostFtdcRspInfoField *pRspInfo)
{
bool bResult = ((pRspInfo) && (pRspInfo->ErrorID != 0));
if (bResult)
{
cerr << "-->>ErrorID = " << pRspInfo->ErrorID << ", ErrorMsg =" << pRspInfo->ErrorMsg << endl;
}
return bResult;
}
void CTraderSpi::ReqQryTradingAccount()
{
CThostFtdcQryTradingAccountField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,"9999");
strcpy(req.InvestorID,"204925");
pUserApi->ReqQryTradingAccount(&req,++iRequestID);
};
void CTraderSpi::OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
QString accout = pTradingAccount->AccountID;
double zqy = pTradingAccount->Balance;
double bzj = pTradingAccount->FrozenMargin;
double kyzj = pTradingAccount->Available;
double fxd = bzj*100/zqy;
QString datazj =accout + "," +QString("%1").arg(zqy,0,'f',2)+ "," +QString::number(bzj)+ "," +QString("%1").arg(kyzj,0,'f',2)+ "," +QString::number(fxd);
emit sendDataZJ(datazj);
if(bIsLast&&!IsErrorRspInfo(pRspInfo)){
Sleep(2000);
ReqQryInvestorPosition();
}
};
void CTraderSpi::ReqQryInstrument()
{
CThostFtdcQryInstrumentField req;
memset(&req,0,sizeof(req));
strcpy(req.InstrumentID,INSTRUMENT_ID);
pUserApi->ReqQryInstrument(&req,++iRequestID);
};
void CTraderSpi::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
qDebug("<OnRspQryInstrument>\n");
if(bIsLast&&!IsErrorRspInfo(pRspInfo)){
Sleep(1000);
ReqQryTradingAccount();
}
QString dm =pInstrument->InstrumentID;
QString mc = QString::fromLocal8Bit(pInstrument->InstrumentName);
QString cs =QString::number(pInstrument->VolumeMultiple);
QString ds =QString::number(pInstrument->PriceTick);
QString hy = dm +","+mc+","+cs+","+ds;
emit sendDataHY(hy);
Hyarray[k][0] =dm;
Hyarray[k][1] =mc;
Hyarray[k][2] =cs;
Hyarray[k][3] =ds;
k++;
}
void CTraderSpi::OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if(bIsLast&&!IsErrorRspInfo(pRspInfo)){
ReqQryInstrument();
}
};
void CTraderSpi::ReqSettlementInfoConfirm()
{
CThostFtdcSettlementInfoConfirmField req;
memset(&req,0,sizeof(req));
strcpy(req.BrokerID,"9999");
strcpy(req.InvestorID,"204925");
pUserApi->ReqSettlementInfoConfirm(&req,++iRequestID);
};
void CTraderSpi::OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
};
void CTraderSpi::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
foreach(CThostFtdcTraderSpi* traderSpi, m_SpiList)
{
traderSpi->OnRspError(pRspInfo,nRequestID,bIsLast);
}
};
void CTraderSpi::OnRtnOrder(CThostFtdcOrderField *pOrder)
{
qDebug("<OnRtnOrder>\n");
QString zt ;
if(pOrder->OrderStatus==THOST_FTDC_OST_AllTraded){
zt = QString::fromLocal8Bit("全部成交");
}
else if(pOrder->OrderStatus==THOST_FTDC_OST_PartTradedQueueing ||pOrder->OrderStatus==THOST_FTDC_OST_PartTradedNotQueueing){
zt = QString::fromLocal8Bit("部分成交");
}
else if(pOrder->OrderStatus==THOST_FTDC_OST_NoTradeQueueing ||pOrder->OrderStatus==THOST_FTDC_OST_NoTradeNotQueueing){
zt = QString::fromLocal8Bit("未成交");
}
else if(pOrder->OrderStatus==THOST_FTDC_OST_Canceled){
zt = QString::fromLocal8Bit("已撤单");
}
else {
return;
}
QString wttime = pOrder->InsertTime;
QString dm = pOrder->InstrumentID;
QString bs = (QString)pOrder->Direction;
QString kp = (QString)pOrder->CombOffsetFlag;
QString lots = QString::number(pOrder->VolumeTotalOriginal);
QString price = QString::number(pOrder->LimitPrice);
QString wth = pOrder->OrderSysID;
QString jys = pOrder->ExchangeID;
QString WTdata = wttime + "," + dm + "," +bs + "," + kp +","+lots+","+price+","+zt+","+wth+","+jys;
emit sendDataWT(WTdata);
};
void CTraderSpi::OnRtnTrade(CThostFtdcTradeField *pTrade)
{
qDebug("OnRtnTrade............");
QString cjtime = pTrade->TradeTime;
QString dm = pTrade->InstrumentID;
QString bs = (QString)pTrade->Direction;
QString kp = (QString)pTrade->OffsetFlag;
QString lots = QString::number( pTrade->Volume);
QString price = QString::number( pTrade->Price);
QString wth = pTrade->OrderSysID;
QString jys = pTrade->ExchangeID;
QString CTdata = cjtime + "," + dm + "," +bs + "," + kp +","+lots+","+price+","+wth+","+jys;
emit sendDataCJ(CTdata);
}
bool CTraderSpi::IsMyOrder(CThostFtdcOrderField *pOrder)
{
return ((pOrder->FrontID == FRONT_ID)&&(pOrder->SessionID ==SESSION_ID));
}
bool CTraderSpi::IsTradingOrder(CThostFtdcOrderField *pOrder)
{
return ((pOrder->OrderStatus != THOST_FTDC_OST_PartTradedNotQueueing)
&&(pOrder->OrderStatus != THOST_FTDC_OST_Canceled)
&&(pOrder->OrderStatus != THOST_FTDC_OST_AllTraded));
}
CTraderSpi::CTraderSpi(QObject *parent) : QObject(parent)
{
this->setParent(parent);
}
CTraderSpi::HYstruct CTraderSpi::hy(QString dm)
{
HYstruct myhy;
for(int i=0;i<2800;i++){
if(dm==Hyarray[i][0]){
myhy.hycs = Hyarray[i][2].toInt();
myhy.hyds = Hyarray[i][3].toDouble();
break;
}
}
return myhy;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。