-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamOfTrades.cpp
335 lines (273 loc) · 7.72 KB
/
StreamOfTrades.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Author: Gleb Karapish
This file contains implementation of the trading metrics computations along with tests and CSV file I/O.
*/
#include <iostream>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
// Describes Trade data structure
typedef unsigned long long int VERYLONG;
class Trade
{
public:
Trade() :
Trade( 0, "", 0, 0 )
{ }
Trade( VERYLONG timestamp, std::string symbol, VERYLONG quantity, unsigned int price )
{
SetPrice( price );
SetQuantity( quantity );
SetSymbol( symbol );
SetTimestamp( timestamp );
}
const std::string& GetSymbol() const
{ return m_symbol; }
VERYLONG GetTimestamp() const
{ return m_timestamp; }
VERYLONG GetQuantity() const
{ return m_quantity; }
unsigned int GetPrice() const
{ return m_price; }
void SetSymbol( std::string symbol )
{ m_symbol = symbol; }
void SetTimestamp( VERYLONG timestamp )
{ m_timestamp = timestamp; }
void SetQuantity( VERYLONG quantity )
{ m_quantity = quantity; }
void SetPrice( unsigned int price )
{ m_price = price; }
private:
VERYLONG m_timestamp;
std::string m_symbol;
VERYLONG m_quantity;
unsigned int m_price;
};
// Implements computation of the metrics for a symbol
class SymbolMetrics
{
public:
SymbolMetrics( VERYLONG timegap, VERYLONG volume, VERYLONG totalVolumeValue, unsigned int maxPrice ) :
m_lastTimeOccurence { 0 },
m_maxTimeGap { timegap },
m_totalVolume { volume },
m_maxPrice { maxPrice },
m_totalVolumeValue { totalVolumeValue },
m_totalTrades { 0 }
{ }
SymbolMetrics() :
SymbolMetrics( 0, 0, 0, 0 )
{ }
// First time this trading symbol occurs
SymbolMetrics( const Trade& trade ) :
SymbolMetrics()
{
m_lastTimeOccurence = trade.GetTimestamp();
Intake( trade );
}
// TODO: Implement comparison operators
//bool operator==( const Statistics& rhs )
//{
// if( &rhs == this )
// return true;
//
//}
void Intake( const Trade& trade )
{
// 1. Compute maximum time gap
auto gapToConsider = trade.GetTimestamp() - m_lastTimeOccurence;
if( gapToConsider > m_maxTimeGap )
{
m_maxTimeGap = gapToConsider;
m_lastTimeOccurence = trade.GetTimestamp();
}
// 2. Total volume traded (sum of the quantity for all trades in a symbol)
m_totalVolume += trade.GetQuantity();
// 3. Max trade price
if( trade.GetPrice() > m_maxPrice )
m_maxPrice = trade.GetPrice();
// 4. Weighted average price
// Divide total value on total volume
m_totalVolumeValue += trade.GetPrice() * trade.GetQuantity();
// 5. Compute number of trades
m_totalTrades++;
}
VERYLONG GetAveragePrice() const
{ return m_totalVolume == 0 ? 0 : m_totalVolumeValue / m_totalVolume; }
private:
VERYLONG m_lastTimeOccurence;
VERYLONG m_maxTimeGap;
VERYLONG m_totalVolume;
VERYLONG m_totalVolumeValue;
unsigned int m_maxPrice;
VERYLONG m_totalTrades;
friend class Aggregator;
};
// Aggregates metrics for all symbols
typedef std::map< std::string, SymbolMetrics > METRICS;
class Aggregator
{
public:
Aggregator()
{
Reset();
}
void Reset()
{
m_metrics.clear();
}
void Intake( const std::vector< Trade >& trades )
{
for( const auto& trade : trades )
Intake( trade );
}
void Intake( const Trade& trade )
{
try
{
SymbolMetrics& stat = m_metrics.at( trade.GetSymbol() );
stat.Intake( trade );
}
catch ( std::out_of_range& )
{
// adding a new symbol to the storage
m_metrics[ trade.GetSymbol() ] = SymbolMetrics( trade );
}
}
const METRICS& GetUnderlyingMetrics() const
{
return m_metrics;
}
std::ostringstream GetUnderlyingMericsAsText() const
{
std::ostringstream result;
for( const auto& item : m_metrics )
{
result << item.first << "," << item.second.m_maxTimeGap << "," << item.second.m_totalVolume << ","
<< item.second.GetAveragePrice() << "," << item.second.m_maxPrice << "," << item.second.m_totalTrades << std::endl;
}
return result;
}
private:
METRICS m_metrics;
};
#pragma region Helper printing functions
static void PrintToConsole( const Aggregator& aggregator )
{
std::cout << aggregator.GetUnderlyingMericsAsText().str();
}
static void PrintToCSV( const Aggregator& aggregator, const std::string& filename )
{
std::ofstream file;
file.open( filename );
file << aggregator.GetUnderlyingMericsAsText().str();
file.close();
}
#pragma endregion
#pragma region Tests
static void RunTest( Aggregator& aggregator, const std::vector< Trade >& datafeed, const METRICS& result )
{
std::cout << "Running test" << std::endl;
aggregator.Intake( datafeed );
PrintToConsole( aggregator );
// TODO: Implement strict comparison
aggregator.Reset();
std::cout << std::endl << std::endl;
}
// Input:
// <symbol>,<MaxTimeGap>,<Volume>,<WeightedAveragePrice>,<MaxPrice>
// Output:
// <TimeStamp>, <Symbol>, <Quantity>, <Price>
static void RunTest1( Aggregator& aggregator )
{
auto test = {
Trade( 52924702, "aaa", 13, 1136 ),
Trade( 52924702, "aac", 20, 477 ),
Trade( 52925641, "aab", 31, 907 ),
Trade( 52927350, "aab", 29, 724 ),
Trade( 52927783, "aac", 21, 638 ),
Trade( 52930489, "aaa", 18, 1222 ),
Trade( 52931654, "aaa", 9, 1077 ),
Trade( 52933453, "aab", 9, 756 )
};
METRICS expected = {
std::make_pair( "aaa", SymbolMetrics( 5787, 40, 1161, 1222 ) ),
std::make_pair( "aab", SymbolMetrics( 6103, 69, 810, 907 ) ),
std::make_pair( "aac", SymbolMetrics( 3081, 41, 559, 638 ) )
};
RunTest( aggregator, test, expected );
}
void RunTest2( Aggregator& aggregator )
{
auto test = {
Trade( 1, "aaa", 1, 100 ),
Trade( 2, "bbb", 2, 100 ),
Trade( 3, "ccc", 3, 100 ),
Trade( 4, "ccc", 3, 100 )
};
METRICS expected = {
std::make_pair( "aaa", SymbolMetrics( 0, 1, 1, 100 ) ),
std::make_pair( "bbb", SymbolMetrics( 0, 2, 1, 100 ) ),
std::make_pair( "ccc", SymbolMetrics( 1, 6, 100, 100 ) )
};
RunTest( aggregator, test, expected );
}
// Test order when printing out
void RunTest3( Aggregator& aggregator )
{
auto test = {
Trade( 1, "bbb", 1, 100 ),
Trade( 2, "aaa", 1, 100 ),
};
METRICS expected = {
std::make_pair( "aaa", SymbolMetrics( 0, 1, 100, 100 ) ),
std::make_pair( "bbb", SymbolMetrics( 0, 1, 100, 100 ) )
};
RunTest( aggregator, test, expected );
}
#pragma endregion
// Real datafeed
void ProcessCSV( Aggregator& aggregator )
{
std::ifstream datafeed( "C:\\Users\\GLEBKA\\Desktop\\input.csv" );
std::string line;
while( std::getline( datafeed, line ) )
{
std::stringstream lineStream( line );
std::string cell;
int position = 0;
Trade trade;
while( std::getline( lineStream, cell, ',' ) )
{
switch( position )
{
case 0:
trade.SetTimestamp( std::stoll( cell ) );
break;
case 1:
trade.SetSymbol( cell );
break;
case 2:
trade.SetQuantity( std::stoll( cell ) );
break;
case 3:
trade.SetPrice( std::stol( cell ) );
break;
}
position++;
}
aggregator.Intake( trade );
}
PrintToConsole( aggregator );
PrintToCSV( aggregator, "C:\\Users\\GLEBKA\\Desktop\\output.csv" );
}
void main()
{
Aggregator aggregator;
RunTest1( aggregator );
RunTest2( aggregator );
RunTest3( aggregator );
ProcessCSV( aggregator );
}