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 |
9ae8b9
|
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) { |
JG |
79 |
std::cout << static_cast<json::ErrorMessage*>(d)->as_string() << std::endl; |
|
80 |
} |
6bdd28
|
81 |
delete b; |
JG |
82 |
delete d; |
|
83 |
} |
|
84 |
|
|
85 |
void deleteOrder(std::string& product, std::string& id) |
|
86 |
{ |
e8c910
|
87 |
json::DeleteMessage a(json::DELETE, product, id); |
JG |
88 |
json::Message* b = protocol::deleteOrder(a); |
|
89 |
if (b->type == json::DELETED) { |
6bdd28
|
90 |
std::cout << static_cast<json::DeletedMessage*>(b)->as_string() |
JG |
91 |
<< std::endl; |
|
92 |
} else if (b->type == json::REJECT) { |
|
93 |
std::cout << static_cast<json::RejectMessage*>(b)->as_string() |
|
94 |
<< std::endl; |
9ae8b9
|
95 |
} else if (b->type == json::ERROR) { |
e8c910
|
96 |
std::cout << static_cast<json::ErrorMessage*>(b)->as_string() |
JG |
97 |
<< std::endl; |
|
98 |
} |
6bdd28
|
99 |
} |
JG |
100 |
|
16b655
|
101 |
int main(int argc, char** argv) |
JG |
102 |
{ |
2c515f
|
103 |
int c; |
JG |
104 |
std::string product, id; |
|
105 |
double price; |
6bdd28
|
106 |
clickType click, side; |
2c515f
|
107 |
uint64_t volume; |
9ae8b9
|
108 |
uint64_t times = 1; |
2c515f
|
109 |
initialise(); |
JG |
110 |
if (argc == 1) { |
|
111 |
usage(), exit(0); |
16b655
|
112 |
} |
9ae8b9
|
113 |
while ((c = getopt(argc, argv, "a::t::s::p::v::i::l::")) != -1) { |
2c515f
|
114 |
switch (c) { |
JG |
115 |
case 'a': |
|
116 |
product = std::string(optarg); |
|
117 |
break; |
6bdd28
|
118 |
case 't': |
2c515f
|
119 |
click = mapClick[optarg]; |
JG |
120 |
break; |
e8c910
|
121 |
case 's': |
JG |
122 |
side = mapClick[optarg]; |
|
123 |
break; |
2c515f
|
124 |
case 'p': |
JG |
125 |
price = std::stod(optarg); |
|
126 |
break; |
|
127 |
case 'v': |
|
128 |
volume = std::stoll(optarg); |
|
129 |
break; |
|
130 |
case 'i': |
|
131 |
id = std::string(optarg); |
|
132 |
break; |
9ae8b9
|
133 |
case 'l': |
JG |
134 |
times = std::stoll(optarg); |
|
135 |
break; |
2c515f
|
136 |
case '?': |
e8c910
|
137 |
std::cout << "*1" << std::endl; |
2c515f
|
138 |
default: |
e8c910
|
139 |
std::cout << "*2 " << (char)c << std::endl; |
2c515f
|
140 |
usage(); |
JG |
141 |
exit(0); |
|
142 |
} |
|
143 |
} |
9ae8b9
|
144 |
for (std::uint64_t i = 0; i < times; i++) |
JG |
145 |
{ |
|
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 |
} |