Joel Grunbaum
2022-01-09 b4cf0a2e71ad2c204925b33f9600dc720e25b827
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;
6926be 33     std::ifstream sampleFile("../data.test");
16b655 34     std::stringstream ss;
JG 35     ss << sampleFile.rdbuf();
36     // httplib::Client cli("http://" + server);
37     // auto res = cli.Get("/recover");
38     std::string l;
39     l = ss.str();
b4cf0a 40     std::queue<json::Message*> a(json::parse(l));
16b655 41     while (!a.empty()) {
JG 42     protocol::handleMessage(bs, a.front());
43     delete a.front();
44     a.pop();
45     }
46     return bs;
47 }
48
6926be 49 json::Message* addOrder(json::AddMessage& order)
16b655 50 {
JG 51     std::string message = "{\"message\": " + order.as_string() + ", " +
52                           "\"username\": \"" + std::string(USER) +
53                           "\", \"password\": \"" + std::string(PASS) + "\"}";
6926be 54     return send(message);
16b655 55 }
JG 56
6926be 57 json::Message* deleteOrder(json::DeleteMessage& order)
16b655 58 {
JG 59     std::string message = "{\"message\": " + order.as_string() +
60                           ", \"username\": \"" + std::string(USER) +
61                           "\", \"password\": \"" + std::string(PASS) + "\"}";
6926be 62     return send(message);
16b655 63 }
JG 64
65 void handleMessage(std::unordered_map<std::string, book::Book>& bs,
66                    json::Message* message)
67 {
68     if (mapAnnounce.empty()) initialise();
69     switch (message->type) {
70     case json::FUTURE_TYPE:
71     case json::SPREAD_TYPE:
72     case json::CALL_TYPE:
73     case json::PUT_TYPE:
6926be 74     announce(bs, dynamic_cast<json::AnnounceMessage*>(message));
16b655 75     break;
JG 76     case json::SETTLEMENT:
77     settle(bs, dynamic_cast<json::SettleMessage*>(message));
78     break;
79     case json::ADDED:
80     addedOrder(bs, dynamic_cast<json::AddedMessage*>(message));
81     break;
82     case json::DELETED:
83     deletedOrder(bs, dynamic_cast<json::DeletedMessage*>(message));
84     break;
85     case json::TRADE:
86     tradeOrder(bs, dynamic_cast<json::TradeMessage*>(message));
87     break;
88     case json::BROKER_REQUEST:
89     case json::BROKER_ACK:
90     case json::BROKER_CONFIRM:
91     broker(bs, dynamic_cast<json::Broker*>(message));
92     break;
93     default:;
94     }
95 }
96
97 void announce(std::unordered_map<std::string, book::Book>& bs,
98               json::AnnounceMessage* message)
99 {
100     bs[message->product] =
101     book::Book(mapAnnounce[message->type], message->product,
102                    message->stationId, message->unit, message->expiry,
103                    message->aggFee, message->pasFee, message->broFee);
104 }
105 void settle(std::unordered_map<std::string, book::Book>& bs,
106             json::SettleMessage* message)
107 {
108     bs.erase(message->product);
109 }
110 void addedOrder(std::unordered_map<std::string, book::Book>& bs,
111                 json::AddedMessage* message)
112 {
113     if (message->side == book::Buy) {
114     book::Order t(message->price, book::Buy, message->resting,
115                   message->timestamp, message->id);
116     bs[message->product].bid(t);
117     } else {
118     book::Order t(message->price, book::Sell, message->resting,
119                   message->timestamp, message->id);
120     bs[message->product].ask(t);
121     }
122 }
123 void deletedOrder(std::unordered_map<std::string, book::Book>& bs,
124                   json::DeletedMessage* message)
125 {
126     if (message->side == book::Buy) {
6926be 127         for (std::size_t i = 0; i < bs[message->product].bidSide.size(); i++) {
JG 128         if (bs[message->product].bidSide[i].id == message->id) {
129             bs[message->product].bidSide[i] = bs[message->product].bidSide.back();
130             bs[message->product].bidSide.erase(bs[message->product].bidSide.end());
131             std::make_heap(bs[message->product].bidSide.begin(),
132                            bs[message->product].bidSide.end(),
133                            std::less<book::Level>());
16b655 134         }
JG 135     }
136     } else {
6926be 137         for (std::size_t i = 0; i < bs[message->product].askSide.size(); i++) {
JG 138         if (bs[message->product].askSide[i].id == message->id) {
139             bs[message->product].askSide[i] = bs[message->product].askSide.back();
140             bs[message->product].askSide.erase(bs[message->product].askSide.end());
16b655 141         std::make_heap(bs[message->product].askSide.begin(),
JG 142                        bs[message->product].askSide.end(),
143                        std::greater<book::Level>());
144         }
145     }
146     }
147 }
148 void tradeOrder(std::unordered_map<std::string, book::Book>& bs,
149                 json::TradeMessage* message)
150 {
151     if (message->tradeType == json::BUY_AGGRESSOR) {
152     book::Order t(message->price, book::Buy, message->volume,
153                   message->timestamp, message->passiveOrder);
154     bs[message->product].bid(t);
155     } else {
156     book::Order t(message->price, book::Sell, message->volume,
157                   message->timestamp, message->passiveOrder);
158     bs[message->product].ask(t);
159     }
160 }
161 void broker(std::unordered_map<std::string, book::Book>& bs,
162             json::Broker* message)
163 {
164 }
165
6926be 166 json::Message* send(std::string& message)
16b655 167 {
6926be 168     auto res = cli.Post("/execution", message, "text/plain");
b4cf0a 169     std::queue<json::Message*> a = json::parse(res->body);
JG 170     return a.front();
16b655 171 }
JG 172
173 } // namespace protocol