From 127d5af6f934b162c98a40ce414b98415d899cea Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Fri, 07 Jan 2022 10:43:34 +0000
Subject: [PATCH] finished book
---
book.cpp | 74 +++++++++++++++++++++++++++----------
1 files changed, 54 insertions(+), 20 deletions(-)
diff --git a/book.cpp b/book.cpp
index 2202639..9cb14e7 100644
--- a/book.cpp
+++ b/book.cpp
@@ -1,5 +1,6 @@
#include "book.hpp"
#include <chrono>
+#include <cstddef>
#include <iostream>
Order::Order(double price, OrderSideEnum side, int volume,
@@ -14,7 +15,7 @@
bool operator<(const Level &a, const Level &b) {
if (a.price < b.price)
return true;
- else if (a.price == b.price && a.timestamp < b.timestamp)
+ else if (a.price == b.price && (a.timestamp.count() > b.timestamp.count()))
return true;
else
return false;
@@ -23,7 +24,7 @@
bool operator>(const Level &a, const Level &b) {
if (a.price > b.price)
return true;
- else if (a.price == b.price && a.timestamp > b.timestamp)
+ else if (a.price == b.price && (a.timestamp.count() > b.timestamp.count()))
return true;
else
return false;
@@ -32,7 +33,7 @@
bool operator<=(const Level &a, const Level &b) {
if (a.price <= b.price)
return true;
- else if (a.price == b.price && a.timestamp <= b.timestamp)
+ else if (a.price == b.price && (a.timestamp.count() >= b.timestamp.count()))
return true;
else
return false;
@@ -41,19 +42,50 @@
bool operator>=(const Level &a, const Level &b) {
if (a.price >= b.price)
return true;
- else if (a.price == b.price && a.timestamp >= b.timestamp)
+ else if (a.price == b.price && (a.timestamp.count() >= b.timestamp.count()))
return true;
else
return false;
}
bool operator==(const Level &a, const Level &b) {
- if (a.price == b.price && a.timestamp == b.timestamp)
+ if (a.price == b.price && (a.timestamp.count() == b.timestamp.count()))
return true;
else
return false;
}
+std::ostream& operator<<(std::ostream& out, const Level& a)
+{
+ return out << "Price: " << a.price << ", volume: " << a.volume << ", time: " << a.timestamp.count() << ", id: " << a.id;
+}
+
+template<class T>
+void Side<T>::deleteLevel(std::string orderId)
+{
+ for (auto i = this->c.begin(); i != this->c.end();) {
+ if (*i.id == orderId) {
+ this->c.erase(i);
+ std::make_heap(this->c.begin(), this->c.end(), this->comp);
+ }
+ }
+}
+
+template<class T>
+void Side<T>::topRemoveVolume(int volume) { this->c[0].volume -= volume; }
+
+template<class T>
+void Side<T>::printTop(std::size_t num)
+{
+ std::vector<Level> copy(this->c);
+ std::sort(copy.begin(), copy.end(), this->comp);
+ if (copy.size() && copy[0].side == Buy)
+ std::reverse(copy.begin(), copy.end());
+ for (std::size_t i = 0; i < copy.size() && i < num; i++) {
+ std::cout << copy[i] << std::endl;
+ }
+}
+
Book::Book()
: bidSide(), askSide(), productType(TEST), product("a"), stationId("b"),
unit("c"), expiry(std::chrono::nanoseconds(0)), aggFee(1), pasFee(-1),
@@ -68,52 +100,54 @@
pasFee(pasFee), broFee(broFee) {}
void Book::ask(Order &order) {
- while (this->bidSide.size() && this->bidSide.top().price > order.price) {
+ while (this->bidSide.size() && this->bidSide.top().price >= order.price) {
if (this->bidSide.top().volume > order.remaining_volume) {
int temp = this->bidSide.top().volume;
order.filled_volume += temp;
this->bidSide.topRemoveVolume(order.remaining_volume);
order.remaining_volume -= temp;
+ break;
} else {
order.remaining_volume -= this->bidSide.top().volume;
order.filled_volume += this->bidSide.top().volume;
this->bidSide.pop();
}
- if (order.remaining_volume > 0) {
- this->askSide.emplace(order);
- }
+ }
+ if (order.remaining_volume > 0) {
+ this->askSide.emplace(order);
}
}
void Book::bid(Order &order) {
- while (this->askSide.size() && this->askSide.top().price > order.price) {
+ while (this->askSide.size() && this->askSide.top().price <= order.price) {
if (this->askSide.top().volume > order.remaining_volume) {
int temp = this->askSide.top().volume;
order.filled_volume += temp;
this->askSide.topRemoveVolume(order.remaining_volume);
order.remaining_volume -= temp;
+ break;
} else {
order.remaining_volume -= this->askSide.top().volume;
order.filled_volume += this->askSide.top().volume;
this->askSide.pop();
}
- if (order.remaining_volume > 0) {
- this->bidSide.emplace(order);
- }
+ }
+ if (order.remaining_volume > 0) {
+ this->bidSide.emplace(order);
}
}
-void Book::printBook() {
- std::cout << "Sell side";
- this->askSide.printTop();
- std::cout << "Buy side";
- this->bidSide.printTop();
+void Book::printBook(std::size_t numOrders) {
+ std::cout << "Sell side: " << this->askSide.size() << std::endl;
+ this->askSide.printTop(numOrders);
+ std::cout << "Buy side: " << this->bidSide.size() << std::endl;
+ this->bidSide.printTop(numOrders);
}
Book testBook(int orders, bool printBook) {
Book b = Book();
std::chrono::nanoseconds time(1);
- for (int i = 0; i < orders; i++) {
+ for (int i = 1; i < orders; i++) {
Order t(i, Buy, 10, time++, "a");
b.bid(t);
}
@@ -122,6 +156,6 @@
b.ask(t);
}
if (printBook)
- b.printBook();
+ b.printBook(orders - 1);
return b;
}
--
Gitblit v1.10.0