Joel Grunbaum
2022-01-07 127d5af6f934b162c98a40ce414b98415d899cea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "book.hpp"
#include <chrono>
#include <cstddef>
#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.count() > b.timestamp.count()))
    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.count() > b.timestamp.count()))
    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.count() >= b.timestamp.count()))
    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.count() >= b.timestamp.count()))
    return true;
  else
    return false;
}
 
bool operator==(const Level &a, const Level &b) {
    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),
      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;
      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);
  }
}
 
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;
      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);
  }
}
 
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 = 1; 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(orders - 1);
  return b;
}