Joel Grunbaum
2022-01-10 6bdd28a09c589cf631fce948476d48e9375f72a0
commit | author | age
16b655 1 #include "book.hpp"
JG 2 #include "json.hpp"
3 #include "protocol.hpp"
4
5 #include <cstdlib>
6 #include <getopt.h>
7 #include <iostream>
8 #include <unordered_map>
9
6bdd28 10 enum clickType { Buy, Sell, Flash, Delete };
16b655 11
JG 12 static std::unordered_map<std::string, clickType> mapClick;
13
14 void initialise()
15 {
2c515f 16     mapClick = {{"BUY", Buy}, {"SELL", Sell}, {"FLASH", Flash},
JG 17                 {"b", Buy},   {"s", Sell},    {"f", Flash},
6bdd28 18                 {"B", Buy},   {"S", Sell},    {"F", Flash},
JG 19                 {"DELETE", Delete}, {"D", Delete}, {"d", Delete}};
16b655 20 }
JG 21
22 void usage()
23 {
2c515f 24     std::cout << "DESCRIPTION" << std::endl
JG 25               << "Click trader using same algs" << std::endl
26               << std::endl
27               << "USAGE" << std::endl
28               << "\t-a product" << std::endl
6bdd28 29               << "\t-t click type (buy, sell, flash, delete)" << std::endl
2c515f 30               << "\t-p price" << std::endl
JG 31               << "\t-v volume" << std::endl
32               << "\t-i id" << std::endl
33               << "Always need product, need side, price and volume for "
34                  "adding/flash, need id for deleting"
35               << std::endl;
16b655 36 }
JG 37
6bdd28 38 void buy(std::string& product, double price, uint64_t volume)
JG 39 {
40     json::AddMessage a(json::ADD, product, price, book::Buy, volume);
41     json::AddedMessage* b = static_cast<json::AddedMessage*>(protocol::addOrder(a));
42     std::cout << b->as_string() << std::endl;
43 }
44
45 void sell(std::string& product, double price, uint64_t volume)
46 {
47     json::AddMessage a(json::ADD, product, price, book::Sell, volume);
48     json::AddedMessage* b = static_cast<json::AddedMessage*>(protocol::addOrder(a));
49     std::cout << b->as_string() << std::endl;
50     delete b;
51 }
52
53 void flash(std::string& product, double price, uint64_t volume, book::OrderSideEnum side)
54 {
55     json::AddMessage a(json::ADD, product, price, side, volume);
56     json::Message* b = static_cast<json::Message*>(protocol::addOrder(a));
57     if (b->type == json::ERROR) {
58         std::cout << static_cast<json::ErrorMessage*>(b)->as_string() << std::endl;
59         return;
60     }
61     json::DeleteMessage c(json::DELETE, product, static_cast<json::AddedMessage*>(b)->id);
62     json::Message* d = protocol::deleteOrder(c);
63     std::cout << static_cast<json::AddedMessage*>(b)->as_string() << std::endl;
64     if (d->type == json::DELETED) {
65         std::cout << static_cast<json::DeletedMessage*>(d)->as_string()
66                   << std::endl;
67     } else {
68         std::cout << static_cast<json::RejectMessage*>(d)->as_string()
69                   << std::endl;
70     }
71     delete b;
72     delete d;
73 }
74
75 void deleteOrder(std::string& product, std::string& id)
76 {
77     json::DeleteMessage a(json::DELETE, product, id);
78     json::Message* b = protocol::deleteOrder(a);
79     if (b->type == json::DELETED) {
80         std::cout << static_cast<json::DeletedMessage*>(b)->as_string()
81                   << std::endl;
82     } else if (b->type == json::REJECT) {
83         std::cout << static_cast<json::RejectMessage*>(b)->as_string()
84                   << std::endl;
85     } else {
86         std::cout << static_cast<json::ErrorMessage*>(b)->as_string() << std::endl;
87     }
88 }
89
16b655 90 int main(int argc, char** argv)
JG 91 {
2c515f 92     int c;
JG 93     std::string product, id;
94     double price;
6bdd28 95     clickType click, side;
2c515f 96     uint64_t volume;
JG 97     initialise();
98     if (argc == 1) {
99         usage(), exit(0);
16b655 100     }
6bdd28 101     while ((c = getopt(argc, argv, "a::t::s::p::v::i::")) != -1) {
2c515f 102         switch (c) {
JG 103         case 'a':
104             product = std::string(optarg);
105             break;
6bdd28 106         case 't':
2c515f 107             click = mapClick[optarg];
JG 108             break;
6bdd28 109         case 's':
JG 110             side = mapClick[optarg];
111             break;
2c515f 112         case 'p':
JG 113             price = std::stod(optarg);
114             break;
115         case 'v':
116             volume = std::stoll(optarg);
117             break;
118         case 'i':
119             id = std::string(optarg);
120             break;
121         case '?':
6bdd28 122             std::cout << "*1" << std::endl;
2c515f 123         default:
6bdd28 124             std::cout << "*2 " << (char) c << std::endl;
2c515f 125             usage();
JG 126             exit(0);
127         }
128     }
129     switch (click) {
130     case Buy:
6bdd28 131         buy(product, price, volume);
JG 132         break;
2c515f 133     case Sell:
6bdd28 134         sell(product, price, volume);
JG 135         break;
136     case Flash:
137         if (side == clickType::Buy)
138             flash(product, price, volume, book::Buy);
139         else
140             flash(product, price, volume, book::Sell);
141         break;
142     case Delete:
143         deleteOrder(product, id);
144         break;
2c515f 145     }
16b655 146 }