-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-trader.cpp
137 lines (128 loc) · 6.06 KB
/
cpp-trader.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
#include <unistd.h>
#include <cfloat>
#include <csignal>
#include "fix_engine.h"
#include "exchange.h"
#include "msg_massquote.h"
#include "msg_orders.h"
class MyServer : public Acceptor<> , public ExchangeListener {
Exchange exchange;
public:
MyServer() : Acceptor(9000,DefaultSessionConfig("SERVER","*"),std::max(int(std::thread::hardware_concurrency()/2),1)), exchange(*this){};
void onMessage(Session<>& session,const FixMessage& msg) override {
auto msgType = msg.msgType();
if(msgType==MassQuote::msgType) {
exchange.quote(session.id(),msg.getString(Tag::SYMBOL),msg.getFixed(132),msg.getInt(134),msg.getFixed(133),msg.getInt(135),msg.getString(302));
FixBuilder fix(256);
MassQuoteAck::build(fix,msg.getString(117),0);
session.sendMessage(MassQuoteAck::msgType,fix);
} else if(msgType==NewOrderSingle::msgType) {
auto side = msg.getInt(Tag::SIDE)==1 ? Order::BUY : Order::SELL;
auto orderType = msg.getInt(Tag::ORD_TYPE)==1 ? OrderType::Market : OrderType::Limit;
auto price = orderType==OrderType::Market ? side == Order::BUY ? DBL_MAX : -DBL_MAX : msg.getFixed(Tag::PRICE);
std::cout << "new order " << msg.getString(Tag::CLORDID) << " " << side << " " << orderType << " "<< msg.getString(Tag::SYMBOL) << " " << price << " " << msg.getInt(Tag::ORDER_QTY) << "\n";
if (side == Order::BUY) {
exchange.buy(session.id(),msg.getString(Tag::SYMBOL),price,msg.getInt(Tag::ORDER_QTY),msg.getString(Tag::CLORDID));
} else {
exchange.sell(session.id(),msg.getString(Tag::SYMBOL),price,msg.getInt(Tag::ORDER_QTY),msg.getString(Tag::CLORDID));
}
} else if(msgType==OrderCancelRequest::msgType) {
auto exchangeId = msg.getLong(37);
std::cout << "request to cancel order, exchange id "<< exchangeId <<"\n";
int status = 0;
try {
status = exchange.cancel(exchangeId,session.id());
} catch (std::exception& e) {
std::cerr << "error cancelling order " << e.what() << "\n";
status = -1;
}
FixBuilder fix(256);
if(status!=0) {
std::cout << "order " << exchangeId << " not found\n";
OrderCancelReject::build(fix,exchangeId,msg.getString(Tag::CLORDID), OrderStatus::Rejected);
session.sendMessage(OrderCancelReject::msgType,fix);
}
}
}
/** callback when order status changes */
virtual void onOrder(const Order& order) override {
if(order.isQuote()) {
return;
}
FixBuilder fix(256);
auto execType = ExecType::Status;
auto orderStatus = order.isCancelled() ? OrderStatus::Canceled : order.isFilled() ? OrderStatus::Filled : order.cumulativeQuantity()==0 ? OrderStatus::New : OrderStatus::PartiallyFilled;
auto orderSide = order.side==Order::BUY ? OrderSide::Buy : OrderSide::Sell;
ExecutionReport::build<7>(fix,order.orderId(), order.instrument, orderSide, 0, 0, order.cumulativeQuantity(), order.averagePrice(), order.remainingQuantity(),order.exchangeId,execType,0,orderStatus);
try {
// session may be disconnected
sendMessage(order.sessionId(),ExecutionReport::msgType,fix);
} catch (std::exception& e) {
std::cerr << "error sending message " << e.what() << "\n";
}
};
/** callback when trade occurs */
virtual void onTrade(const Trade& trade) override {
FixBuilder fix(256);
for (auto order : {trade.aggressor,trade.opposite}) {
auto execType = order.remainingQuantity()==0 ? ExecType::Filled : ExecType::PartiallyFilled;
auto orderStatus = order.remainingQuantity()==0 ? OrderStatus::Filled : OrderStatus::PartiallyFilled;
auto orderSide = order.side==Order::BUY ? OrderSide::Buy : OrderSide::Sell;
ExecutionReport::build<7>(fix,order.orderId(), order.instrument, orderSide, trade.price, trade.quantity, order.cumulativeQuantity(), order.averagePrice(), order.remainingQuantity(),order.exchangeId,execType,trade.execId,orderStatus);
try {
// session may be disconnected
sendMessage(order.sessionId(),ExecutionReport::msgType,fix);
} catch (std::exception& e) {
std::cerr << "error sending message " << e.what() << "\n";
}
}
};
bool validateLogon(const FixMessage& msg) override {
return true;
}
void onLoggedOn(const Session<>& session) override {
std::cout << "logon " << session.id() << "\n";
Acceptor::onLoggedOn(session);
}
void dumpBooks() {
std::cout << "dumping books...\n";
auto instruments = exchange.instruments();
for(auto& instrument : instruments) {
auto book = exchange.book(instrument);
std::cout << "Book " << instrument << ":\n";
std::cout << book;
}
}
void onDisconnected(const Session<>& session) override {
std::cout << "disconnected " << session.id() << "\n";
for(auto order : exchange.orders()) {
if(order->sessionId()==session.id()) {
if(order->isActive()) {
exchange.cancel(order->exchangeId,session.id());
}
}
}
Acceptor::onDisconnected(session);
}
};
static std::atomic<bool> displayBooks = false;
int main(int argc, char* argv[]) {
MyServer server;
auto thread = std::thread([&](){
server.listen();
});
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "send USR1 to " << getpid() << " to display books\n";
struct sigaction sa;
sa.sa_handler = [](int){ displayBooks = true; };
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, nullptr);
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
if(displayBooks) {
displayBooks = false;
server.dumpBooks();
}
}
}