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 |
{ |
e8c910
|
16 |
mapClick = {{"BUY", Buy}, {"SELL", Sell}, {"FLASH", Flash}, |
JG |
17 |
{"b", Buy}, {"s", Sell}, {"f", Flash}, |
|
18 |
{"B", Buy}, {"S", Sell}, {"F", Flash}, |
|
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); |
e8c910
|
41 |
json::AddedMessage* b = |
JG |
42 |
static_cast<json::AddedMessage*>(protocol::addOrder(a)); |
6bdd28
|
43 |
std::cout << b->as_string() << std::endl; |
JG |
44 |
} |
|
45 |
|
|
46 |
void sell(std::string& product, double price, uint64_t volume) |
|
47 |
{ |
|
48 |
json::AddMessage a(json::ADD, product, price, book::Sell, volume); |
e8c910
|
49 |
json::AddedMessage* b = |
JG |
50 |
static_cast<json::AddedMessage*>(protocol::addOrder(a)); |
6bdd28
|
51 |
std::cout << b->as_string() << std::endl; |
JG |
52 |
delete b; |
|
53 |
} |
|
54 |
|
e8c910
|
55 |
void flash(std::string& product, double price, uint64_t volume, |
JG |
56 |
book::OrderSideEnum side) |
6bdd28
|
57 |
{ |
JG |
58 |
json::AddMessage a(json::ADD, product, price, side, volume); |
|
59 |
json::Message* b = static_cast<json::Message*>(protocol::addOrder(a)); |
e8c910
|
60 |
if (b->type == json::ERROR) { |
JG |
61 |
std::cout << static_cast<json::ErrorMessage*>(b)->as_string() |
|
62 |
<< std::endl; |
|
63 |
return; |
|
64 |
} |
|
65 |
json::DeleteMessage c(json::DELETE, product, |
|
66 |
static_cast<json::AddedMessage*>(b)->id); |
6bdd28
|
67 |
json::Message* d = protocol::deleteOrder(c); |
JG |
68 |
std::cout << static_cast<json::AddedMessage*>(b)->as_string() << std::endl; |
|
69 |
if (d->type == json::DELETED) { |
|
70 |
std::cout << static_cast<json::DeletedMessage*>(d)->as_string() |
|
71 |
<< std::endl; |
|
72 |
} else { |
|
73 |
std::cout << static_cast<json::RejectMessage*>(d)->as_string() |
|
74 |
<< std::endl; |
|
75 |
} |
|
76 |
delete b; |
|
77 |
delete d; |
|
78 |
} |
|
79 |
|
|
80 |
void deleteOrder(std::string& product, std::string& id) |
|
81 |
{ |
e8c910
|
82 |
json::DeleteMessage a(json::DELETE, product, id); |
JG |
83 |
json::Message* b = protocol::deleteOrder(a); |
|
84 |
if (b->type == json::DELETED) { |
6bdd28
|
85 |
std::cout << static_cast<json::DeletedMessage*>(b)->as_string() |
JG |
86 |
<< std::endl; |
|
87 |
} else if (b->type == json::REJECT) { |
|
88 |
std::cout << static_cast<json::RejectMessage*>(b)->as_string() |
|
89 |
<< std::endl; |
|
90 |
} else { |
e8c910
|
91 |
std::cout << static_cast<json::ErrorMessage*>(b)->as_string() |
JG |
92 |
<< std::endl; |
|
93 |
} |
6bdd28
|
94 |
} |
JG |
95 |
|
16b655
|
96 |
int main(int argc, char** argv) |
JG |
97 |
{ |
2c515f
|
98 |
int c; |
JG |
99 |
std::string product, id; |
|
100 |
double price; |
6bdd28
|
101 |
clickType click, side; |
2c515f
|
102 |
uint64_t volume; |
JG |
103 |
initialise(); |
|
104 |
if (argc == 1) { |
|
105 |
usage(), exit(0); |
16b655
|
106 |
} |
6bdd28
|
107 |
while ((c = getopt(argc, argv, "a::t::s::p::v::i::")) != -1) { |
2c515f
|
108 |
switch (c) { |
JG |
109 |
case 'a': |
|
110 |
product = std::string(optarg); |
|
111 |
break; |
6bdd28
|
112 |
case 't': |
2c515f
|
113 |
click = mapClick[optarg]; |
JG |
114 |
break; |
e8c910
|
115 |
case 's': |
JG |
116 |
side = mapClick[optarg]; |
|
117 |
break; |
2c515f
|
118 |
case 'p': |
JG |
119 |
price = std::stod(optarg); |
|
120 |
break; |
|
121 |
case 'v': |
|
122 |
volume = std::stoll(optarg); |
|
123 |
break; |
|
124 |
case 'i': |
|
125 |
id = std::string(optarg); |
|
126 |
break; |
|
127 |
case '?': |
e8c910
|
128 |
std::cout << "*1" << std::endl; |
2c515f
|
129 |
default: |
e8c910
|
130 |
std::cout << "*2 " << (char)c << std::endl; |
2c515f
|
131 |
usage(); |
JG |
132 |
exit(0); |
|
133 |
} |
|
134 |
} |
|
135 |
switch (click) { |
|
136 |
case Buy: |
6bdd28
|
137 |
buy(product, price, volume); |
JG |
138 |
break; |
2c515f
|
139 |
case Sell: |
6bdd28
|
140 |
sell(product, price, volume); |
JG |
141 |
break; |
|
142 |
case Flash: |
e8c910
|
143 |
if (side == clickType::Buy) |
JG |
144 |
flash(product, price, volume, book::Buy); |
|
145 |
else |
|
146 |
flash(product, price, volume, book::Sell); |
6bdd28
|
147 |
break; |
e8c910
|
148 |
case Delete: |
JG |
149 |
deleteOrder(product, id); |
|
150 |
break; |
2c515f
|
151 |
} |
16b655
|
152 |
} |