开发l2行情接口怎么设计行情秒数据处理程序?
一般情况下币安行情数据,开发l2行情接口怎么设计行情秒数据处理程序可以精确到分或秒的,小编简单的示例一组程序:
#define MAX_ACCEPTABLE_PRICE (1.0E+15)
#define DEAL_TOO_MAX( x ) if( ((x)>MAX_ACCEPTABLE_PRICE) || ((x)<(0-MAX_ACCEPTABLE_PRICE)) ) x = 0
//HANDLE g_hEvent = CreateEvent(NULL, true, false, NULL);
namespace acsy
namespace monitor
//tools实现
namespace ff = boost::asio;
int tools::timer_loop(boost::asio::yield_context yield)
boost::asio::deadline_timer timer(svc);
try{
while (true)
timer.expires_from_now( boost::posix_time::seconds(600) );
timer.async_wait(yield);
if ( on_timer(yield) < 0 )
break;
catch (std::exception& e)
展开全文
ACSY_DEBUG_FUNC() << e.what();
catch (...)
ACSY_DEBUG_FUNC() << "error!";
return 0;
int tools::on_timer(boost::asio::yield_context yield)
static util::AcsyTimeSec ats_save_bg("15:30:00");
static util::AcsyTimeSec ats_save_ed("15:40:00");
util::AcsyTimeSec ats(util::AcsyDateTime::nowLocal());
obm.obtain();
return 0;
int tools::main()
/*obm.obtain();*/
boost::coroutines::attributes coattr;
coattr.size = 4* 1024 * 1024; //4M
boost::asio::spawn(svc, std::bind(&tools::timer_loop, this, std::placeholders::_1),coattr);
datasvrlog ds;
ref().logger().registerLogger(&ds);
// 产生一个CThostFtdcTraderApi实例
CThostFtdcTraderApi *pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
CThostFtdcMdApi *pUserApi1 = CThostFtdcMdApi::CreateFtdcMdApi();
// 产生一个事件处理的实例
CSimpleHandler sh(pUserApi, this);
CSimpleHandler1 sh1(pUserApi1, this);
// 注册 事件处理的实例
pUserApi->RegisterSpi(&sh);
pUserApi1->RegisterSpi(&sh1);
// 订阅私有流
// TERT_RESTART:从本交易日开始重传.
// TERT_RESUME:从上次收到的续传.
// TERT_QUICK:只传送登录后私有流的内容.
pUserApi->SubscribePrivateTopic(THOST_TERT_RESUME);
// 订阅公共流
// TERT_RESTART:从本交易日开始重传.
// TERT_RESUME:从上次收到的续传.
// TERT_QUICK:只传送登录后公共流的内容.
pUserApi->SubscribePublicTopic(THOST_TERT_RESUME);
// 设置交易托管系统服务的地址币安行情数据,可以注册多个地址备用.
pUserApi->RegisterFront("tcp://180.168.146.187:10000");
pUserApi1->RegisterFront("tcp://180.168.146.187:10010");
// 使客户端开始与后台服务建立连接.
pUserApi->Init();
pUserApi1->Init();
// 客户端等待报单操作完成
// WaitForSingleObject(g_hEvent, INFINITE);
svc.run();
std::string ss;
std::cin >> ss;
// 释放API实例
pUserApi->Release();
pUserApi1->Release();
system("pause");
return 0;
tools::tools():obm(this)
m_insok.value = false;
m_login.value = false;
strcpy(m_chBrokerID, "9999");
strcpy(m_chUserID, "020368");
strcpy(m_chUserPassword, "106317");
strcpy(m_instrumentID, "");
strcpy(m_exchangeID, "");
strcpy(m_productID, "");
//datasvrlog实现
int datasvrlog::log(const char *str)
std::string dir = tools::instance().conf().get("data.log", "");
std::string fname = "log.txt";
std::string filename = util::path_append(dir,fname);
std::ifstream ifs(filename);
if (ifs) //存在
std::ofstream fout(filename, std::ios::app);
fout << str;
fout.close();
else //不存在
std::ofstream fout(filename);
fout << str;
return 0;
//CSimpleHandler实现
CSimpleHandler::CSimpleHandler(CThostFtdcTraderApi *pUserApi,tools *tl)
this->tl = tl;
tl->m_pUserApi = pUserApi;
void CSimpleHandler::OnFrontConnected()
CThostFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.BrokerID, tl->m_chBrokerID);
strcpy(reqUserLogin.UserID, tl->m_chUserID);
strcpy(reqUserLogin.Password, tl->m_chUserPassword);
tl->m_pUserApi->ReqUserLogin(&reqUserLogin, 0);
void CSimpleHandler::OnFrontDisconnected(int nReason)
// 当发生这个情况后币安行情数据,API会自动重新连接,客户端可不做处理
tools::instance().logger().log("OnFrontDisconnected.");
void CSimpleHandler::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin,CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
tools::instance().logger().log("OnRspUserLogin:");
tools::instance().logger().log("ErrorCode=[%d], ErrorMsg=[%s]", pRspInfo->ErrorID,
pRspInfo->ErrorMsg);
tools::instance().logger().log("RequestID= [%d], Chain=[%d]", nRequestID, bIsLast);
if (pRspInfo->ErrorID != 0) {
// 端登失败币安行情数据,客户端需进行错误处理
tools::instance().logger().log("Trader failed to login, errorcode=%d errormsg=%s requestid=%d chain = %d", pRspInfo->ErrorID, pRspInfo->ErrorMsg, nRequestID, bIsLast);
/*exit(-1);*/
util::sleep_msec(5000);
tl->cnt++;
CThostFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.BrokerID, tl->m_chBrokerID);
strcpy(reqUserLogin.UserID, tl->m_chUserID);
strcpy(reqUserLogin.Password, tl->m_chUserPassword);
tl->m_pUserApi->ReqUserLogin(&reqUserLogin, 0);
tools::instance().logger().log("Trader登录失败后币安行情数据,第[%d]次登录!",tl->cnt);
//查询合约请求
else if (pRspInfo->ErrorID == 0)
CThostFtdcQryInstrumentField req;
strcpy(req.InstrumentID, tl->m_instrumentID);
strcpy(req.ExchangeInstID, tl->m_exchangeInstID);
strcpy(req.ExchangeID, tl->m_exchangeID);
strcpy(req.ProductID, tl->m_productID);
tl->m_pUserApi->ReqQryInstrument(&req, 0);
开发l2行情接口设计行情秒数据处理程序
评论