Joel Grunbaum
2022-01-10 6bdd28a09c589cf631fce948476d48e9375f72a0
commit | author | age
16b655 1 #pragma once
JG 2
3 #include "book.hpp"
4
5 #include <chrono>
6 #include <cstdint>
7 #include <ostream>
8 #include <queue>
9 #include <unordered_map>
10
11 namespace json
12 {
13 enum MessageTypes {
2c515f 14     FUTURE_TYPE,
JG 15     SPREAD_TYPE,
16     CALL_TYPE,
17     PUT_TYPE,
18     SETTLEMENT,
19     ADD,
20     ADDED,
21     DELETE,
22     DELETED,
23     REJECT,
24     TRADE,
25     BROKER_REQUEST,
26     BROKER_ACK,
27     BROKER_CONFIRM,
6bdd28 28     ERROR,
2c515f 29     NONE
16b655 30 };
JG 31
32 enum TradeTypeEnum { BUY_AGGRESSOR, SELL_AGGRESSOR };
33
34 struct Message {
2c515f 35     MessageTypes type;
JG 36     std::string product;
37     Message(MessageTypes type, std::string product);
38     Message();
39     virtual ~Message() = default;
6bdd28 40 };
JG 41
42 struct ErrorMessage : public Message {
43     std::string message;
44     ErrorMessage(std::string message);
45     std::string as_string();
16b655 46 };
JG 47
48 struct FromExchange : public Message {
2c515f 49     uint64_t sequence;
JG 50     double timestamp;
51     FromExchange(MessageTypes type, std::string product, uint64_t sequence,
52                  double timestamp);
53     virtual ~FromExchange() = default;
16b655 54 };
JG 55
56 struct ToExchange : public Message {
2c515f 57     ToExchange(MessageTypes type, std::string product);
JG 58     virtual ~ToExchange() = default;
16b655 59 };
JG 60
61 struct Broker : public Message {
2c515f 62     double price;
JG 63     book::OrderSideEnum side;
64     uint64_t volume;
65     std::string counterparty;
66     Broker(MessageTypes type, std::string product, double price,
67            book::OrderSideEnum side, uint64_t volume, std::string counterparty);
68     virtual ~Broker() = default;
16b655 69 };
JG 70
71 struct AnnounceMessage : public FromExchange {
2c515f 72     int stationId;
JG 73     std::string stationName;
74     std::string unit;
75     std::chrono::nanoseconds expiry;
76     double aggFee;
77     double pasFee;
78     double broFee;
16b655 79
2c515f 80     AnnounceMessage(MessageTypes type, std::string product, int stationId,
JG 81                     std::string stationName, std::string unit,
82                     std::chrono::nanoseconds expiry, double aggFee,
83                     double pasFee, double broFee, uint64_t sequence,
84                     double timestamp);
16b655 85 };
JG 86
87 struct SettleMessage : public FromExchange {
2c515f 88     std::string stationName;
JG 89     std::chrono::nanoseconds expiry;
90     double price;
91     SettleMessage(MessageTypes type, std::string product,
92                   std::string stationName, std::chrono::nanoseconds expiry,
93                   double price, uint64_t sequence, double timestamp);
16b655 94 };
JG 95
96 struct AddMessage : public ToExchange {
2c515f 97     double price;
JG 98     book::OrderSideEnum side;
99     uint64_t volume;
100     AddMessage(MessageTypes type, std::string product, double price,
101                book::OrderSideEnum side, uint64_t volume);
102     std::string as_string();
16b655 103 };
JG 104
105 struct AddedMessage : public FromExchange {
2c515f 106     std::string id;
JG 107     book::OrderSideEnum side;
108     double price;
109     uint64_t filled;
110     uint64_t resting;
111     AddedMessage(MessageTypes type, std::string product, std::string id,
112                  book::OrderSideEnum side, double price, uint64_t filled,
113                  uint64_t resting, uint64_t sequence, double timestamp);
6bdd28 114     std::string as_string();
16b655 115 };
JG 116
117 struct DeleteMessage : public ToExchange {
2c515f 118     std::string id;
JG 119     DeleteMessage(MessageTypes type, std::string product, std::string id);
120     std::string as_string();
16b655 121 };
JG 122
123 struct DeletedMessage : public FromExchange {
2c515f 124     std::string id;
JG 125     book::OrderSideEnum side;
126     DeletedMessage(MessageTypes type, std::string product, std::string id,
127                    book::OrderSideEnum side, uint64_t sequence,
128                    double timestamp);
6bdd28 129     std::string as_string();
16b655 130 };
JG 131
132 struct RejectMessage : public FromExchange {
2c515f 133     std::string error;
JG 134     RejectMessage(MessageTypes type, std::string product, std::string error,
135                   uint64_t sequence, double timestamp);
6bdd28 136     std::string as_string();
16b655 137 };
JG 138
139 struct TradeMessage : public FromExchange {
2c515f 140     double price;
JG 141     uint64_t volume;
142     std::string buyer;
143     std::string seller;
144     TradeTypeEnum tradeType;
145     std::string passiveOrder;
146     uint64_t passiveOrderRemaining;
147     TradeMessage(MessageTypes type, std::string product, double price,
148                  uint64_t volume, std::string buyer, std::string seller,
149                  TradeTypeEnum tradeType, std::string passiveOrder,
150                  uint64_t passiveOrderRemaining, uint64_t sequence,
151                  double timestamp);
16b655 152 };
JG 153
154 struct BrokerRequest : public Broker {
2c515f 155     BrokerRequest(MessageTypes type, std::string product, double price,
JG 156                   book::OrderSideEnum side, uint64_t volume,
157                   std::string counterparty);
16b655 158 };
JG 159
160 struct BrokerAck : public Broker {
2c515f 161     std::string id;
JG 162     std::string brokerTradeStatus;
163     std::string owner;
164     BrokerAck(MessageTypes type, std::string product, double price,
165               book::OrderSideEnum side, uint64_t volume,
166               std::string counterparty, std::string id,
167               std::string brokerTradeStatus, std::string owner);
16b655 168 };
JG 169
170 struct BrokerConfirm : public Broker {
2c515f 171     std::string id;
JG 172     BrokerConfirm(MessageTypes type, std::string product, double price,
173                   book::OrderSideEnum side, uint64_t volume,
174                   std::string counterparty, std::string id);
16b655 175 };
JG 176
b4cf0a 177 std::queue<Message*> parse(std::string& str);
16b655 178 } // namespace json