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