Joel Grunbaum
2022-01-12 611ad7b12fcc6b34229316099ae66bccbcf24ad9
commit | author | age
bb2353 1 #include "book.hpp"
16b655 2 #include <algorithm>
bb2353 3 #include <chrono>
127d5a 4 #include <cstddef>
bb2353 5 #include <iostream>
4fdb65 6 #include <string>
bb2353 7
16b655 8 namespace book
JG 9 {
10 Order::Order(double price, OrderSideEnum side, int volume, double timestamp,
11              std::string id)
2c515f 12     : price{price}, side{side}, remaining_volume{volume},
JG 13       filled_volume(0), timestamp{timestamp}, id{id}
16b655 14 {
JG 15 }
bb2353 16
16b655 17 Level::Level(Order& order)
2c515f 18     : price{order.price}, volume(order.remaining_volume), side{order.side},
JG 19       timestamp{order.timestamp}, id{order.id}
16b655 20 {
bb2353 21 }
JG 22
16b655 23 bool operator<(const Level& a, const Level& b)
JG 24 {
2c515f 25     if (a.price < b.price)
JG 26         return true;
27     else if (a.price == b.price && (a.timestamp > b.timestamp))
28         return true;
29     else
30         return false;
bb2353 31 }
JG 32
16b655 33 bool operator>(const Level& a, const Level& b)
JG 34 {
2c515f 35     if (a.price > b.price)
JG 36         return true;
37     else if (a.price == b.price && (a.timestamp > b.timestamp))
38         return true;
39     else
40         return false;
bb2353 41 }
JG 42
16b655 43 bool operator<=(const Level& a, const Level& b)
JG 44 {
2c515f 45     if (a.price <= b.price)
JG 46         return true;
47     else if (a.price == b.price && (a.timestamp >= b.timestamp))
48         return true;
49     else
50         return false;
bb2353 51 }
JG 52
16b655 53 bool operator>=(const Level& a, const Level& b)
JG 54 {
2c515f 55     if (a.price >= b.price)
JG 56         return true;
57     else if (a.price == b.price && (a.timestamp >= b.timestamp))
58         return true;
59     else
60         return false;
16b655 61 }
JG 62
63 bool operator==(const Level& a, const Level& b)
64 {
2c515f 65     if (a.price == b.price && (a.timestamp == b.timestamp))
JG 66         return true;
67     else
68         return false;
127d5a 69 }
JG 70
71 std::ostream& operator<<(std::ostream& out, const Level& a)
72 {
2c515f 73     return out << "Price: " << a.price << ", volume: " << a.volume
JG 74                << ", time: " << a.timestamp << ", id: " << a.id;
bb2353 75 }
JG 76
77 Book::Book()
2c515f 78     : bidSide(), askSide(), productType(TEST), product("a"), stationId(7),
JG 79       unit("c"), expiry(std::chrono::nanoseconds(0)), aggFee(1), pasFee(-1),
80       broFee(2)
16b655 81 {
JG 82 }
bb2353 83
2c515f 84 Book::Book(ProductTypeEnum productType, std::string product, int stationId,
JG 85            std::string unit, std::chrono::nanoseconds expiry, double aggFee,
86            double pasFee, double broFee)
87     : bidSide{}, askSide{}, productType{productType}, product(product),
88       stationId(stationId), unit(unit), expiry(expiry), aggFee(aggFee),
611ad7 89       pasFee(pasFee), broFee(broFee), bomPrice(0)
16b655 90 {
JG 91 }
bb2353 92
16b655 93 void Book::ask(Order& order)
JG 94 {
4fdb65 95     auto a = 
JG 96     this->askSide.emplace(order.id, order);
97     if (!a.second) {
98         std::cout << order.id << "already exists" << std::endl;
99     }
bb2353 100 }
JG 101
16b655 102 void Book::bid(Order& order)
JG 103 {
4fdb65 104     auto a =
JG 105     this->bidSide.emplace(order.id, order);
106     if (!a.second) {
107         std::cout << order.id << "already exists" << std::endl;
108     }
bb2353 109 }
JG 110
16b655 111 void Book::printBook(std::size_t numOrders)
JG 112 {
2c515f 113     std::cout << "Sell side: " << this->askSide.size() << std::endl;
4fdb65 114     std::vector<Level> askCopy;
JG 115     for (auto i : this->askSide) askCopy.push_back(i.second);
2c515f 116     std::size_t count = 0;
JG 117     std::sort(askCopy.begin(), askCopy.end());
4fdb65 118     for (auto i = askCopy.rbegin(); i != askCopy.rend(); i++) {
JG 119         std::cout << *i << std::endl;
2c515f 120         count++;
JG 121         if (count > numOrders) break;
122     }
123     std::cout << "Buy side: " << this->bidSide.size() << std::endl;
4fdb65 124     std::vector<Level> bidCopy;
JG 125     for (auto i : this->bidSide) bidCopy.push_back(i.second);
2c515f 126     count = 0;
JG 127     std::sort(bidCopy.begin(), bidCopy.end());
4fdb65 128     for (auto i = bidCopy.rbegin(); i != bidCopy.rend(); i++) {
JG 129         std::cout << *i << std::endl;
2c515f 130         count++;
JG 131         if (count > numOrders) break;
132     }
133 }
16b655 134
JG 135 Book testBook(int orders, bool printBook)
136 {
2c515f 137     Book b = Book();
JG 138     double time(1);
139     for (int i = 1; i < orders; i++) {
4fdb65 140         Order t(i, Buy, 10, time++, std::to_string(i));
2c515f 141         b.bid(t);
JG 142     }
143     for (int i = orders + 1; i < 2 * orders; i++) {
4fdb65 144         Order t(i, Sell, 10, time++, std::to_string(i));
2c515f 145         b.ask(t);
JG 146     }
147     if (printBook) b.printBook(orders - 1);
148     return b;
bb2353 149 }
16b655 150 } // namespace book