-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderBook.cpp
More file actions
124 lines (98 loc) · 2.89 KB
/
OrderBook.cpp
File metadata and controls
124 lines (98 loc) · 2.89 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
#include "OrderBook.h"
#include "CSVReader.h"
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
OrderBook::OrderBook(std::string filename) {
orders = CSVReader::readCSV(filename);
}
std::vector<std::string> OrderBook::getKnownProducts() { // Loops through the dataset and maps true value to the products
std::vector<std::string> products; // Stores all the available products into a vector of strings and is returned
std::map<std::string, bool> prodMap;
for (OrderBookEntry& e : orders) {
prodMap[e.product] = true;
}
for (auto const& e : prodMap) {
products.push_back(e.first);
}
return products;
}
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type, std::string product, std::string timestamp) {
std::vector<OrderBookEntry> orders_sub;
for (OrderBookEntry& e : orders) {
if (e.orderType == type &&
e.product == product &&
e.timestamp == timestamp) {
orders_sub.push_back(e);
}
}
return orders_sub;
}
double OrderBook::getHighPrice(std::vector<OrderBookEntry>& orders) {
if (orders.size() == 0) { //if entries for product is empty, print out line
std::cout << "This product has no entries" << std::endl;
return 0;
}
else {
double max = orders[0].price;
for (OrderBookEntry e : orders) {
if (e.price > max)max = e.price;
}
return max;
}
}
double OrderBook::getLowPrice(std::vector<OrderBookEntry>& orders) {
if (orders.size() == 0) { //if entries for product is empty, print out line
std::cout << "This product has no entries" << std::endl;
return 0;
}
else {
double min = orders[0].price;
for (OrderBookEntry e : orders) {
if (e.price < min)min = e.price;
}
return min;
}
}
double OrderBook::getAvgPrice(std::vector<OrderBookEntry>& orders) {
double sum = 0;
double avg = 0;
for (OrderBookEntry e : orders) {
sum = sum + e.price;
avg = sum / orders.size();
}
return avg;
}
std::string OrderBook::getEarliestTime() {
return orders[0].timestamp;
}
std::string OrderBook::getNextTime(std::string timestamp) {
std::string next_timestamp = "";
for (OrderBookEntry& e : orders) {
if (e.timestamp > timestamp) {
next_timestamp = e.timestamp;
break;
}
}
if (next_timestamp == "") {
next_timestamp = orders[0].timestamp;
}
return next_timestamp;
}
std::string OrderBook::getPrevTime(std::string timestamp) {
std::string prev_timestamp = "";
int index = 0;
for (OrderBookEntry& e : orders) {
if (e.timestamp == timestamp && timestamp != orders[0].timestamp) { // matches current timestep to dataset's timestep & not the first timestep
prev_timestamp = orders[index-1].timestamp; // minus 1 to the index to get the previous timestep
break;
} else {
index++;
}
}
if (prev_timestamp == "") {
prev_timestamp = orders[0].timestamp;
}
return prev_timestamp;
}