commit | author | age
|
16b655
|
1 |
#include "protocol.hpp" |
JG |
2 |
|
|
3 |
#include "book.hpp" |
|
4 |
#include "cpp-httplib/httplib.h" |
|
5 |
#include "json.hpp" |
|
6 |
#include "secrets.hpp" |
|
7 |
|
|
8 |
#include <chrono> |
|
9 |
#include <iostream> |
|
10 |
#include <queue> |
|
11 |
#include <sstream> |
|
12 |
#include <string> |
|
13 |
#include <unordered_map> |
|
14 |
|
|
15 |
namespace protocol |
|
16 |
{ |
|
17 |
static std::unordered_map<json::MessageTypes, book::ProductTypeEnum> |
|
18 |
mapAnnounce; |
|
19 |
std::string server = std::string(HOST) + ":" + std::string(PORT); |
|
20 |
httplib::Client cli("http://" + server); |
|
21 |
|
|
22 |
void initialise() |
|
23 |
{ |
|
24 |
mapAnnounce = {{json::FUTURE_TYPE, book::FUTURE}, |
|
25 |
{json::SPREAD_TYPE, book::SPREAD}, |
|
26 |
{json::CALL_TYPE, book::CALL}, |
|
27 |
{json::PUT_TYPE, book::PUT}}; |
|
28 |
} |
|
29 |
|
|
30 |
std::unordered_map<std::string, book::Book> recoverBook() |
|
31 |
{ |
|
32 |
std::unordered_map<std::string, book::Book> bs; |
|
33 |
std::ifstream sampleFile("../rec.log"); |
|
34 |
std::stringstream ss; |
|
35 |
ss << sampleFile.rdbuf(); |
|
36 |
// httplib::Client cli("http://" + server); |
|
37 |
// auto res = cli.Get("/recover"); |
|
38 |
std::string l; |
|
39 |
l = ss.str(); |
|
40 |
std::queue<json::Message*> a(json::parseMany(l)); |
|
41 |
while (!a.empty()) { |
|
42 |
protocol::handleMessage(bs, a.front()); |
|
43 |
delete a.front(); |
|
44 |
a.pop(); |
|
45 |
} |
|
46 |
return bs; |
|
47 |
} |
|
48 |
|
|
49 |
void addOrder(json::AddMessage& order) |
|
50 |
{ |
|
51 |
std::string message = "{\"message\": " + order.as_string() + ", " + |
|
52 |
"\"username\": \"" + std::string(USER) + |
|
53 |
"\", \"password\": \"" + std::string(PASS) + "\"}"; |
|
54 |
send(message); |
|
55 |
} |
|
56 |
|
|
57 |
void deleteOrder(json::DeleteMessage& order) |
|
58 |
{ |
|
59 |
std::string message = "{\"message\": " + order.as_string() + |
|
60 |
", \"username\": \"" + std::string(USER) + |
|
61 |
"\", \"password\": \"" + std::string(PASS) + "\"}"; |
|
62 |
send(message); |
|
63 |
} |
|
64 |
|
|
65 |
void handleMessage(std::unordered_map<std::string, book::Book>& bs, |
|
66 |
json::Message* message) |
|
67 |
{ |
|
68 |
if (mapAnnounce.empty()) initialise(); |
|
69 |
json::AnnounceMessage* a = dynamic_cast<json::AnnounceMessage*>(message); |
|
70 |
json::SettleMessage* b = dynamic_cast<json::SettleMessage*>(message); |
|
71 |
json::AddedMessage* c = dynamic_cast<json::AddedMessage*>(message); |
|
72 |
json::DeletedMessage* d = dynamic_cast<json::DeletedMessage*>(message); |
|
73 |
json::TradeMessage* e = dynamic_cast<json::TradeMessage*>(message); |
|
74 |
json::Broker* f = dynamic_cast<json::Broker*>(message); |
|
75 |
switch (message->type) { |
|
76 |
case json::FUTURE_TYPE: |
|
77 |
case json::SPREAD_TYPE: |
|
78 |
case json::CALL_TYPE: |
|
79 |
case json::PUT_TYPE: |
|
80 |
announce(bs, a); |
|
81 |
break; |
|
82 |
case json::SETTLEMENT: |
|
83 |
settle(bs, dynamic_cast<json::SettleMessage*>(message)); |
|
84 |
break; |
|
85 |
case json::ADDED: |
|
86 |
addedOrder(bs, dynamic_cast<json::AddedMessage*>(message)); |
|
87 |
break; |
|
88 |
case json::DELETED: |
|
89 |
deletedOrder(bs, dynamic_cast<json::DeletedMessage*>(message)); |
|
90 |
break; |
|
91 |
case json::TRADE: |
|
92 |
tradeOrder(bs, dynamic_cast<json::TradeMessage*>(message)); |
|
93 |
break; |
|
94 |
case json::BROKER_REQUEST: |
|
95 |
case json::BROKER_ACK: |
|
96 |
case json::BROKER_CONFIRM: |
|
97 |
broker(bs, dynamic_cast<json::Broker*>(message)); |
|
98 |
break; |
|
99 |
default:; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
void announce(std::unordered_map<std::string, book::Book>& bs, |
|
104 |
json::AnnounceMessage* message) |
|
105 |
{ |
|
106 |
bs[message->product] = |
|
107 |
book::Book(mapAnnounce[message->type], message->product, |
|
108 |
message->stationId, message->unit, message->expiry, |
|
109 |
message->aggFee, message->pasFee, message->broFee); |
|
110 |
} |
|
111 |
void settle(std::unordered_map<std::string, book::Book>& bs, |
|
112 |
json::SettleMessage* message) |
|
113 |
{ |
|
114 |
bs.erase(message->product); |
|
115 |
} |
|
116 |
void addedOrder(std::unordered_map<std::string, book::Book>& bs, |
|
117 |
json::AddedMessage* message) |
|
118 |
{ |
|
119 |
if (message->side == book::Buy) { |
|
120 |
book::Order t(message->price, book::Buy, message->resting, |
|
121 |
message->timestamp, message->id); |
|
122 |
bs[message->product].bid(t); |
|
123 |
} else { |
|
124 |
book::Order t(message->price, book::Sell, message->resting, |
|
125 |
message->timestamp, message->id); |
|
126 |
bs[message->product].ask(t); |
|
127 |
} |
|
128 |
} |
|
129 |
void deletedOrder(std::unordered_map<std::string, book::Book>& bs, |
|
130 |
json::DeletedMessage* message) |
|
131 |
{ |
|
132 |
if (message->side == book::Buy) { |
|
133 |
for (auto i = bs[message->product].bidSide.begin(); |
|
134 |
i != bs[message->product].bidSide.end(); i++) { |
|
135 |
if (i->id == message->id) { |
|
136 |
bs[message->product].bidSide.erase(i); |
|
137 |
std::make_heap(bs[message->product].bidSide.begin(), |
|
138 |
bs[message->product].bidSide.end(), |
|
139 |
std::less<book::Level>()); |
|
140 |
} |
|
141 |
} |
|
142 |
} else { |
|
143 |
for (auto i = bs[message->product].askSide.begin(); |
|
144 |
i != bs[message->product].askSide.end(); i++) { |
|
145 |
if (i->id == message->id) { |
|
146 |
bs[message->product].askSide.erase(i); |
|
147 |
std::make_heap(bs[message->product].askSide.begin(), |
|
148 |
bs[message->product].askSide.end(), |
|
149 |
std::greater<book::Level>()); |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
void tradeOrder(std::unordered_map<std::string, book::Book>& bs, |
|
155 |
json::TradeMessage* message) |
|
156 |
{ |
|
157 |
if (message->tradeType == json::BUY_AGGRESSOR) { |
|
158 |
book::Order t(message->price, book::Buy, message->volume, |
|
159 |
message->timestamp, message->passiveOrder); |
|
160 |
bs[message->product].bid(t); |
|
161 |
} else { |
|
162 |
book::Order t(message->price, book::Sell, message->volume, |
|
163 |
message->timestamp, message->passiveOrder); |
|
164 |
bs[message->product].ask(t); |
|
165 |
} |
|
166 |
} |
|
167 |
void broker(std::unordered_map<std::string, book::Book>& bs, |
|
168 |
json::Broker* message) |
|
169 |
{ |
|
170 |
} |
|
171 |
|
|
172 |
void send(std::string& message) |
|
173 |
{ |
|
174 |
cli.Post("/execution", message, "text/plain"); |
|
175 |
} |
|
176 |
|
|
177 |
} // namespace protocol |