行情秒数据处理为分钟数据

1、obtmin.h文件
#ifndef ORDER_OBTMIN_H_
#define ORDER_OBTMIN_H_
#include <boost/shared_ptr.hpp>
#include<vector>
#include"acsy/util/dbconn.h"


namespace acsy
{
/********************************************************************************************************/
namespace monitor
{
/********************************************************************************************************/
class tools;
class obtmin
{
public:
obtmin(tools *tl);
int obtain();
std::shared_ptr<util::DBaseConn> connectFunction();
void process(std::shared_ptr<util::DBaseConn> sp3);
int obtmin::loginTrader();
int obtmin::loginMarketData();
int obtmin::logoutTrader();
int obtmin::logoutMarketData();
void obtmin::insMinData(std::shared_ptr<util::DBaseConn> sp1);
void obtmin::insFilPos(std::shared_ptr<util::DBaseConn> sp2);
void obtmin::step(std::shared_ptr<util::DBaseConn> sp5);
void obtmin::step1(std::shared_ptr<util::DBaseConn> sp);
void obtmin::step2(std::shared_ptr<util::DBaseConn> sp4);
public:
tools *tl;
std::vector<std::string> vecBuf;
// int cnt;
std::string time;
std::string op;
std::string cl;
double high;
double low;
char buf[1024];
std::string buft;
FILE *myf;
int f1;
double high1;
std::string time1;
std::string cont;
std::string filename;
std::string url;
std::string wjn;
std::string h;
std::string l;
std::string position;
};
}
}
#endif
2、obtmin.cpp文件
#include <iostream>
#include <fstream>
#include <iostream>
#include<string>
#include<boost/filesystem.hpp>
#include<boost/tokenizer.hpp>
#include<boost/typeof/typeof.hpp>
#include<boost/filesystem/path.hpp>
#include<regex>
#include<boost/regex/regex_traits.hpp>
#include "acsy/util/acsyapplication.h"
#include "acsy/monitor/combine.h"
#include"acsy/monitor/obtmin.h"
#include"acsy/util/dbconn.h"
#include <boost/shared_ptr.hpp>
#include<algorithm>
#include<vector>




#define SQL_DBO_DATA_MINUTE "insert into data_minute(trade_date,contract_code,trade_time,open_price,high_price,low_price,close_price) values(?,?,?,?,?,?,?)"
#define SQL_DBO_FILE_POSITION "insert into file_position (file_name,file_pos) values (?,?)"






namespace acsy
{
namespace monitor
{
obtmin::obtmin(tools *tl)
{
f1 = 0;
high = 0;
low = 0;
high1 = 0;
for (int i = 0; i < sizeof(buf); i++)
{
buf[i] = 0;
}
this->tl = tl;
}
int obtmin::obtain()
{
// std::string exist_dir = tl->instance().conf().get("data.wurl", "");
using namespace std;
namespace fs = boost::filesystem;
std::vector<int> vecDate;
std::vector<int> vecNew;
std::string rfloder = tl->instance().conf().get("data.url", "");//要读的文件夹.


fs::directory_iterator end_iter1;
int dir = 0;
int temp = 0;
for (fs::directory_iterator iter(rfloder); iter != end_iter1; ++iter)
{
if (fs::is_directory(iter->status()))
{
dir = std::atoi(iter->path().filename().string().c_str());
vecDate.push_back(dir);
}
}
sort(vecDate.begin(),vecDate.end());
int i1 = 0;
if (vecDate.size()>2)//取最近两天的文件夹
{
i1 = vecDate.size() - 2;

}
for (int i = i1; i < vecDate.size(); i++)
{
std::cout << vecDate[i] << std::endl;
temp = vecDate[i];
vecNew.push_back(temp);
}
auto connq = connectFunction();
// connq->execute("begin transaction");
for (int j = 0; j < vecNew.size(); j++)
{
std::cout << vecNew[j] << std::endl;
char tmp[10];
sprintf(tmp, "%d", vecNew[j]);
std::string  yeandto = util::path_append(rfloder, tmp);    //昨天和今天的数据文件夹
fs::recursive_directory_iterator end_iter;
for (fs::recursive_directory_iterator iter(yeandto); iter != end_iter; iter++)
{
if (fs::is_directory(*iter))
{
cout << *iter << endl;
//std::string *date;
//date = reinterpret_cast<std::string>(iter);
// vecDate.push_back(iter);
}
else
{
cout << *iter << endl;
cont = iter->path().string();
connq->execute("begin transaction");
step(connq);
connq->execute("commit");
}
}
}
// connq->execute("commit");
return 0;
}
void obtmin::step(std::shared_ptr<util::DBaseConn> sp5)
{
namespace fs = boost::filesystem;
fs::path ps(cont);
ps = ps.parent_path();//H:\data\20151027
url = ps.filename().string();//20151027


namespace fs = boost::filesystem;
bool hasDir = true;
fs::path ps1(cont);
std::string wjname = ps1.filename().string();      //a1511.csv  




std::string que = "select file_pos from file_position where file_name='" + cont + "'";
if (sp5 != nullptr)
{
util::ResultSet *rp = sp5->query(que.c_str());
if (rp != nullptr)
{
int count = 0;
std::string time1;
bool haspos = false;
std::string sql_filepos;
while (rp->next())
{
count++; //如果查询到位置.
haspos = true;
sql_filepos = rp->getStringValue(0);
}
rp->close();
if (haspos == true)
{
int posi = atoi(sql_filepos.c_str());
myf = fopen(cont.c_str(), "rb");
if (myf == nullptr)
{
std::cout << "File not exist!!" << std::endl;
return; //如果文件为空.
}
fseek(myf, posi, SEEK_SET);
process(sp5);
if (buft != "")
{
step2(sp5);
}
fclose(myf);
buft = "";
buf[0] = 0;
}
rp = nullptr;
if (count == 0)
{
step1(sp5);
}
}
}
}
void obtmin::step1(std::shared_ptr<util::DBaseConn> sp){
myf = fopen(cont.c_str(), "rb");
if (myf == nullptr)
{
std::cout << "File not exist!!" << std::endl;
return; //如果文件为空.
}
fseek(myf, 0, SEEK_SET);
process(sp);
fclose(myf);
char s1[30];
char s2[30];
sprintf(s1, "%lf", high);
sprintf(s2, "%lf", low);
h = s1;
l = s2;
namespace fs = boost::filesystem;
fs::path ps(cont);
wjn = ps.stem().string();


insMinData(sp);
char po[10];
sprintf(po, "%d", f1);
position = po;
insFilPos(sp);
}
void obtmin::step2(std::shared_ptr<util::DBaseConn> sp4)
{
char s1[30];
char s2[30];
sprintf(s1, "%lf", high);
sprintf(s2, "%lf", low);
h = s1;
l = s2;
namespace fs = boost::filesystem;
fs::path ps(cont);
wjn = ps.stem().string();
if (sp4 != nullptr)
{
int cnts = 0; //比较数据库中同一天同一分钟同一合约中最大 最小 收盘价
std::string qur = "select open_price,high_price,low_price,close_price from data_minute where contract_code='" + wjn + "'and trade_date='" + url + "'and trade_time='" + time + "'";
util::ResultSet *rp = sp4->query(qur.c_str());
if (rp != nullptr)
{
std::string opp;
std::string hip;
std::string lop;
std::string clp;
bool hasal = false;
while (rp->next())
{
cnts++;
hasal = true;
opp = rp->getStringValue(0);
hip = rp->getStringValue(1);
lop = rp->getStringValue(2);
clp = rp->getStringValue(3);
}
if (hasal == true)
{
int ihip = atoi(hip.c_str());
int ilop = atoi(lop.c_str());
if (ihip > high)
{
high = ihip;
}
if (ilop < low)
{
low = ilop;
}
op = opp;
}
rp->close();
rp = nullptr;
char ss1[30];
char ss2[30];
sprintf(ss1, "%lf", high);
sprintf(ss2, "%lf", low);
h = ss1;
l = ss2;
fs::path ps(cont);
wjn = ps.stem().string();
insMinData(sp4);
if (cnts == 0)
{
insMinData(sp4);
}
}
}


char po[10];
sprintf(po, "%d", f1);
position = po;
insFilPos(sp4);


}
void obtmin::process(std::shared_ptr<util::DBaseConn> sp3)
{
int cnt = 0;
while (!feof(myf))
{
cnt++;
fgets(buf, 1024, myf);
f1 = ftell(myf);
buft = buf;
using namespace std;
using namespace boost;
char_separator<char> sep(",");
tokenizer<char_separator<char>> tok(buft, sep);
vecBuf.clear();
for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)
{
// cout << *pos << "\n";
vecBuf.push_back(*pos);
}
if (buft.length() < 30)
{
break;
}
if (cnt > 1)
{
if (vecBuf.size() == 13)
{
time1 = vecBuf[1].substr(0, 5);
if (time == time1)
{
high1 = std::stof(vecBuf[2]);
if (high1 > high)
{
high = high1; //取最高价
}
if (high1 < low)
{
low = high1; //取最低价
}
}
if (time1 != time)
{
char s1[30];
char s2[30];
sprintf(s1, "%lf", high);
sprintf(s2, "%lf", low);
h = s1;
l = s2;
namespace fs = boost::filesystem;
fs::path ps(cont);
wjn = ps.stem().string();
insMinData(sp3);
cnt = 1;
}
cl = vecBuf[2];
}
}
if (cnt == 1)
{
op = vecBuf[2];
high = std::stof(vecBuf[2]);
low = std::stof(vecBuf[2]);
cl = vecBuf[2];
time = vecBuf[1].substr(0, 5);
}


}
}
std::shared_ptr<util::DBaseConn> obtmin::connectFunction()
{
std::shared_ptr<util::DBaseConn> db_conn(new util::SQLITE_conn);
std::string url = tl->instance().conf().get("data.filesqlurl", "");
db_conn->connect(const_cast<char*>(url.c_str()));
if (db_conn->connect(const_cast<char*>(url.c_str())) == 0)
return db_conn;
else
return nullptr;
}
void obtmin::insMinData(std::shared_ptr<util::DBaseConn> sp1)
{
// auto connS = connectFunction();
util::DbPrepare *ins1 = sp1->prepare(SQL_DBO_DATA_MINUTE);
if (time != ""&&op != ""&&cl != "")
{
ins1->bind(url.c_str());
ins1->bind(wjn.c_str());
ins1->bind(time.c_str());
ins1->bind(op.c_str());
ins1->bindDouble(std::stod(h));
ins1->bindDouble(std::stod(l));
ins1->bind(cl.c_str());
int i = ins1->execute();
if (0 != i)
{
std::cout << "i=[" << i << "] insert minute data error!!!" << sp1->getLastError() << std::endl;
}
ins1->close();
ins1 = nullptr;
}
}
void obtmin::insFilPos(std::shared_ptr<util::DBaseConn> sp2)
{
util::DbPrepare *inse1 = sp2->prepare(SQL_DBO_FILE_POSITION);
inse1->bind(cont.c_str());
inse1->bind(position.c_str());
if (0 != inse1->execute())
{
std::cout << "insert file position error!!!" << sp2->getLastError() << std::endl;
}
inse1->close();
inse1 = nullptr;
}


}
}
3、combine.h文件
#ifndef ORDER_COMBINE_H_
#define ORDER_COMBINE_H_
#include <mutex>
#include<boost/asio/spawn.hpp>
#include <boost/asio.hpp>
#include "ThostTraderApi/win32/ThostFtdcMdApi.h"
#include "ThostTraderApi/win32/ThostFtdcTraderApi.h"
#include<boost/asio/io_service.hpp>
#include"acsy/monitor/obtmin.h"
/********************************************************************************************************/
namespace acsy
{
/********************************************************************************************************/
namespace monitor
{
/********************************************************************************************************/
struct cond
{
std::mutex mutex;
bool value;
void setValue(bool v)
{
this->mutex.lock();
this->value = v;
this->mutex.unlock();
}
bool getValue()
{
return this->value;
}
void fun();
bool fun1();
};
/********************************************************************************************************/
class tools :public util::Application < tools > {
public:
TThostFtdcBrokerIDType m_chBrokerID;
TThostFtdcUserIDType m_chUserID;
TThostFtdcUserIDType m_chUserPassword;
TThostFtdcInstrumentIDType m_instrumentID;
TThostFtdcExchangeIDType m_exchangeID;
TThostFtdcExchangeInstIDType m_exchangeInstID;
TThostFtdcInstrumentIDType m_productID;
std::vector<std::string> m_vec;
cond m_login;
cond m_insok;
int cnt = 0;
int cnt1 = 0;
CThostFtdcMdApi *m_pUserApi1;
CThostFtdcTraderApi *m_pUserApi;
public:
tools();
int main();
int timer_loop(boost::asio::yield_context yield);
int on_timer(boost::asio::yield_context yield);
obtmin obm;
boost::asio::io_service svc;
std::string getappname(){ return "shujubaocun"; }
};
/********************************************************************************************************/
template < class T > class Deal{
public:
void setValue(const T &v);
T getValue() const;
public:
std::mutex m_mutex;
T m_value;
};
/********************************************************************************************************/
class datasvrlog :public util::Logger
{
public:
virtual int log(const char *str);
};
/********************************************************************************************************/
class CSimpleHandler : public CThostFtdcTraderSpi
{
public:
// 构造函数,需要 个有效的指向CThostFtdcMduserApi实例的指针 
CSimpleHandler(CThostFtdcTraderApi *pUserApi, tools *tl);
~CSimpleHandler() {}
virtual void OnFrontConnected();
virtual void OnFrontDisconnected(int nReason);
virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
public:
tools *tl;
};
/********************************************************************************************************/
class CSimpleHandler1 : public CThostFtdcMdSpi
{
public:
CSimpleHandler1(CThostFtdcMdApi *pUserApi1,tools *tl);
~CSimpleHandler1() {};
virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) override;
virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnFrontDisconnected(int nReason);
virtual void OnFrontConnected();
public:
tools *tl;
};
/********************************************************************************************************/
}
}
/********************************************************************************************************/
#endif
4、combine.cpp文件
#include"acsy/util/publictools.h"
#include <stdio.h> 
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
#include <string>
#include <boost/asio.hpp>
#include<boost/asio/spawn.hpp>
#include "acsy/util/acsyapplication.h"
#include "acsy/monitor/combine.h"
#include<boost/asio/io_service.hpp>
#include<boost/filesystem/path.hpp>




#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);
}
}
void CSimpleHandler::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
tools::instance().logger().log("OnRspError:");
tools::instance().logger().log("ErrorCode=[%d], ErrorMsg=[%s]", pRspInfo->ErrorID,
pRspInfo->ErrorMsg);
tools::instance().logger().log("RequestID= [%d], Chain=[%d]", nRequestID, bIsLast);
// 客户端需进行错误处理 
//{客户端的错误处理}
}
void CSimpleHandler::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
tools::instance().logger().log("合约请求响应!,InstrumentID:[%s]", pInstrument->InstrumentID);
std::cout << "InstrumentID=" << pInstrument->InstrumentID << std::endl;


tl->m_vec.push_back(pInstrument->InstrumentID);
if (bIsLast)
{
tl->m_insok.fun();   //m_insok加锁
if (tl->m_login.fun1()==true)   //m_login加锁
{
int len = tl->m_vec.size();
char** Instrument = new char*[len];
for (int i = 0; i < len; i++)
{
Instrument[i] = const_cast<char*>(tl->m_vec[i].c_str());
}
tl->m_pUserApi1->SubscribeMarketData(Instrument, len);
}
}
std::string ss = "\n";
std::string isd = pInstrument->InstrumentID + ss;
}
//CSimpleHandler1实现
CSimpleHandler1::CSimpleHandler1(CThostFtdcMdApi *pUserApi1,tools *tl)
{
this->tl = tl;
tl->m_pUserApi1 = pUserApi1;
}
void CSimpleHandler1::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
tools::instance().logger().log("OnRspError:");
tools::instance().logger().log("ErrorCode=[%d], ErrorMsg=[%s]", pRspInfo->ErrorID,
pRspInfo->ErrorMsg);
tools::instance().logger().log("RequestID=[%d], Chain=[%d]", nRequestID, bIsLast);
// 客户端需进行错误处理
//{客户端的错误处理}
}
void CSimpleHandler1::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)
{
// 输出报单录入结果
char stime[64];
acsy::util::BinDateTime bts_now = acsy::util::BinDateTime::now();
acsy::util::BinDateTime bts_now_local = bts_now + 8 * acsy::util::BinDateTime::MILLIS_PER_HOUR;
bts_now_local.toLongString(stime);
/*"InstrumentID=[%s],UpdateTime=[%s],UpdateMillisec=[%d],LastPrice=[%lf],BidPrice1=[%lf],AskPrice1=[%],LowestPrice=[%],HighestPrice=[%],Volume=[%],BidVolume1=[%],AskVolume1=[%],OpenInterest=[%],Turnover=[%]\n"*/
DEAL_TOO_MAX(pDepthMarketData->PreOpenInterest);
DEAL_TOO_MAX(pDepthMarketData->PreClosePrice);
DEAL_TOO_MAX(pDepthMarketData->PreSettlementPrice);
DEAL_TOO_MAX(pDepthMarketData->ClosePrice);
DEAL_TOO_MAX(pDepthMarketData->SettlementPrice);
DEAL_TOO_MAX(pDepthMarketData->OpenInterest);
DEAL_TOO_MAX(pDepthMarketData->LastPrice);
DEAL_TOO_MAX(pDepthMarketData->UpperLimitPrice);
DEAL_TOO_MAX(pDepthMarketData->LowerLimitPrice);
DEAL_TOO_MAX(pDepthMarketData->HighestPrice);
DEAL_TOO_MAX(pDepthMarketData->LowestPrice);
DEAL_TOO_MAX(pDepthMarketData->OpenPrice);
DEAL_TOO_MAX(pDepthMarketData->LowestPrice);
DEAL_TOO_MAX(pDepthMarketData->Turnover);
if (pDepthMarketData->AskPrice1 > 10 * pDepthMarketData->LastPrice)
{
pDepthMarketData->AskPrice1 = pDepthMarketData->LastPrice;
}
if (pDepthMarketData->BidPrice1 > 10 * pDepthMarketData->LastPrice)
{
pDepthMarketData->BidPrice1 = pDepthMarketData->LastPrice;
}


//printf("%s,%s.%03d,%.4f,%.4f,%.4f,%.4f,%.4f,%d,%d,%d,%s,%.2lf,%.2lf\n",
// pDepthMarketData->InstrumentID, pDepthMarketData->UpdateTime, pDepthMarketData->UpdateMillisec, pDepthMarketData->LastPrice,
// pDepthMarketData->BidPrice1, pDepthMarketData->AskPrice1, pDepthMarketData->LowestPrice, pDepthMarketData->HighestPrice,
// pDepthMarketData->Volume, pDepthMarketData->BidVolume1, pDepthMarketData->AskVolume1, stime,
// pDepthMarketData->OpenInterest, pDepthMarketData->Turnover);
std::string subs = acsy::util::strprintf("%s,%s.%03d,%.4f,%.4f,%.4f,%.4f,%.4f,%d,%d,%d,%s,%.2lf,%.2lf\n",
pDepthMarketData->InstrumentID, pDepthMarketData->UpdateTime, pDepthMarketData->UpdateMillisec, pDepthMarketData->LastPrice,
pDepthMarketData->BidPrice1, pDepthMarketData->AskPrice1, pDepthMarketData->LowestPrice, pDepthMarketData->HighestPrice,
pDepthMarketData->Volume, pDepthMarketData->BidVolume1, pDepthMarketData->AskVolume1, stime,
pDepthMarketData->OpenInterest, pDepthMarketData->Turnover);
//处理分钟数据.
/* std::vector<int> vecMnt;
std::string time = pDepthMarketData->UpdateTime;
std::string sec = time.substr(3, 2);
double op = pDepthMarketData->OpenPrice;
double cl = pDepthMarketData->ClosePrice;
*/
namespace fs = boost::filesystem;
/* char stime[30] = "2015-10-23 14:07:03.579";*/
std::string date;
date = pDepthMarketData->TradingDay;
if (date == "")
{
return;
}
std::string dir = tools::instance().conf().get("data.url","");
std::string floder = util::path_append(dir, date); 
bool result = boost::filesystem::is_directory(floder);
if (result==false)
{
try{ fs::create_directories(floder); }
catch (std::exception& e)
{
std::cout << "exception!!!!["<<e.what()<<"]"<< std::endl;
}
}



std::string filename = pDepthMarketData->InstrumentID;
std::string filename1 = floder + "\\" + filename + ".csv";
std::ifstream ifs(filename1);


if (ifs)   //存在
{
std::ofstream fout(filename1, std::ios::app);
fout << subs;
fout.close();
}
else     //不存在
{
std::ofstream fout(filename1);
fout << subs;
}
}
void CSimpleHandler1::OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
tools::instance().logger().log("行情请求响应!,InstrumentID:[%s]", pSpecificInstrument->InstrumentID);
}
void CSimpleHandler1::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (bIsLast)
{
tl->m_login.fun();  //m_login加锁




if (tl->m_insok.fun1()==true)   //m_insok加锁
{
int len = tl->m_vec.size();
char** Instrument = new char*[len];
for (int i = 0; i < len; i++)
{
Instrument[i] = const_cast<char*>(tl->m_vec[i].c_str());
}
tl->m_pUserApi1->SubscribeMarketData(Instrument, len);
}
}
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("MD failed to login, errorcode=%d errormsg=%s requestid=%d chain = %d", pRspInfo->ErrorID, pRspInfo->ErrorMsg, nRequestID, bIsLast);
/*exit(-1);*/
util::sleep_msec(5000);
tl->cnt1++;
CThostFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.BrokerID, tl->m_chBrokerID);
strcpy(reqUserLogin.UserID, tl->m_chUserID);
strcpy(reqUserLogin.Password, tl->m_chUserPassword);
tl->m_pUserApi1->ReqUserLogin(&reqUserLogin, 0);
tools::instance().logger().log("MD登录失败后,第[%d]次登录!", tl->cnt1);
}
}
void CSimpleHandler1::OnFrontDisconnected(int nReason)
{
// 当发生这个情况后,API会自动重新连接,客户端可不做处理
tools::instance().logger().log("OnFrontDisconnected.");
}
void CSimpleHandler1::OnFrontConnected()
{
CThostFtdcReqUserLoginField reqUserLogin;
strcpy(reqUserLogin.BrokerID, tl->m_chBrokerID);
strcpy(reqUserLogin.UserID, tl->m_chUserID);
strcpy(reqUserLogin.Password, tl->m_chUserPassword);
tl->m_pUserApi1->ReqUserLogin(&reqUserLogin, 0);


}
void cond::fun()
{
this->mutex.lock();
this->value = true;
this->mutex.unlock();
}
bool cond::fun1()
{
bool b;
this->mutex.lock();
b = this->value;
this->mutex.unlock();
return b;
}
}
}
ACSY_DEF_MAINCLASS(acsy::monitor::tools);




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值