-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexchange.cpp
79 lines (65 loc) · 3.22 KB
/
exchange.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
#include "exchange.h"
#include "orderbook.h"
#include <stdexcept>
#include <string>
#define LOCK_EXCHANGE() std::lock_guard<std::mutex> lock(mu)
#define UNLOCK_EXCHANGE() lock.
static ExchangeListener dummy;
Exchange::Exchange(ExchangeListener& listener) : listener(listener) {}
Exchange::Exchange() : listener(dummy) {}
const Order Exchange::getOrder(long exchangeId) {
OrderBook* book;
Order* order = allOrders.get(exchangeId);
if(!order) throw std::runtime_error("invalid exchange order id "+std::to_string(exchangeId));
book = books.get(order->instrument);
if(!book) throw std::runtime_error("missing book for order id"+std::to_string(exchangeId));
auto bookGuard = book->lock();
return book->getOrder(order);
}
const Book Exchange::book(const std::string& instrument) {
OrderBook* book;
book = books.getOrCreate(instrument,*this);
auto bookGuard = book->lock();
return book->book();
}
int Exchange::cancel(long exchangeId,const std::string& sessionId) {
OrderBook* book;
Order* order = allOrders.get(exchangeId);
if(!order) throw std::runtime_error("invalid exchange order id "+std::to_string(exchangeId));
if(order->sessionId()!=sessionId) throw std::runtime_error("invalid session id "+sessionId);
book = books.get(order->instrument);
if(!book) throw std::runtime_error("missing book for order id"+std::to_string(exchangeId));
auto bookGuard = book->lock();
return book->cancelOrder(order);
}
long Exchange::insertOrder(const std::string& sessionId,const std::string_view& instrument,F price,int quantity,Order::Side side,const std::string_view& orderId) {
OrderBook *book = books.getOrCreate(instrument,*this);
auto bookGuard = book->lock();
long id = nextID();
Order *order = new (book->allocateOrder()) Order(sessionId,std::string(orderId),book->instrument,price,quantity,side,id);
allOrders.add(order);
book->insertOrder(order);
return id;
}
long Exchange::buy(const std::string& sessionId,const std::string_view& instrument,F price,int quantity,const std::string_view& orderId) {
return insertOrder(sessionId,instrument,price,quantity,Order::BUY,orderId);
}
long Exchange::sell(const std::string& sessionId,const std::string_view& instrument,F price,int quantity,const std::string_view& orderId) {
return insertOrder(sessionId,instrument,price,quantity,Order::SELL,orderId);
}
void Exchange::quote(const std::string& sessionId,const std::string_view& instrument,F bidPrice,int bidQuantity,F askPrice,int askQuantity,const std::string_view& quoteId) {
OrderBook *book = books.getOrCreate(instrument,*this);
auto bookGuard = book->lock();
auto orders = book->getQuotes(sessionId,std::string(quoteId),[&]() -> QuoteOrders{
auto bid = new (book->allocateOrder()) Order(sessionId,std::string(quoteId),book->instrument,bidPrice,bidQuantity,Order::BUY,nextID());
auto ask = new (book->allocateOrder()) Order(sessionId,std::string(quoteId),book->instrument,askPrice,askQuantity,Order::SELL,nextID());
allOrders.add(bid);
allOrders.add(ask);
return {bid,ask};
});
book->quote(orders,bidPrice,bidQuantity,askPrice,askQuantity);
}
long Exchange::nextID() {
static std::atomic<long> id = 0;
return ++id;
}