-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrade.cpp
More file actions
168 lines (151 loc) · 5.55 KB
/
trade.cpp
File metadata and controls
168 lines (151 loc) · 5.55 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
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <stdexcept>
namespace streamingtrade {
class Statistics {
public:
Statistics(const uint64_t &ts, const int &qty, const int &price) :
lastTimestamp_(ts),
maxTradePrice_(price),
totalVolume_(qty)
{weightedAvgPrice_ = totalVolume_ * maxTradePrice_;}
Statistics(const Statistics &other) = delete;
Statistics& operator=(Statistics other) = delete;
uint64_t lastTimestamp_ {0};
uint64_t maxTimeGap_ {0};
int maxTradePrice_ {0};
int totalVolume_ {0};
uint64_t weightedAvgPrice_ {0};
};
class TradeEntry {
public:
TradeEntry(uint64_t ts, std::string &&sym, int qty, int price) :
timestamp_(ts), symbol_(std::move(sym)),
price_(price), quantity_(qty) {}
TradeEntry(const TradeEntry &other) = delete;
TradeEntry& operator=(TradeEntry other) = delete;
uint64_t timestamp_;
std::string symbol_;
int price_;
int quantity_;
};
template<typename key, typename val>
class Storage {
public:
std::unordered_map<key, val> hashmap_;
inline void clear() {hashmap_.clear();}
};
template<typename derived, typename tradeentry>
class TradeEntryBase {
public:
void process(const tradeentry &entry) {
return static_cast<derived*>(this)->addTradeEntry(entry);
}
std::vector<std::string> cleanup() {
return static_cast<derived*>(this)->cleanup();
}
protected:
TradeEntryBase() {}
};
class StatisticsCalculator: public TradeEntryBase<StatisticsCalculator, TradeEntry> {
public:
StatisticsCalculator(Storage<std::string, std::unique_ptr<Statistics>> &storage) : storage_(storage) {}
void addTradeEntry(const TradeEntry &entry) const {
uint64_t ts = entry.timestamp_;
std::string sym = entry.symbol_;
int qty = entry.quantity_;
int price = entry.price_;
if(storage_.hashmap_.count(sym) == 0) {
storage_.hashmap_[sym] = std::make_unique<Statistics>(ts, qty, price);
return;
}
storage_.hashmap_[sym]->totalVolume_ += qty;
storage_.hashmap_[sym]->weightedAvgPrice_ += qty * price;
storage_.hashmap_[sym]->maxTradePrice_ =
std::max(price, storage_.hashmap_[sym]->maxTradePrice_);
uint64_t diff = ts - storage_.hashmap_[sym]->lastTimestamp_ ;
storage_.hashmap_[sym]->lastTimestamp_ = ts;
storage_.hashmap_[sym]->maxTimeGap_ = std::max(storage_.hashmap_[sym]->maxTimeGap_, diff);
}
std::vector<std::string> cleanup() const {
std::vector<std::string> retvec;
for(const auto &v: storage_.hashmap_) {
std::string str;
str.reserve(75);
str += v.first;
str += "," ;
str += std::to_string(v.second->maxTimeGap_) ;
str += "," ;
str += std::to_string(v.second->totalVolume_) ;
str += "," ;
str += std::to_string(v.second->weightedAvgPrice_ / v.second->totalVolume_) ;
str += ",";
str += std::to_string(v.second->maxTradePrice_);
retvec.push_back(std::move(str));
}
storage_.clear();
return retvec;
}
private:
Storage<std::string, std::unique_ptr<Statistics>> &storage_;
};
template <typename derived, typename tradeentry>
class Parser {
public:
Parser(TradeEntryBase<derived, tradeentry> &base) : base_(base) {}
void parseFile(std::istream &infile, int num_entries = 4) const {
std::string line;
while(getline(infile, line)) {
line.erase(remove_if(line.begin(), line.end(),
static_cast<int(&)(int)>(std::isspace)), line.end());
std::vector<std::string> tokens;
tokens.reserve(num_entries);
std::stringstream linestream(line);
std::string tokenstr;
while(getline(linestream, tokenstr, ',')) {
tokens.push_back(std::move(tokenstr));
}
checkInput(tokens, num_entries);
tradeentry te(std::move(std::stoull(tokens[0])), std::move(tokens[1]),
stoi(tokens[2]), stoi(tokens[3]));
base_.process(te);
}
}
private:
TradeEntryBase<derived, tradeentry> &base_;
inline void checkInput(const std::vector<std::string> &input, const int &num_entries) const {
if(input.size() < num_entries) {
throw std::invalid_argument("number of enteries in the trade are less than " +
std::to_string(num_entries));
}
if(stoi(input[2]) <= 0 || stoi(input[3]) <= 0) {
throw std::invalid_argument("price or quantity is less than or equal to 0 ");
}
}
};
class Outputter {
public:
void save(std::ostream &outfile, std::vector<std::string> &vec, int sym_len = 3) const {
sort(vec.begin(), vec.end(),
[&sym_len](std::string &a, std::string &b)
{return a.substr(0,sym_len) < b.substr(0,sym_len);});
for(auto &v: vec) outfile << v << std::endl;
}
};
}
int main() {
using namespace streamingtrade;
Storage<std::string, std::unique_ptr<Statistics>> storage;
Outputter outputter;
StatisticsCalculator calculator(storage);
Parser<StatisticsCalculator, TradeEntry> p(calculator);
//std::ifstream infile = std::ifstream("test5.csv");
std::ifstream infile = std::ifstream("input.csv");
p.parseFile(infile);
auto vec = calculator.cleanup();
std::ofstream outfile = std::ofstream("output.csv");
outputter.save(outfile,vec);
}