| | |
| | | #include <chrono> |
| | | #include <cstddef> |
| | | #include <iostream> |
| | | #include <string> |
| | | |
| | | namespace book |
| | | { |
| | |
| | | std::ostream& operator<<(std::ostream& out, const Level& a) |
| | | { |
| | | return out << "Price: " << a.price << ", volume: " << a.volume |
| | | << ", time: " << a.timestamp << ", id: " << a.id; |
| | | << ", time: " << a.timestamp << ", id: " << a.id; |
| | | } |
| | | |
| | | Book::Book() |
| | |
| | | double pasFee, double broFee) |
| | | : bidSide{}, askSide{}, productType{productType}, product(product), |
| | | stationId(stationId), unit(unit), expiry(expiry), aggFee(aggFee), |
| | | pasFee(pasFee), broFee(broFee) |
| | | pasFee(pasFee), broFee(broFee), bomPrice(0) |
| | | { |
| | | } |
| | | |
| | | void Book::ask(Order& order) |
| | | { |
| | | while (this->bidSide.size() && this->bidSide[0].price >= order.price) { |
| | | if (this->bidSide[0].volume > order.remaining_volume) { |
| | | int temp = this->bidSide[0].volume; |
| | | order.filled_volume += temp; |
| | | this->bidSide.front().volume -= order.remaining_volume; |
| | | order.remaining_volume -= temp; |
| | | break; |
| | | } else { |
| | | order.remaining_volume -= this->bidSide[0].volume; |
| | | order.filled_volume += this->bidSide[0].volume; |
| | | this->bidSide.erase(this->bidSide.begin()); |
| | | std::make_heap(this->bidSide.begin(), this->bidSide.end(), |
| | | std::less<Level>()); |
| | | } |
| | | } |
| | | if (order.remaining_volume > 0) { |
| | | this->askSide.emplace_back(order); |
| | | std::make_heap(this->askSide.begin(), this->askSide.end(), |
| | | std::greater<Level>()); |
| | | auto a = this->askSide.emplace(order.id, order); |
| | | if (!a.second) { |
| | | std::cout << order.id << "already exists" << std::endl; |
| | | } |
| | | } |
| | | |
| | | void Book::bid(Order& order) |
| | | { |
| | | while (this->askSide.size() && this->askSide[0].price <= order.price) { |
| | | if (this->askSide[0].volume > order.remaining_volume) { |
| | | int temp = this->askSide.front().volume; |
| | | order.filled_volume += temp; |
| | | this->askSide.front().volume -= order.remaining_volume; |
| | | order.remaining_volume -= temp; |
| | | break; |
| | | } else { |
| | | order.remaining_volume -= this->askSide[0].volume; |
| | | order.filled_volume += this->askSide[0].volume; |
| | | this->askSide.erase(this->askSide.begin()); |
| | | std::make_heap(this->askSide.begin(), this->askSide.end(), |
| | | std::greater<Level>()); |
| | | } |
| | | } |
| | | if (order.remaining_volume > 0) { |
| | | this->bidSide.emplace_back(order); |
| | | std::make_heap(this->bidSide.begin(), this->bidSide.end(), |
| | | std::less<Level>()); |
| | | auto a = this->bidSide.emplace(order.id, order); |
| | | if (!a.second) { |
| | | std::cout << order.id << "already exists" << std::endl; |
| | | } |
| | | } |
| | | |
| | | void Book::printBook(std::size_t numOrders) |
| | | { |
| | | std::cout << "Sell side: " << this->askSide.size() << std::endl; |
| | | std::vector<Level> askCopy(this->askSide); |
| | | std::vector<Level> askCopy; |
| | | for (auto i : this->askSide) |
| | | askCopy.push_back(i.second); |
| | | std::size_t count = 0; |
| | | std::sort(askCopy.begin(), askCopy.end()); |
| | | std::reverse(askCopy.begin(), askCopy.end()); |
| | | double price = askCopy.front().price; |
| | | for (auto i : askCopy) { |
| | | std::cout << i << std::endl; |
| | | for (auto i = askCopy.rbegin(); i != askCopy.rend(); i++) { |
| | | std::cout << *i << std::endl; |
| | | count++; |
| | | if (count > numOrders) break; |
| | | } |
| | | std::cout << "Buy side: " << this->bidSide.size() << std::endl; |
| | | std::vector<Level> bidCopy(this->bidSide); |
| | | std::vector<Level> bidCopy; |
| | | for (auto i : this->bidSide) |
| | | bidCopy.push_back(i.second); |
| | | count = 0; |
| | | std::sort(bidCopy.begin(), bidCopy.end()); |
| | | std::reverse(bidCopy.begin(), bidCopy.end()); |
| | | price = bidCopy.front().price; |
| | | for (auto i : bidCopy) { |
| | | std::cout << i << std::endl; |
| | | for (auto i = bidCopy.rbegin(); i != bidCopy.rend(); i++) { |
| | | std::cout << *i << std::endl; |
| | | count++; |
| | | if (count > numOrders) break; |
| | | } |
| | |
| | | Book b = Book(); |
| | | double time(1); |
| | | for (int i = 1; i < orders; i++) { |
| | | Order t(i, Buy, 10, time++, "a"); |
| | | Order t(i, Buy, 10, time++, std::to_string(i)); |
| | | b.bid(t); |
| | | } |
| | | for (int i = orders + 1; i < 2 * orders; i++) { |
| | | Order t(i, Sell, 10, time++, "b"); |
| | | Order t(i, Sell, 10, time++, std::to_string(i)); |
| | | b.ask(t); |
| | | } |
| | | if (printBook) b.printBook(orders - 1); |