-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOrderBook.h
More file actions
209 lines (167 loc) · 5.79 KB
/
Copy pathOrderBook.h
File metadata and controls
209 lines (167 loc) · 5.79 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef __ORDERBOOK__H__
#define __ORDERBOOK__H__
#include <map>
#include <list>
#include <cstdio>
class Order;
class Market;
/*! \brief Generic order book class
*
* The class implements an order book, on a given asset.
*
*/
class OrderBook
{
public:
/*! \brief order book constructor
*
* \param a_market link to the market
* \param a_identifier asset identifier
* \param a_tickSize tick size
* \param a_defaultBid it is only needed to put the first limit bid orders, as these are put in a certain distance relative to the bid price. So the order book cannot be empty at the beginning of the simulation
* \param a_defaultAsk same as for bids
*
*/
OrderBook(Market *a_market, int a_identifier, int a_tickSize, int a_defaultBid, int a_defaultAsk);
virtual ~OrderBook();
int getIdentifier() const ;
/*! \brief returns the ask price
*
*/
int getAskPrice() const;
/*! \brief returns the bid price
*
*/
int getBidPrice() const;
/*! \brief returns the tick size
*
*/
int getTickSize() const;
/*! \brief returns the total quantity of the best bid price
*
*/
int getBidQuantity() const;
/*! \brief returns the total quantity of the best ask price
*
*/
int getAskQuantity() const;
/*! \brief returns the available quantity for a given limit price
*
* \param a_orderQueue The order queue (the bid one, or the ask one)
* \param a_priceLevel price level
*
* If the price does not exist, it returns 0.
*/
int getQuantityForThisPrice(std::map< int , std::list < Order > > a_orderQueue,int a_priceLevel) const;
/*! \brief returns the available quantity up to a given Bid price
*/
int getBidQuantityUpToPrice(int a_price) const;
/*! \brief returns the available quantity up to a given ask price
*/
int getAskQuantityUpToPrice(int a_price) const;
/*! cancel a specific given order
*
*/
void processCancellation(int a_agentIdentifier,int a_orderIdentifier, double a_time) ;
/*! \brief processes the order
*
* \param a_order order to be processed
*
* The function also notifies the owner of the order of the result of this processing, and updates the last price for the concerned asset, and the cash position for concerned agents.
*/
void processOrder(Order & a_order);
/*! \brief prints an already stored history of the order book, if the bolean m_storeOrderBookHistory was set to true
*
*/
void printStoredOrderBookHistory();
/*! \brief sets the storage choice
*
* \param a_store true if you want to remember all the events at a certain depth
* \param a_depth maximum depth.
*
*/
void setStoreOrderBookHistory(bool a_store, int a_depth);
void setStoreOrderHistory(bool a_store);
/*! \brief sets the print choice
*
* \param a_print true if you want to print events when they come
* \param a_depth maximum depth.
*
*/
void setPrintOrderBookHistory(bool a_print, int a_depth);
/*! \brief returns the last price of the asset
*
*/
int getPrice() const;
/*! \brief returns the distance to best opposite quote in ticks
*
*/
int getDistanceToBestOppositeQuote(int a_price) const;
/*! \brief prepares data to be plotted in Gnuplot
*
*/
void getOrderBookForPlot(std::vector<int> &a_price,std::vector<int> &a_priceQ, std::vector<int> &MMprices, std::vector<int> &MMvolumes) const;
/*! \brief returns the bid Queue
*
*/
std::map< int , std::list < Order > > getBidQueue() const;
/*! \brief returns the ask Queue
*
*/
std::map< int , std::list < Order > > getAskQueue() const;
std::vector<int> getHistoricPrices();
std::vector<double> getTransactionsTimes();
double getReturnsSumSquared();
/*! \brief returns the number of orders of the concerned agent at the specified price
*
*/
int getNumberOfOrders(int a_agentIdentifier, int a_price) const;
int getTotalVolumeAtPrice(int a_price) const;
int getTotalAskQuantity() ;
int getTotalBidQuantity() ;
int getBidPriceAtLevel(int levelMax) ;
int getAskPriceAtLevel(int levelMax) ;
const std::vector<OrderBookHistory> & getOrderBookHistory() const;
const std::vector<Order> & getOrderHistory() const;
Market * getLinkToMarket() ;
void cleanOrderBook();
void setDefaultBidAsk(int bid, int ask);
private:
void printOrderBookHistoryOnTheFly(double a_time);
void storeOrderBookHistory(double a_time);
OrderBookHistory buildAHistoryLine(double a_time) const;
void printLineOfHistory(OrderBookHistory a_line,std::ofstream & a_outFile);
void processLimitBuyOrder(Order &a_order);
void processLimitSellOrder(Order &a_order);
void processMarketBuyOrder(Order &a_order);
void processMarketSellOrder(Order &a_order);
void printHeader(std::ofstream & a_outFile) const;
private:
int m_identifier;
Market * m_linkToMarket; // DO NOT DELETE, JUST A LINK
int m_tickSize ;
int m_defaultBid;
int m_defaultAsk;
int m_last;
int m_lastQ;
// We need sometimes to iterate on the elements.
// And while canceling, we can cancel an order situated in the middle of the container
// hence the choice of a list
// But Only queue-type operations of lists will be used when pushing (push_back), and executing(pop_front)
std::map< int , std::list < Order > > m_bids;
std::map< int , std::list < Order > > m_asks;
// Parameters for keeping history of orders
std::vector<OrderBookHistory> m_orderBookHistory;
std::vector<Order> m_orderHistory;
bool m_storeOrderBookHistory;
bool m_storeOrderHistory ;
bool m_printHistoryonTheFly;
int m_maxDepth;
bool m_headerPrinted;
//contains the prices of the asset
std::vector<int> m_historicPrices;
//contains the times of transactions (Market orders)
std::vector<double> m_transactionsTimes;
double m_returnsSumSquared;
};
#endif // __ORDERBOOK__H__