-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (112 loc) · 3.96 KB
/
main.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
#include <iostream>
#include <string>
#include <vector>
enum class OrderBookType {bid, ask};
OrderBookType orderType = OrderBookType::ask;
// define a class that can store the data for an entry in the order book
class OrderBookEntry {
private:
std::string timeStamps;
std::string products;
OrderBookType orderTypes;
double bids;
double asks;
// Constructor implementation
public:
OrderBookEntry( std::string timeStamps, std::string products,
OrderBookType orderTypes, double bids, double asks)
: timeStamps(timeStamps), products(products), orderTypes(orderTypes),
bids(bids), asks(asks) {}
std::string getTimestamp() const { return timeStamps; }
std::string getProduct() const { return products; }
OrderBookType getOrderType() const { return orderTypes; }
double getBidPrice() const { return bids; }
double getAskAmount() const { return asks; }
};
// Function to compute average price
double computeAveragePrice(const std::vector<OrderBookEntry>& entries) {
double total = 0.0;
for (const auto& entry : entries) {
total += entry.getBidPrice();
}
return total / entries.size();
}
// Function to compute the lowest bid price
double computeLowPrice(const std::vector<OrderBookEntry>& entries) {
double lowest = entries[0].getBidPrice();
for (const auto& entry : entries) {
if (entry.getBidPrice() < lowest) {
lowest = entry.getBidPrice();
}
}
return lowest;
}
// Function to compute the highest bid price
double computeHighPrice(const std::vector<OrderBookEntry>& entries) {
double highest = entries[0].getBidPrice();
for (const auto& entry : entries) {
if (entry.getBidPrice() > highest) {
highest = entry.getBidPrice();
}
}
return highest;
}
// Function to compute the bid price spread
double computePriceSpread(const std::vector<OrderBookEntry>& entries) {
double lowest = computeLowPrice(entries);
double highest = computeHighPrice(entries);
return highest - lowest;
}
int main () {
// Constraint the data in the program
// try to write very clear code that does not use too much implicit information
std::vector<OrderBookEntry> entries;
entries.push_back( OrderBookEntry{"2020/03/17 17:01:24.884492",
"ETH/BTC",
OrderBookType::ask,
0.5,
0.125});
entries.push_back( OrderBookEntry{"2021/04/18 18:02:25.884493",
"BTC/ETH",
OrderBookType::bid,
0.56,
0.1257});
entries.push_back( OrderBookEntry{"2020/02/18 16:03:23.884491",
"ETH/BTC",
OrderBookType::ask,
0.6,
0.236});
entries.push_back( OrderBookEntry{"2021/03/16 15:03:27.884494",
"BTC/ETH",
OrderBookType::bid,
0.46,
0.2368});
entries.push_back( OrderBookEntry{"2020/04/15 11:05:27.884496",
"ETH/BTC",
OrderBookType::ask,
0.72,
0.314});
entries.push_back( OrderBookEntry{"2021/05/12 18:08:28.995508",
"BTC/ETH",
OrderBookType::bid,
0.86,
0.1257});
// Iterating over entries
std::cout << "Today's buy and sell \n";
std::cout << "Order Time, Ordered Product, Order Type, Bid Amount, Ask Amount \n";
for (const auto& entry : entries) {
std::cout << entry.getTimestamp() << ", " << entry.getProduct() << ", ";
switch (orderType) {
case OrderBookType::ask : std::cout << "ask, "; break;
case OrderBookType::bid : std::cout << "bid, "; break;
}
std::cout << entry.getBidPrice() << ", " << entry.getAskAmount() << "\n";
}
// Compute and print market stats
std::cout << "-------Market Stats--------\n";
std::cout << "Average Bid Price: " << computeAveragePrice(entries) << "\n";
std::cout << "Lowest Bid Price: " << computeLowPrice(entries) << "\n";
std::cout << "Highest BidPrice: " << computeHighPrice(entries) << "\n";
std::cout << "Price Bid Spread: " << computePriceSpread(entries) << "\n";
return 0;
}