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