-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange.h
54 lines (50 loc) · 1.88 KB
/
exchange.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
#pragma once
#include <string>
#include <unordered_map>
#include "order.h"
#include "orderbook.h"
#include "bookmap.h"
#include "spinlock.h"
#include "ordermap.h"
struct ExchangeListener {
/** callback when order properties change */
virtual void onOrder(const Order& order){};
/** callback when trade occurs */
virtual void onTrade(const Trade& trade){};
};
class Exchange : OrderBookListener {
public:
Exchange();
Exchange(ExchangeListener& listener);
/** submit limit buy order. returns exchange order id */
long buy(const std::string& instrument,F price,int quantity,const std::string& orderId);
/** submit market buy order. returns exchange order id. If the order cannot be filled, the rest is cancelled */
long marketBuy(const std::string& instrument,int quantity,const std::string& orderId) {
return buy(instrument,DBL_MAX,quantity,orderId);
}
/** submit limit sell order. returns exchange order id */
long sell(const std::string& instrument,F price,int quantity,const std::string& orderId);
/** submit market sell order. returns exchange order id. If the order cannot be filled, the rest is cancelled */
long marketSell(const std::string& instrument,int quantity,const std::string& orderId) {
return sell(instrument,-DBL_MAX,quantity,orderId);
}
int cancel(long exchangeId);
const Book book(const std::string& instrument);
const Order getOrder(long exchangeId);
void onOrder(const Order& order) override {
listener.onOrder(order);
}
void onTrade(const Trade& trade) override {
listener.onTrade(trade);
}
Guard lock() {
return mu.lock();
}
private:
BookMap books;
OrderMap allOrders;
SpinLock mu;
long nextID();
long insertOrder(const std::string& instrument,F price,int quantity,Side side,const std::string& orderId);
ExchangeListener& listener;
};