commit | author | age
|
bb2353
|
1 |
#pragma once |
JG |
2 |
|
|
3 |
#include <chrono> |
|
4 |
#include <iostream> |
|
5 |
#include <queue> |
|
6 |
#include <string> |
|
7 |
#include <vector> |
|
8 |
|
|
9 |
enum OrderSideEnum { Buy, Sell }; |
|
10 |
enum ProductTypeEnum { TEST, FUTURE, SPREAD, CALL, PUT }; |
|
11 |
|
|
12 |
struct Order { |
|
13 |
double price; |
|
14 |
OrderSideEnum side; |
|
15 |
int remaining_volume; |
|
16 |
int filled_volume; |
|
17 |
std::chrono::nanoseconds timestamp; |
|
18 |
std::string id; |
|
19 |
Order(double price, OrderSideEnum side, int volume, |
|
20 |
std::chrono::nanoseconds timestamp, std::string id); |
|
21 |
}; |
|
22 |
|
|
23 |
struct Level { |
|
24 |
double price; |
|
25 |
int volume; |
|
26 |
OrderSideEnum side; |
|
27 |
std::chrono::nanoseconds timestamp; |
|
28 |
std::string id; |
|
29 |
|
|
30 |
Level(Order &order); |
|
31 |
}; |
|
32 |
|
|
33 |
bool operator>(const Level &a, const Level &b); |
|
34 |
bool operator<(const Level &a, const Level &b); |
|
35 |
bool operator>=(const Level &a, const Level &b); |
|
36 |
bool operator<=(const Level &a, const Level &b); |
|
37 |
bool operator==(const Level &a, const Level &b); |
|
38 |
|
|
39 |
template <class T> |
|
40 |
struct Side : public std::priority_queue<Level, std::vector<Level>, T> { |
|
41 |
public: |
|
42 |
void deleteLevel(std::string orderId) { |
|
43 |
for (auto i = this->c.begin(); i != this->c.end();) { |
|
44 |
if (*i.id == orderId) { |
|
45 |
this->c.erase(i); |
|
46 |
std::make_heap(this->c.begin(), this->c.end(), this->comp); |
|
47 |
} |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
void topRemoveVolume(int volume) { this->c[0].volume -= volume; } |
|
52 |
|
|
53 |
void printTop(int num = 5) { |
|
54 |
std::sort(this->c.begin(), this->c.end(), this->comp); |
|
55 |
for (int i = 0; i < num && i < this->size(); i++) { |
|
56 |
std::cout << "Price: " << this->c[i].price |
|
57 |
<< ", volume: " << this->c[i].volume |
|
58 |
<< ", time: " << this->c[i].timestamp.count() |
|
59 |
<< ", id: " << this->c[i].id << std::endl; |
|
60 |
} |
|
61 |
std::make_heap(this->c.begin(), this->c.end(), this->comp); |
|
62 |
} |
|
63 |
}; |
|
64 |
|
|
65 |
using AskSide = Side<std::greater<Level>>; |
|
66 |
using BidSide = Side<std::less<Level>>; |
|
67 |
|
|
68 |
struct Book { |
|
69 |
BidSide bidSide; |
|
70 |
AskSide askSide; |
|
71 |
ProductTypeEnum productType; |
|
72 |
std::string product; |
|
73 |
std::string stationId; |
|
74 |
std::string unit; |
|
75 |
std::chrono::nanoseconds expiry; |
|
76 |
double aggFee; |
|
77 |
double pasFee; |
|
78 |
double broFee; |
|
79 |
|
|
80 |
Book(); |
|
81 |
Book(ProductTypeEnum productType, std::string product, std::string stationId, |
|
82 |
std::string unit, std::chrono::nanoseconds expiry, double aggFee, |
|
83 |
double pasFee, double broFee); |
|
84 |
void ask(Order &order); |
|
85 |
void bid(Order &order); |
|
86 |
void printBook(); |
|
87 |
}; |
|
88 |
|
|
89 |
Book testBook(int orders = 10, bool printBook = true); |