vnpy/archive/vn.lts_old/vnltsmd/pyltsmd/vnltsmd.cpp

663 lines
17 KiB
C++
Raw Normal View History

// MdApi.cpp : <20><><EFBFBD><EFBFBD> DLL Ӧ<>ó<EFBFBD><C3B3><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//
#include "stdafx.h"
#include "vnltsmd.h"
///-------------------------------------------------------------------------------------
///<2F><>Python<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C++<2B><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>õĺ<C3B5><C4BA><EFBFBD>
///-------------------------------------------------------------------------------------
void getInt(dict d, string key, int *value)
{
if (d.has_key(key)) //<2F><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڸü<DAB8>ֵ
{
object o = d[key]; //<2F><>ȡ<EFBFBD>ü<EFBFBD>ֵ
extract<int> x(o); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>
if (x.check()) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
{
*value = x(); //<2F><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>븳ֵ
}
}
};
void getDouble(dict d, string key, double *value)
{
if (d.has_key(key))
{
object o = d[key];
extract<double> x(o);
if (x.check())
{
*value = x();
}
}
};
void getChar(dict d, string key, char *value)
{
if (d.has_key(key))
{
object o = d[key];
extract<string> x(o);
if (x.check())
{
string s = x();
const char *buffer = s.c_str();
//<2F><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ָ<EFBFBD>븳ֵ<EBB8B3><D6B5><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>strcpy_s, vs2013ʹ<33><CAB9>strcpy<70><79><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//+1Ӧ<31><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪC++<2B>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ľ<EFBFBD>β<EFBFBD><CEB2><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
strcpy_s(value, strlen(buffer) + 1, buffer);
}
}
};
///-------------------------------------------------------------------------------------
///C++<2B>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><E6B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
///-------------------------------------------------------------------------------------
void MdApi::OnFrontConnected()
{
Task task = Task();
task.task_name = ONFRONTCONNECTED;
this->task_queue.push(task);
};
void MdApi::OnFrontDisconnected(int nReason)
{
Task task = Task();
task.task_name = ONFRONTDISCONNECTED;
task.task_id = nReason;
this->task_queue.push(task);
};
void MdApi::OnHeartBeatWarning(int nTimeLapse)
{
Task task = Task();
task.task_name = ONHEARTBEATWARNING;
task.task_id = nTimeLapse;
this->task_queue.push(task);
};
void MdApi::OnRspError(CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
Task task = Task();
task.task_name = ONRSPERROR;
if (pRspInfo)
{
task.task_error = *pRspInfo;
}
else
{
CSecurityFtdcRspInfoField empty_error = CSecurityFtdcRspInfoField();
memset(&empty_error, 0, sizeof(empty_error));
task.task_error = empty_error;
}
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
};
void MdApi::OnRspUserLogin(CSecurityFtdcRspUserLoginField *pRspUserLogin, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
Task task = Task();
task.task_name = ONRSPUSERLOGIN;
task.task_data = *pRspUserLogin;
if (pRspInfo)
{
task.task_error = *pRspInfo;
}
else
{
CSecurityFtdcRspInfoField empty_error = CSecurityFtdcRspInfoField();
memset(&empty_error, 0, sizeof(empty_error));
task.task_error = empty_error;
}
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
};
void MdApi::OnRspUserLogout(CSecurityFtdcUserLogoutField *pUserLogout, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
Task task = Task();
task.task_name = ONRSPUSERLOGOUT;
task.task_data = *pUserLogout;
if (pRspInfo)
{
task.task_error = *pRspInfo;
}
else
{
CSecurityFtdcRspInfoField empty_error = CSecurityFtdcRspInfoField();
memset(&empty_error, 0, sizeof(empty_error));
task.task_error = empty_error;
}
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
};
void MdApi::OnRspSubMarketData(CSecurityFtdcSpecificInstrumentField *pSpecificInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
Task task = Task();
task.task_name = ONRSPSUBMARKETDATA;
task.task_data = *pSpecificInstrument;
if (pRspInfo)
{
task.task_error = *pRspInfo;
}
else
{
CSecurityFtdcRspInfoField empty_error = CSecurityFtdcRspInfoField();
memset(&empty_error, 0, sizeof(empty_error));
task.task_error = empty_error;
}
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
};
void MdApi::OnRspUnSubMarketData(CSecurityFtdcSpecificInstrumentField *pSpecificInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
Task task = Task();
task.task_name = ONRSPUNSUBMARKETDATA;
task.task_data = *pSpecificInstrument;
if (pRspInfo)
{
task.task_error = *pRspInfo;
}
else
{
CSecurityFtdcRspInfoField empty_error = CSecurityFtdcRspInfoField();
memset(&empty_error, 0, sizeof(empty_error));
task.task_error = empty_error;
}
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
};
void MdApi::OnRtnDepthMarketData(CSecurityFtdcDepthMarketDataField *pDepthMarketData)
{
Task task = Task();
task.task_name = ONRTNDEPTHMARKETDATA;
task.task_data = *pDepthMarketData;
this->task_queue.push(task);
};
///-------------------------------------------------------------------------------------
///<2F><><EFBFBD><EFBFBD><EFBFBD>̴߳Ӷ<CCB4><D3B6><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ת<EFBFBD><D7AA>Ϊpython<6F><6E><EFBFBD><EFBFBD><EFBFBD>󣬽<EFBFBD><F3A3ACBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
///-------------------------------------------------------------------------------------
void MdApi::processTask()
{
while (1)
{
Task task = this->task_queue.wait_and_pop();
switch (task.task_name)
{
case ONFRONTCONNECTED:
{
this->processFrontConnected(task);
break;
}
case ONFRONTDISCONNECTED:
{
this->processFrontDisconnected(task);
break;
}
case ONHEARTBEATWARNING:
{
this->processHeartBeatWarning(task);
break;
}
case ONRSPERROR:
{
this->processRspError(task);
break;
}
case ONRSPUSERLOGIN:
{
this->processRspUserLogin(task);
break;
}
case ONRSPUSERLOGOUT:
{
this->processRspUserLogout(task);
break;
}
case ONRSPSUBMARKETDATA:
{
this->processRspSubMarketData(task);
break;
}
case ONRSPUNSUBMARKETDATA:
{
this->processRspUnSubMarketData(task);
break;
}
case ONRTNDEPTHMARKETDATA:
{
this->processRtnDepthMarketData(task);
break;
}
};
}
};
void MdApi::processFrontConnected(Task task)
{
//<2F><><EFBFBD><EFBFBD>python<6F><6E><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ûص<C3BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ȼ<EFBFBD>ȡȫ<C8A1><C8AB><EFBFBD><EFBFBD>GIL<49><4C><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PyLock lock;
this->onFrontConnected();
};
void MdApi::processFrontDisconnected(Task task)
{
PyLock lock;
this->onFrontDisconnected(task.task_id);
};
void MdApi::processHeartBeatWarning(Task task)
{
PyLock lock;
this->onHeartBeatWarning(task.task_id);
};
void MdApi::processRspError(Task task)
{
PyLock lock;
CSecurityFtdcRspInfoField task_error = any_cast<CSecurityFtdcRspInfoField>(task.task_error);
dict error;
error["ErrorMsg"] = task_error.ErrorMsg;
error["ErrorID"] = task_error.ErrorID;
this->onRspError(error, task.task_id, task.task_last);
};
void MdApi::processRspUserLogin(Task task)
{
PyLock lock;
CSecurityFtdcRspUserLoginField task_data = any_cast<CSecurityFtdcRspUserLoginField>(task.task_data);
dict data;
data["MaxOrderRef"] = task_data.MaxOrderRef;
data["UserID"] = task_data.UserID;
data["TradingDay"] = task_data.TradingDay;
data["SessionID"] = task_data.SessionID;
data["SystemName"] = task_data.SystemName;
data["FrontID"] = task_data.FrontID;
data["BrokerID"] = task_data.BrokerID;
data["LoginTime"] = task_data.LoginTime;
CSecurityFtdcRspInfoField task_error = any_cast<CSecurityFtdcRspInfoField>(task.task_error);
dict error;
error["ErrorMsg"] = task_error.ErrorMsg;
error["ErrorID"] = task_error.ErrorID;
this->onRspUserLogin(data, error, task.task_id, task.task_last);
};
void MdApi::processRspUserLogout(Task task)
{
PyLock lock;
CSecurityFtdcUserLogoutField task_data = any_cast<CSecurityFtdcUserLogoutField>(task.task_data);
dict data;
data["UserID"] = task_data.UserID;
data["BrokerID"] = task_data.BrokerID;
CSecurityFtdcRspInfoField task_error = any_cast<CSecurityFtdcRspInfoField>(task.task_error);
dict error;
error["ErrorMsg"] = task_error.ErrorMsg;
error["ErrorID"] = task_error.ErrorID;
this->onRspUserLogout(data, error, task.task_id, task.task_last);
};
void MdApi::processRspSubMarketData(Task task)
{
PyLock lock;
CSecurityFtdcSpecificInstrumentField task_data = any_cast<CSecurityFtdcSpecificInstrumentField>(task.task_data);
dict data;
data["InstrumentID"] = task_data.InstrumentID;
data["ExchangeID"] = task_data.ExchangeID;
CSecurityFtdcRspInfoField task_error = any_cast<CSecurityFtdcRspInfoField>(task.task_error);
dict error;
error["ErrorMsg"] = task_error.ErrorMsg;
error["ErrorID"] = task_error.ErrorID;
this->onRspSubMarketData(data, error, task.task_id, task.task_last);
};
void MdApi::processRspUnSubMarketData(Task task)
{
PyLock lock;
CSecurityFtdcSpecificInstrumentField task_data = any_cast<CSecurityFtdcSpecificInstrumentField>(task.task_data);
dict data;
data["InstrumentID"] = task_data.InstrumentID;
data["ExchangeID"] = task_data.ExchangeID;
CSecurityFtdcRspInfoField task_error = any_cast<CSecurityFtdcRspInfoField>(task.task_error);
dict error;
error["ErrorMsg"] = task_error.ErrorMsg;
error["ErrorID"] = task_error.ErrorID;
this->onRspUnSubMarketData(data, error, task.task_id, task.task_last);
};
void MdApi::processRtnDepthMarketData(Task task)
{
PyLock lock;
CSecurityFtdcDepthMarketDataField task_data = any_cast<CSecurityFtdcDepthMarketDataField>(task.task_data);
dict data;
data["HighestPrice"] = task_data.HighestPrice;
data["BidPrice5"] = task_data.BidPrice5;
data["BidPrice4"] = task_data.BidPrice4;
data["BidPrice1"] = task_data.BidPrice1;
data["BidPrice3"] = task_data.BidPrice3;
data["BidPrice2"] = task_data.BidPrice2;
data["LowerLimitPrice"] = task_data.LowerLimitPrice;
data["OpenPrice"] = task_data.OpenPrice;
data["AskPrice5"] = task_data.AskPrice5;
data["AskPrice4"] = task_data.AskPrice4;
data["AskPrice3"] = task_data.AskPrice3;
data["PreClosePrice"] = task_data.PreClosePrice;
data["AskPrice1"] = task_data.AskPrice1;
data["PreSettlementPrice"] = task_data.PreSettlementPrice;
data["AskVolume1"] = task_data.AskVolume1;
data["UpdateTime"] = task_data.UpdateTime;
data["UpdateMillisec"] = task_data.UpdateMillisec;
data["AveragePrice"] = task_data.AveragePrice;
data["BidVolume5"] = task_data.BidVolume5;
data["BidVolume4"] = task_data.BidVolume4;
data["BidVolume3"] = task_data.BidVolume3;
data["BidVolume2"] = task_data.BidVolume2;
data["PreOpenInterest"] = task_data.PreOpenInterest;
data["AskPrice2"] = task_data.AskPrice2;
data["Volume"] = task_data.Volume;
data["AskVolume3"] = task_data.AskVolume3;
data["AskVolume2"] = task_data.AskVolume2;
data["AskVolume5"] = task_data.AskVolume5;
data["AskVolume4"] = task_data.AskVolume4;
data["UpperLimitPrice"] = task_data.UpperLimitPrice;
data["BidVolume1"] = task_data.BidVolume1;
data["InstrumentID"] = task_data.InstrumentID;
data["ClosePrice"] = task_data.ClosePrice;
data["ExchangeID"] = task_data.ExchangeID;
data["TradingDay"] = task_data.TradingDay;
data["PreDelta"] = task_data.PreDelta;
data["OpenInterest"] = task_data.OpenInterest;
data["CurrDelta"] = task_data.CurrDelta;
data["Turnover"] = task_data.Turnover;
data["LastPrice"] = task_data.LastPrice;
data["SettlementPrice"] = task_data.SettlementPrice;
data["ExchangeInstID"] = task_data.ExchangeInstID;
data["LowestPrice"] = task_data.LowestPrice;
data["ActionDay"] = task_data.ActionDay;
this->onRtnDepthMarketData(data);
};
///-------------------------------------------------------------------------------------
///<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
///-------------------------------------------------------------------------------------
void MdApi::createFtdcMdApi(string pszFlowPath)
{
this->api = CSecurityFtdcMdApi::CreateFtdcMdApi(pszFlowPath.c_str());
this->api->RegisterSpi(this);
};
void MdApi::release()
{
this->api->Release();
};
void MdApi::init()
{
this->api->Init();
};
int MdApi::join()
{
int i = this->api->Join();
return i;
};
int MdApi::exit()
{
//<2F>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>API<50><49>û<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ڰ<EFBFBD>ȫ<EFBFBD>˳<EFBFBD>API<50>ã<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>join<69>ƺ<EFBFBD><C6BA><EFBFBD>̫<EFBFBD>ȶ<EFBFBD>
this->api->RegisterSpi(NULL);
this->api->Release();
this->api = NULL;
return 1;
};
string MdApi::getTradingDay()
{
string day = this->api->GetTradingDay();
return day;
};
void MdApi::registerFront(string pszFrontAddress)
{
this->api->RegisterFront((char*)pszFrontAddress.c_str());
};
int MdApi::subscribeMarketData(dict req)
{
char instrumentID[256];
char exchangeID[256];
getChar(req, "InstrumentID", instrumentID);
getChar(req, "ExchangeID", exchangeID);
char* myreq[1] = { instrumentID };
int i = this->api->SubscribeMarketData(myreq, 1, exchangeID);
return i;
};
int MdApi::unSubscribeMarketData(dict req)
{
char instrumentID[256];
char exchangeID[256];
getChar(req, "InstrumentID", instrumentID);
getChar(req, "ExchangeID", exchangeID);
char* myreq[1] = { instrumentID };
int i = this->api->UnSubscribeMarketData(myreq, 1, exchangeID);
return i;
};
int MdApi::reqUserLogin(dict req, int nRequestID)
{
CSecurityFtdcReqUserLoginField myreq = CSecurityFtdcReqUserLoginField();
memset(&myreq, 0, sizeof(myreq));
getChar(req, "MacAddress", myreq.MacAddress);
getChar(req, "UserProductInfo", myreq.UserProductInfo);
getChar(req, "UserID", myreq.UserID);
getChar(req, "AuthCode", myreq.AuthCode);
getChar(req, "TradingDay", myreq.TradingDay);
getChar(req, "InterfaceProductInfo", myreq.InterfaceProductInfo);
getChar(req, "BrokerID", myreq.BrokerID);
getChar(req, "ClientIPAddress", myreq.ClientIPAddress);
getChar(req, "OneTimePassword", myreq.OneTimePassword);
getChar(req, "ProtocolInfo", myreq.ProtocolInfo);
getChar(req, "Password", myreq.Password);
int i = this->api->ReqUserLogin(&myreq, nRequestID);
return i;
};
int MdApi::reqUserLogout(dict req, int nRequestID)
{
CSecurityFtdcUserLogoutField myreq = CSecurityFtdcUserLogoutField();
memset(&myreq, 0, sizeof(myreq));
getChar(req, "UserID", myreq.UserID);
getChar(req, "BrokerID", myreq.BrokerID);
int i = this->api->ReqUserLogout(&myreq, nRequestID);
return i;
};
///-------------------------------------------------------------------------------------
///Boost.Python<6F><6E>װ
///-------------------------------------------------------------------------------------
struct MdApiWrap : MdApi, wrapper < MdApi >
{
virtual void onFrontConnected()
{
//<2F><><EFBFBD>µ<EFBFBD>try...catch...<2E><><EFBFBD><EFBFBD>ʵ<EFBFBD>ֲ<EFBFBD>׽python<6F><6E><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>ܣ<EFBFBD><DCA3><EFBFBD>ֹC++ֱ<>ӳ<EFBFBD><D3B3><EFBFBD>ԭ<EFBFBD><D4AD>δ֪<CEB4>ı<EFBFBD><C4B1><EFBFBD>
try
{
this->get_override("onFrontConnected")();
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onFrontDisconnected(int i)
{
try
{
this->get_override("onFrontDisconnected")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onHeartBeatWarning(int i)
{
try
{
this->get_override("onHeartBeatWarning")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspError(dict data, int id, bool last)
{
try
{
this->get_override("onRspError")(data, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogin(dict data, dict error, int id, bool last)
{
try
{
this->get_override("onRspUserLogin")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogout(dict data, dict error, int id, bool last)
{
try
{
this->get_override("onRspUserLogout")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspSubMarketData(dict data, dict error, int id, bool last)
{
try
{
this->get_override("onRspSubMarketData")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUnSubMarketData(dict data, dict error, int id, bool last)
{
try
{
this->get_override("onRspUnSubMarketData")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnDepthMarketData(dict data)
{
try
{
this->get_override("onRtnDepthMarketData")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
};
BOOST_PYTHON_MODULE(vnltsmd)
{
PyEval_InitThreads(); //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>У<EFBFBD><D0A3><EFBFBD>֤<EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>GIL
class_<MdApiWrap, boost::noncopyable>("MdApi")
.def("createFtdcMdApi", &MdApiWrap::createFtdcMdApi)
.def("release", &MdApiWrap::release)
.def("init", &MdApiWrap::init)
.def("join", &MdApiWrap::join)
.def("exit", &MdApiWrap::exit)
.def("getTradingDay", &MdApiWrap::getTradingDay)
.def("registerFront", &MdApiWrap::registerFront)
.def("subscribeMarketData", &MdApiWrap::subscribeMarketData)
.def("unSubscribeMarketData", &MdApiWrap::unSubscribeMarketData)
.def("reqUserLogin", &MdApiWrap::reqUserLogin)
.def("reqUserLogout", &MdApiWrap::reqUserLogout)
.def("onFrontConnected", pure_virtual(&MdApiWrap::onFrontConnected))
.def("onFrontDisconnected", pure_virtual(&MdApiWrap::onFrontDisconnected))
.def("onHeartBeatWarning", pure_virtual(&MdApiWrap::onHeartBeatWarning))
.def("onRspError", pure_virtual(&MdApiWrap::onRspError))
.def("onRspUserLogin", pure_virtual(&MdApiWrap::onRspUserLogin))
.def("onRspUserLogout", pure_virtual(&MdApiWrap::onRspUserLogout))
.def("onRspSubMarketData", pure_virtual(&MdApiWrap::onRspSubMarketData))
.def("onRspUnSubMarketData", pure_virtual(&MdApiWrap::onRspUnSubMarketData))
.def("onRtnDepthMarketData", pure_virtual(&MdApiWrap::onRtnDepthMarketData))
;
};