#include "book.hpp"
|
#include <chrono>
|
#include <iostream>
|
|
Order::Order(double price, OrderSideEnum side, int volume,
|
std::chrono::nanoseconds timestamp, std::string id)
|
: price{price}, side{side}, remaining_volume{volume},
|
filled_volume(0), timestamp{timestamp}, id{id} {}
|
|
Level::Level(Order &order)
|
: price{order.price}, volume(order.remaining_volume), side{order.side},
|
timestamp{order.timestamp}, id{order.id} {}
|
|
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)
|
return true;
|
else
|
return false;
|
}
|
|
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)
|
return true;
|
else
|
return false;
|
}
|
|
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)
|
return true;
|
else
|
return false;
|
}
|
|
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)
|
return true;
|
else
|
return false;
|
}
|
|
bool operator==(const Level &a, const Level &b) {
|
if (a.price == b.price && a.timestamp == b.timestamp)
|
return true;
|
else
|
return false;
|
}
|
|
Book::Book()
|
: bidSide(), askSide(), productType(TEST), product("a"), stationId("b"),
|
unit("c"), expiry(std::chrono::nanoseconds(0)), aggFee(1), pasFee(-1),
|
broFee(2) {}
|
|
Book::Book(ProductTypeEnum productType, std::string product,
|
std::string stationId, std::string unit,
|
std::chrono::nanoseconds expiry, double aggFee, double pasFee,
|
double broFee)
|
: bidSide{}, askSide{}, productType{productType}, product(product),
|
stationId(stationId), unit(unit), expiry(expiry), aggFee(aggFee),
|
pasFee(pasFee), broFee(broFee) {}
|
|
void Book::ask(Order &order) {
|
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;
|
} 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);
|
}
|
}
|
}
|
|
void Book::bid(Order &order) {
|
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;
|
} 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);
|
}
|
}
|
}
|
|
void Book::printBook() {
|
std::cout << "Sell side";
|
this->askSide.printTop();
|
std::cout << "Buy side";
|
this->bidSide.printTop();
|
}
|
|
Book testBook(int orders, bool printBook) {
|
Book b = Book();
|
std::chrono::nanoseconds time(1);
|
for (int i = 0; i < orders; i++) {
|
Order t(i, Buy, 10, time++, "a");
|
b.bid(t);
|
}
|
for (int i = orders + 1; i < 2 * orders; i++) {
|
Order t(i, Sell, 10, time++, "b");
|
b.ask(t);
|
}
|
if (printBook)
|
b.printBook();
|
return b;
|
}
|