I have to merge two time series. I have used Boost Time_duration from POSIX Library. However I was unable to find any function to merge the two time series. I have made a struct of two database. The below represents what I am trying to acomplish.
Task:
Merge trade file and nbbo file to one time-series file and size of memory is only 1 gb. The file size of both is 18 GB i.e. Trade file is 2 gb and 16 gb Nbbo file.
Requirement:
- For output file, only include price/volume/time/symbol fields for trade, ask_price/ask_size/bid_price/bid_size/time/symbol fields for nbbo quote.
- Make the output file as small as possible and can be played back fast.
- C++
- Test code
The format from downloaded website for tradefile is:
093000030NA O 00002420800000405400N000000000000003230C
Example:
trade file
MSFT 100 57.2 13:00
MSFT 100 58.2 13:10
GOOG 200 300.2 13:01
GOOG 300 300.2 14:30
nbbo file
GOOG 100 300.2 100 300.3 13:03
GOOG 100 300.3 100 300.4 13:04
MSFT 100 57.2 100 57.3 12:09
MSFT 100 57.3 100 58.4 13:05
⇒
output file
MSFT 100 57.2 100 57.3 12:09
MSFT 100 57.2 13:00
GOOG 200 300.2 13:01
GOOG 100 300.2 100 300.3 13:03
GOOG 100 300.3 100 300.4 13:04
MSFT 100 57.3 100 58.4 13:05
MSFT 100 58.2 13:10
GOOG 300 300.2 14:30
I have tried a lot since I am new to c++ and came up with the below code:
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iterator>
#include <cassert>
#include <boost/date_time/posix_time/posix_time.hpp> //include all types plus i/o
#include <boost/format.hpp>
using namespace std;
using namespace boost::posix_time;
struct RawTrade
{
char tm[9];
char venue;
char symbol[16];
char cond[4];
char qty[9];
char prc[11];
char filler0;
char corr[2];
char filler1[18];
char rchar;
char nchar;
};
struct RawNBBO
{
char tm[9];
char venue;
char symbol[16];
char filler0[65];
char bid[11]; // bid price
char bsize[7]; // bid size
char filler1[8];
char ask[11]; // ask price
char asize[7]; // ask size
char filler2[7];
char filler3[2]; // since 2013/02/05
char rchar;
char nchar;
};
int main()
{
//std::string ts("23:59:59.000");
//time_duration td(duration_from_string(ts));
std::vector<std::string> Tradetime;
std::vector<std::string> Nbbotime;
std::vector<std::string> SymbolTrade; //Vector for parsing to Boost for Trade data
std::vector<std::string> SymbolNbbo; //Vector for parsing to Boost for Nbbo data
std::vector<std::string> QtyTrade; //Vector for getting Qty Trade data
std::vector<std::string> BidQtyNbbo; //Vector for getting Bid Qty Nbbo data
std::vector<std::string> AskQtyNbbo; //Vector for getting Ask Qty Nbbo data
std::vector<std::string> PriceTrade; //Vector for getting Price Trade data
std::vector<std::string> BidPriceNbbo; //Vector for getting Price Nbbo data
std::vector<std::string> AskPriceNbbo; //Vector for getting Price Nbbo data
string line;
string ForTradetime;
string ForNbbotime;
string ForSymbolTrade;
string ForSymbolNbbo;
string ForQtyTrade;
string ForBidQtyNbbo;
string ForAskQtyNbbo;
string ForPriceTrade;
string ForBidPriceNbbo;
string ForAskPriceNbbo;
std::vector<std::string> TradeArray;
std::vector<std::string> NbboArray;
std::vector<std::string> myvalue;
fstream fin;
std::ifstream myfile("C:\Test\taqtrade1000.txt");
std::ifstream qfile("C:\Test\taqnbbo1000.txt");
if(!myfile) //Always test the file open.
{
std::cout<<"Error opening output file"<< std::endl;
system("pause");
return -1;
}
while(std::getline(myfile, line))
{
TradeArray.push_back(line);
}
//Putting Time in Vector
for(int i=1; i < TradeArray.size(); i++)
{
ForTradetime=TradeArray[i].substr(0,2)+":"+TradeArray[i].substr(2,2)+":"+TradeArray[i].substr(4,2)+"."+TradeArray[i].substr(6,3);
Tradetime.push_back(ForTradetime);
}
//Putting symbol is Vector
for(int i=1; i < TradeArray.size(); i++)
{
ForSymbolTrade=TradeArray[i].substr(10,16);
SymbolTrade.push_back(ForSymbolTrade);
}
cout<<SymbolTrade[0]<<endl;
//Putting qty is Vector
for(int i=1; i < TradeArray.size(); i++)
{
ForQtyTrade=TradeArray[i].substr(30,9);
QtyTrade.push_back(ForQtyTrade);
}
cout<<QtyTrade[6]<<endl;
//Putting Price is Vector
for(int i=1; i < TradeArray.size(); i++)
{
ForPriceTrade=TradeArray[i].substr(39,7)+"."+TradeArray[i].substr(43,4);
PriceTrade.push_back(ForPriceTrade);
}
cout<<PriceTrade[6]<<endl;
if(!qfile) //Always test the file open.
{
std::cout<<"Error opening output file"<<std::endl;
system("pause");
return -1;
}
while(std::getline(qfile, line))
{
NbboArray.push_back(line);
}
for(int i=1; i < NbboArray.size(); i++)
{
ForNbbotime=NbboArray[i].substr(0,2)+":"+NbboArray[i].substr(2,2)+":"+NbboArray[i].substr(4,2)+"."+NbboArray[i].substr(6,3);
Nbbotime.push_back(ForNbbotime);
}
/***********************************Doing same Exercise for Nbbo*************************/
//Putting Symbol in Vector for Nbbo
for(int i=1; i < NbboArray.size(); i++)
{
ForSymbolNbbo=NbboArray[i].substr(10,16);
SymbolNbbo.push_back(ForSymbolNbbo);
}
cout<<SymbolNbbo[5]<<endl;
//Putting Bid qty is Vector
for(int i=1; i < NbboArray.size(); i++)
{
ForBidQtyNbbo=NbboArray[i].substr(37,7);
BidQtyNbbo.push_back(ForBidQtyNbbo);
}
cout<<BidQtyNbbo[6]<<endl;
//Putting Bid Price is Vector
for(int i=1; i < NbboArray.size(); i++)
{
ForBidPriceNbbo=NbboArray[i].substr(26,7)+"."+NbboArray[i].substr(33,4);;
BidPriceNbbo.push_back(ForBidPriceNbbo);
}
cout<<BidPriceNbbo[6]<<endl;
//Putting Ask qty is Vector
for(int i=1; i < NbboArray.size(); i++)
{
ForAskQtyNbbo=NbboArray[i].substr(55,7);
AskQtyNbbo.push_back(ForAskQtyNbbo);
}
cout<<AskQtyNbbo[6]<<endl;
//Putting Bid Price is Vector
for(int i=1; i < NbboArray.size(); i++)
{
ForAskPriceNbbo=NbboArray[i].substr(44,7)+"."+NbboArray[i].substr(52,4);;
AskPriceNbbo.push_back(ForAskPriceNbbo);
}
cout<<AskPriceNbbo[6]<<endl;
//Checking for sorting and putting in vector
ofstream nbbofile("C:\Test\nbbofinal.txt",std::ios_base::app);
for(int i=0; i < Nbbotime.size(); i++)
{
time_duration td=duration_from_string(Tradetime[i]);
time_duration ts=duration_from_string(Nbbotime[i]);
nbbofile<<SymbolNbbo[i]<<"$"<<BidQtyNbbo[i]<<"$"<<BidPriceNbbo[i]<<"$"<<AskQtyNbbo[i]<<"$"<<AskPriceNbbo[i]<<"$"<<ts<<endl;
for(int i=0; i < Tradetime.size(); i++)
{
if(td<ts)
{
nbbofile<<SymbolTrade[i]<<"$"<<QtyTrade[i]<<"$"<<PriceTrade[i]<<"$"<<td<<endl;
}
}
}
nbbofile.close();
//Merge the two Time Series
//td.intersects(ts);
//std::cout<<QueryArray[200]<<std::endl;
//std::cout<<"n"<<"n"<<DataArray[200]<<std::endl;
system("pause");
}
//thoughts read the trade file first and when reading nbbo file just write to text file by comparing time.
Aucun commentaire:
Enregistrer un commentaire