-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStats.cpp
More file actions
306 lines (292 loc) · 9 KB
/
Copy pathStats.cpp
File metadata and controls
306 lines (292 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "Market.h"
#include "OrderBook.h"
#include "Order.h"
#include "Stats.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
Stats::Stats(OrderBook * myOrderBook)
{
m_OrderBook = myOrderBook ;
}
Stats::~Stats()
{
}
std::vector<double> Stats::getPriceTimeSeries(TimeSeriesType dataType, double samplePeriod)
{
std::vector<double> timeSeries ;
// get order book history
const std::vector<OrderBookHistory> fullHistory = m_OrderBook->getOrderBookHistory() ;
std::vector<OrderBookHistory>::const_iterator it = fullHistory.begin() ;
// parse history and build time series
int timeIndex = 0 ;
double timeStart = it->m_timeStamp ;
while(it != fullHistory.end())
{
double l_ts = it->m_timeStamp;
if(l_ts>=timeStart+timeIndex*samplePeriod)
{
// if consecutive identical ts, get to the last
double next_ts = l_ts ;
while(l_ts==next_ts && it != fullHistory.end()-1){
next_ts = (it+1)->m_timeStamp ;
it++ ;
}
if(l_ts!=next_ts) it-- ;
// now get the requested value
double valueToBeAdded ;
switch(dataType)
{
case ASK:
valueToBeAdded = it->m_limitOrders[0].m_ask/100.0 ;
break ;
case BID:
valueToBeAdded = it->m_limitOrders[0].m_bid/100.0 ;
break ;
case MID:
valueToBeAdded = 0.5*(it->m_limitOrders[0].m_ask/100.0
+ it->m_limitOrders[0].m_bid/100.0) ;
break ;
case LAST:
valueToBeAdded = it->m_last ;
break ;
case SPREAD:
valueToBeAdded = it->m_limitOrders[0].m_ask/100.0
- it->m_limitOrders[0].m_bid/100.0 ;
break ;
default:
std::cout << "Unknown TimeSeriesType requested in Stats::getPriceTimeSeries." << std::endl ;
exit(1) ;
}
timeSeries.push_back(valueToBeAdded) ;
timeIndex++ ;
}
it++ ;
}
return timeSeries ;
}
void Stats::printTimeSeries(TimeSeriesType dataType, double samplePeriod)
{
// get timeseries
std::vector<double> timeSeries = getPriceTimeSeries(dataType, samplePeriod);
// prepare outputFile
std::ostringstream ss_fileName ;
ss_fileName << "./res/" << m_OrderBook->getLinkToMarket()->getName() << "/" ;
switch(dataType)
{
case ASK:
ss_fileName << "Ask_" ;
break ;
case BID:
ss_fileName << "Bid_" ;
break ;
case MID:
ss_fileName << "Mid_" ;
break ;
case LAST:
ss_fileName << "Last_" ;
break ;
default:
std::cout << "Unknown TimeSeriesType requested in Stats::printTimeSeries." << std::endl ;
exit(1) ;
}
ss_fileName << samplePeriod << "_sampledTimeSeries.dat" ;
// Print time series
std::fstream outputFile(ss_fileName.str().c_str()) ;
for(unsigned int idx=0; idx<timeSeries.size(); idx++){
outputFile<<timeSeries[idx]<<"\n";
}
outputFile.close() ;
}
//void Stats::plotPDF(const std::string dataName, const std::vector<double> data, int nBins)
//{
// long nElements = data.size() ;
// double * dataArray = (double*)malloc(nElements*sizeof(double));
// for(int k=0;k<nElements;k++){
// dataArray[k] = data[k] ;
// }
// // Estimate number of bins
// if(nBins==-1){
// nBins = 20; //nElements/100 ;
// }
// // Allocate memory
// gsl_histogram * dataHistogram = gsl_histogram_alloc (nBins) ;
// gsl_histogram_set_ranges_uniform (dataHistogram,
// gsl_stats_min(dataArray, 1, nElements),
// gsl_stats_max(dataArray, 1, nElements)
// ) ;
// // Build histogram
// for(int k=0;k<nElements;k++){
// gsl_histogram_increment (dataHistogram, dataArray[k]) ;
// }
// // Normalize as a pdf
// gsl_histogram_pdf * pdfHist = gsl_histogram_pdf_alloc (nBins) ;
// gsl_histogram_pdf_init (pdfHist, dataHistogram) ;
// // Print
// FILE * outFile;
// std::ostringstream outFilename ;
// outFilename << "./res/" << m_OrderBook->getLinkToMarket()->getName() << "/" << dataName << "_PDF.csv" ;
// outFile=fopen(outFilename.str().c_str(),"w");
// for(int i=0;i<nBins;i++)
// {
// fprintf(outFile, "%f\t%f\n", 0.5*(pdfHist->range[i]+pdfHist->range[i+1]),
// pdfHist->sum[i+1]-pdfHist->sum[i]);
// }
// fclose(outFile);
//}
//
//void Stats::plotNormalizedPDF(const std::string dataName, const std::vector<double> data, int nBins)
//{
// // Normalize elements (center and reduce)
// long nElements = data.size() ;
// double * dataArray = (double*)malloc(nElements*sizeof(double));
// for(int k=0;k<nElements;k++){
// dataArray[k] = data[k] ;
// }
// double m1 = gsl_stats_mean (dataArray, 1, nElements) ;
// double sd = gsl_stats_sd (dataArray, 1, nElements) ;
// std::cout << dataName << "\t" << m1 << "\t" << sd << std::endl ;
// std::vector<double> normalizedData ;
// for(int k=0;k<nElements;k++){
// normalizedData.push_back((data[k]-m1)/sd) ;
// }
// // Allocate memory
// nBins = 24 ;
// double lowerBound = -6 ;
// double upperBound = 6 ;
// gsl_histogram * dataHistogram = gsl_histogram_alloc (nBins) ;
// gsl_histogram_set_ranges_uniform (dataHistogram,
// lowerBound,
// upperBound
// ) ;
// // Build histogram
// for(int k=0;k<nElements;k++){
// gsl_histogram_increment (dataHistogram, normalizedData[k]) ;
// }
// // Normalize as a pdf
// gsl_histogram_pdf * pdfHist = gsl_histogram_pdf_alloc (nBins) ;
// gsl_histogram_pdf_init (pdfHist, dataHistogram) ;
// // Print
// FILE * outFile;
// std::ostringstream outFilename ;
// outFilename << "./res/" << m_OrderBook->getLinkToMarket()->getName() << "/" << dataName << "_NormalizedPDF.csv" ;
// outFile=fopen(outFilename.str().c_str(),"w");
// for(int i=0;i<nBins;i++)
// {
// fprintf(outFile, "%f\t%f\t%f\t%f\n", pdfHist->range[i], pdfHist->range[i+1],
// 0.5*(pdfHist->range[i]+pdfHist->range[i+1]),
// pdfHist->sum[i+1]-pdfHist->sum[i]);
// }
// fclose(outFile);
//}
//
std::vector<double> Stats::getOrderSignsTimeSeries(TimeSeriesType dataType)
{
std::vector<double> timeSeries ;
// get order book history
std::vector<Order> fullOrderHistory = m_OrderBook->getOrderHistory() ;
std::vector<Order>::iterator it = fullOrderHistory.begin() ;
// parse history and build time series
while(it != fullOrderHistory.end())
{
// Is this order to be considered ?
bool toBeConsidered = false ;
switch(dataType)
{
case MARKET:
if(it->getType()==MARKET_SELL || it->getType()==MARKET_BUY) toBeConsidered = true ;
break ;
case LIMIT:
if(it->getType()==LIMIT_SELL || it->getType()==LIMIT_BUY) toBeConsidered = true ;
break ;
case ALL:
toBeConsidered = true ;
break ;
default:
std::cout << "Unknown TimeSeriesType requested in Stats::getOrderSignsTimeSeries." << std::endl ;
exit(1) ;
}
// get the requested value
if(toBeConsidered)
{
int valueToBeAdded ;
if(it->getType()==MARKET_SELL || it->getType()==LIMIT_SELL) valueToBeAdded = -1 ;
if(it->getType()==MARKET_BUY || it->getType()==LIMIT_BUY) valueToBeAdded = 1 ;
timeSeries.push_back(valueToBeAdded) ;
}
it++ ;
}
return timeSeries ;
}
std::map<int,double> Stats::getAutocorrelation(std::vector<double> data, int lagMax, int deltaLag)
{
std::map<int,double> result ;
int lag = 0 ;
double norm = 1.0 ;
while (lag<=lagMax) {
double res = 0.0 ;
for(unsigned int i=lag; i<data.size(); i++)
{
res += 1.0*(data[i] * data[i-lag])/(data.size()-lag) ;
}
result[lag] = res;
if(lag==0) norm = res ;
result[lag] /= norm ;
lag += deltaLag ;
}
return result ;
}
void Stats::printAutocorrelation(std::string tag, std::vector<double> data, int lagMax, int deltaLag)
{
// get time series
std::map<int,double> autocorrelations = getAutocorrelation(data, lagMax, deltaLag) ;
// prepare outputFile
std::ostringstream ss_fileName ;
ss_fileName << "./res/" << m_OrderBook->getLinkToMarket()->getName() << "/Autocorrelation_" << tag << ".csv" ;
// Print time series
std::fstream outputFile(ss_fileName.str().c_str()) ;
std::map<int,double>::iterator it = autocorrelations.begin() ;
while(it!=autocorrelations.end()){
outputFile<<(it->first, "%d")<<'\t'<<(it->second,"%e")<<'\n' ;
it++ ;
}
outputFile.close() ;
}
void Stats::printSummary()
{
// prepare outputFile
std::ostringstream ss_fileName ;
ss_fileName << "./res/" << m_OrderBook->getLinkToMarket()->getName() << "/Summary.csv" ;
std::fstream outputFile(ss_fileName.str().c_str()) ;
// get order history
std::vector<Order> orderHistory = m_OrderBook->getOrderHistory() ;
int nBuyLimitOrders = 0 ;
int nSellLimitOrders = 0 ;
int nBuyMarketOrders = 0 ;
int nSellMarketOrders = 0 ;
int totalVolumeExchanged = 0 ;
std::vector<Order>::iterator iter = orderHistory.begin() ;
while(iter!=orderHistory.end())
{
// count orders by type
if(iter->getType()==MARKET_BUY) nBuyMarketOrders++ ;
if(iter->getType()==MARKET_SELL) nSellMarketOrders++ ;
if(iter->getType()==LIMIT_BUY) nBuyLimitOrders++ ;
if(iter->getType()==LIMIT_SELL) nSellLimitOrders++ ;
// count volume exchanged
if(iter->getType()==MARKET_BUY || iter->getType()==MARKET_SELL)
{
totalVolumeExchanged += iter->getInitialVolume() ;
}
// next order
iter++;
}
// write output
outputFile << "nBuyLimitOrders\t" << nBuyLimitOrders << '\n';
outputFile<< "nSellLimitOrders\t"<< nSellLimitOrders <<'\n';
outputFile<< "nBuyMarketOrders\t"<< nBuyMarketOrders <<'\n';
outputFile<< "nSellMarketOrders\t"<< nSellMarketOrders <<'\n';
outputFile<< "totalVolumeExchanged\t"<< totalVolumeExchanged <<'\n';
// close output file
outputFile.close() ;
}