-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_test.cpp
225 lines (148 loc) · 5.08 KB
/
exchange_test.cpp
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
210
211
212
213
214
215
216
217
218
#define BOOST_TEST_MODULE exchange
#include <boost/test/included/unit_test.hpp>
#include "test.h"
struct TestListener : ExchangeListener {
std::vector<Trade> trades;
void onTrade(const Trade& trade) override {
trades.push_back(trade);
}
std::vector<Order> orders;
void onOrder(const Order& order) override {
orders.push_back(order);
}
};
BOOST_AUTO_TEST_CASE( insertOrder_buy ) {
TestExchange ob;
auto o1_id = ob.buy(1.0,10,"1");
auto o2_id = ob.buy(2.0,10,"2");
BOOST_TEST( ob.bidCount()==2);
BOOST_TEST( ob.askCount()==0);
BOOST_TEST( ob.bidIndex(o2_id) == 0, o2_id);
BOOST_TEST( ob.bidIndex(o1_id) == 1, o1_id);
}
BOOST_AUTO_TEST_CASE( insertOrder_buy2 ) {
TestExchange ob;
auto o2_id = ob.buy(2.0,10,"2");
auto o1_id = ob.buy(1.0,10,"1");
// should end up with same ordering
BOOST_TEST( ob.bidCount()==2);
BOOST_TEST( ob.askCount()==0);
BOOST_TEST( ob.bidIndex(o2_id) == 0);
BOOST_TEST( ob.bidIndex(o1_id) == 1);
}
BOOST_AUTO_TEST_CASE( insertOrder_sell ) {
TestExchange ob;
auto o1_id = ob.sell(1.0,10,"1");
auto o2_id = ob.sell(2.0,10,"2");
BOOST_TEST( ob.bidCount()==0);
BOOST_TEST( ob.askCount()==2);
BOOST_TEST( ob.askIndex(o2_id) == 1);
BOOST_TEST( ob.askIndex(o1_id) == 0);
}
BOOST_AUTO_TEST_CASE( insertOrder_sell2 ) {
TestExchange ob;
auto o2_id = ob.sell(2.0,10,"2");
auto o1_id = ob.sell(1.0,10,"1");
BOOST_TEST( ob.bidCount()==0);
BOOST_TEST( ob.askCount()==2);
BOOST_TEST( ob.askIndex(o2_id) == 1);
BOOST_TEST( ob.askIndex(o1_id) == 0);
}
BOOST_AUTO_TEST_CASE( insertOrder_buy_same_price ) {
TestExchange ob;
auto o1_id = ob.buy(1.0,10,"1");
auto o2_id = ob.buy(2.0,10,"2");
auto o3_id = ob.buy(2.0,25,"3");
BOOST_TEST( ob.bidCount()==2);
BOOST_TEST( ob.book().bidOrderIds.size()==3);
BOOST_TEST( ob.askCount()==0);
BOOST_TEST( ob.bidIndex(o2_id) == 0);
BOOST_TEST( ob.bidIndex(o3_id) == 1);
BOOST_TEST( ob.bidIndex(o1_id) == 2);
}
BOOST_AUTO_TEST_CASE( fill ) {
TestListener listener;
TestExchange ob(listener);
auto o1_id = ob.buy(1.0,10,"1");
BOOST_TEST( listener.trades.size()==0);
auto o2_id = ob.sell(0.75,10,"2");
BOOST_TEST( listener.trades.size()==1);
BOOST_TEST( listener.trades[0].aggressor.exchangeId == o2_id);
BOOST_TEST( listener.trades[0].opposite.exchangeId == o1_id);
}
BOOST_AUTO_TEST_CASE( partial_fill ) {
TestListener listener;
TestExchange ob(listener);
auto o1_id = ob.buy(1.0,20,"1");
BOOST_TEST( listener.trades.size()==0);
auto o2_id = ob.sell(.75,10,"2");
BOOST_TEST( listener.trades.size()==1);
BOOST_TEST( listener.trades[0].aggressor.exchangeId == o2_id);
BOOST_TEST( listener.trades[0].opposite.exchangeId == o1_id);
BOOST_TEST( ob.getOrder(o2_id).remainingQuantity() == 0);
BOOST_TEST( ob.getOrder(o1_id).remainingQuantity() == 10);
BOOST_TEST( ob.bidCount() == 1);
BOOST_TEST( ob.askCount() == 0);
}
BOOST_AUTO_TEST_CASE( cancel ) {
TestListener listener;
TestExchange ob(listener);
auto o1_id = ob.buy(1.0,20,"1");
BOOST_TEST(ob.cancel(o1_id)==0);
BOOST_TEST(ob.cancel(o1_id)==-1);
// should be an event for the order and the cancel
BOOST_TEST( listener.orders.size()==2);
BOOST_TEST( ob.bidCount() == 0);
}
BOOST_AUTO_TEST_CASE( market_buy ) {
TestListener listener;
TestExchange ob(listener);
ob.sell(1.0,20,"1");
ob.marketBuy(10,"2");
BOOST_TEST( listener.orders.size()==4);
BOOST_TEST( listener.trades.size()==1);
BOOST_TEST( ob.bidCount() == 0);
BOOST_TEST( ob.askCount() == 1);
}
BOOST_AUTO_TEST_CASE( market_buy_cancel_remaining ) {
TestListener listener;
TestExchange ob(listener);
ob.sell(1.0,20,"1");
ob.marketBuy(30,"2");
BOOST_TEST( listener.orders.size()==5);
BOOST_TEST( listener.trades.size()==1);
BOOST_TEST( ob.bidCount() == 0);
BOOST_TEST( ob.askCount() == 0);
}
BOOST_AUTO_TEST_CASE( market_buy_multi_level ) {
TestListener listener;
TestExchange ob(listener);
ob.sell(1.0,20,"1");
ob.sell(2.0,20,"2");
ob.marketBuy(30,"3");
// initial orders (3) + 2x2 updates due to trades (1 full and 1 partial) = 7 statuses
BOOST_TEST( listener.orders.size()==7);
BOOST_TEST( listener.trades.size()==2);
BOOST_TEST( ob.bidCount() == 0);
BOOST_TEST( ob.askCount() == 1);
BOOST_TEST( ob.book().asks[0].quantity == 10);
}
BOOST_AUTO_TEST_CASE( market_buy_one_sided ) {
TestListener listener;
TestExchange ob(listener);
ob.marketBuy(30,"1");
BOOST_TEST( listener.orders.size()==2);
BOOST_TEST( listener.trades.size()==0);
BOOST_TEST( ob.bidCount() == 0);
BOOST_TEST( ob.askCount() == 0);
}
BOOST_AUTO_TEST_CASE( order_immutability ) {
TestListener listener;
TestExchange ob(listener);
auto o1_id = ob.buy(1.0,30,"1");
Order order = ob.getOrder(o1_id);
ob.sell(1.0,10,"2");
Order latest = ob.getOrder(o1_id);
BOOST_TEST( order.remainingQuantity()==30);
BOOST_TEST( latest.remainingQuantity()==20);
}