#include "book.hpp"
|
#include <algorithm>
|
#include <chrono>
|
#include <cstddef>
|
#include <iostream>
|
#include <string>
|
|
namespace book
|
{
|
Order::Order(double price, OrderSideEnum side, int volume, double 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;
|
}
|
|
std::ostream& operator<<(std::ostream& out, const Level& a)
|
{
|
return out << "Price: " << a.price << ", volume: " << a.volume
|
<< ", time: " << a.timestamp << ", id: " << a.id;
|
}
|
|
Book::Book()
|
: bidSide(), askSide(), productType(TEST), product("a"), stationId(7),
|
unit("c"), expiry(std::chrono::nanoseconds(0)), aggFee(1), pasFee(-1),
|
broFee(2)
|
{
|
}
|
|
Book::Book(ProductTypeEnum productType, std::string product, int 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), bomPrice(0)
|
{
|
}
|
|
void Book::ask(Order& order)
|
{
|
Level a(order);
|
auto b = std::lower_bound(this->askSide.begin(), this->askSide.end(), a);
|
this->askSide.insert(b, a);
|
}
|
|
void Book::bid(Order& order)
|
{
|
Level a(order);
|
auto b = std::upper_bound(this->bidSide.begin(), this->bidSide.end(), a);
|
this->bidSide.insert(b, a);
|
}
|
|
void Book::printBook(std::size_t numOrders)
|
{
|
std::cout << "Sell side: " << this->askSide.size() << std::endl;
|
std::size_t count = 0;
|
for (auto i = this->askSide.rbegin(); i != this->askSide.rend(); i++) {
|
std::cout << *i << std::endl;
|
count++;
|
if (count > numOrders) break;
|
}
|
std::cout << "Buy side: " << this->bidSide.size() << std::endl;
|
count = 0;
|
for (auto i = this->bidSide.rbegin(); i != bidSide.rend(); i++) {
|
std::cout << *i << std::endl;
|
count++;
|
if (count > numOrders) break;
|
}
|
}
|
|
Book testBook(int orders, bool printBook)
|
{
|
Book b = Book();
|
double time(1);
|
for (int i = 1; i < orders; i++) {
|
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++, std::to_string(i));
|
b.ask(t);
|
}
|
if (printBook) b.printBook(orders - 1);
|
return b;
|
}
|
} // namespace book
|