-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orderbook.h
77 lines (61 loc) · 1.95 KB
/
Orderbook.h
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
#pragma once
#include <map>
#include <unordered_map>
#include <thread>
#include <condition_variable>
#include <mutex>
#include "Usings.h"
#include "Order.h"
#include "OrderModify.h"
#include "OrderbookLevelInfos.h"
#include "Trade.h"
class Orderbook
{
private:
struct OrderEntry
{
OrderPointer order_{ nullptr };
OrderPointers::iterator location_;
};
struct LevelData
{
Quantity quantity_{ };
Quantity count_{ };
enum class Action
{
Add,
Remove,
Match,
};
};
std::unordered_map<Price, LevelData> data_;
std::map<Price, OrderPointers, std::greater<Price>> bids_;
std::map<Price, OrderPointers, std::less<Price>> asks_;
std::unordered_map<OrderId, OrderEntry> orders_;
mutable std::mutex ordersMutex_;
std::thread ordersPruneThread_;
std::condition_variable shutdownConditionVariable_;
std::atomic<bool> shutdown_{ false };
void PruneGoodForDayOrders();
void CancelOrders(OrderIds orderIds);
void CancelOrderInternal(OrderId orderId);
void OnOrderCancelled(OrderPointer order);
void OnOrderAdded(OrderPointer order);
void OnOrderMatched(Price price, Quantity quantity, bool isFullyFilled);
void UpdateLevelData(Price price, Quantity quantity, LevelData::Action action);
bool CanFullyFill(Side side, Price price, Quantity quantity) const;
bool CanMatch(Side side, Price price) const;
Trades MatchOrders();
public:
Orderbook();
Orderbook(const Orderbook&) = delete;
void operator=(const Orderbook&) = delete;
Orderbook(Orderbook&&) = delete;
void operator=(Orderbook&&) = delete;
~Orderbook();
Trades AddOrder(OrderPointer order);
void CancelOrder(OrderId orderId);
Trades ModifyOrder(OrderModify order);
std::size_t Size() const;
OrderbookLevelInfos GetOrderInfos() const;
};