Joel Grunbaum
2022-01-19 0bef191acd5a77544852e6a3daae2df05bd34a31
Reformat and change send to x-www-form-urlencoded
10 files modified
321 ■■■■ changed files
book.cpp 16 ●●●● patch | view | raw | blame | history
bot.cpp 17 ●●●●● patch | view | raw | blame | history
click.cpp 54 ●●●● patch | view | raw | blame | history
json.cpp 8 ●●●● patch | view | raw | blame | history
json.hpp 9 ●●●●● patch | view | raw | blame | history
protocol.cpp 99 ●●●● patch | view | raw | blame | history
strat.cpp 46 ●●●●● patch | view | raw | blame | history
strat.hpp 8 ●●●● patch | view | raw | blame | history
test.cpp 31 ●●●● patch | view | raw | blame | history
test.py 33 ●●●●● patch | view | raw | blame | history
book.cpp
@@ -71,7 +71,7 @@
std::ostream& operator<<(std::ostream& out, const Level& a)
{
    return out << "Price: " << a.price << ", volume: " << a.volume
               << ", time: " << a.timestamp << ", id: " << a.id;
               << ", time: " << a.timestamp << ", id: " << a.id;
}
Book::Book()
@@ -92,22 +92,22 @@
void Book::ask(Order& order)
{
    Level a(order);
    auto b = std::lower_bound(this->askSide.begin(), this->askSide.end(), a);
    this->askSide.insert(b, a);
    Level a(order);
    auto b = std::lower_bound(this->askSide.begin(), this->askSide.end(), a);
    this->askSide.insert(b, a);
}
void Book::bid(Order& order)
{
    Level a(order);
    auto b = std::upper_bound(this->bidSide.begin(), this->bidSide.end(), a);
    this->bidSide.insert(b, a);
    Level a(order);
    auto b = std::upper_bound(this->bidSide.begin(), this->bidSide.end(), a);
    this->bidSide.insert(b, a);
}
void Book::printBook(std::size_t numOrders)
{
    std::cout << "Sell side: " << this->askSide.size() << std::endl;
    std::size_t count = 0;
    std::size_t count = 0;
    for (auto i = this->askSide.rbegin(); i != this->askSide.rend(); i++) {
        std::cout << *i << std::endl;
        count++;
bot.cpp
@@ -5,19 +5,16 @@
void signalHandler(int signum)
{
    strat::destroy();
    std::exit(0);
    strat::destroy();
    std::exit(0);
}
void no(int signum)
{
    std::cout << "no." << std::endl;
}
void no(int signum) { std::cout << "no." << std::endl; }
int main(void)
{
    signal(SIGINT, signalHandler);
    // signal(SIGTERM, no);
    strat::initialise();
    strat::mainLoop();
    signal(SIGINT, signalHandler);
    // signal(SIGTERM, no);
    strat::initialise();
    strat::mainLoop();
}
click.cpp
@@ -32,7 +32,7 @@
              << "\t-p price" << std::endl
              << "\t-v volume" << std::endl
              << "\t-i id" << std::endl
              << "\t-l loop count (default to 1)" << std::endl
              << "\t-l loop count (default to 1)" << std::endl
              << "Always need product, need side, price and volume for "
                 "adding/flash, need id for deleting"
              << std::endl;
@@ -76,8 +76,9 @@
        std::cout << static_cast<json::RejectMessage*>(d)->as_string()
                  << std::endl;
    } else if (d->type == json::ERROR) {
        std::cout << static_cast<json::ErrorMessage*>(d)->as_string() << std::endl;
    }
        std::cout << static_cast<json::ErrorMessage*>(d)->as_string()
                  << std::endl;
    }
    delete b;
    delete d;
}
@@ -105,7 +106,7 @@
    double price;
    clickType click, side;
    uint64_t volume;
    uint64_t times = 1;
    uint64_t times = 1;
    initialise();
    if (argc == 1) {
        usage(), exit(0);
@@ -130,9 +131,9 @@
        case 'i':
            id = std::string(optarg);
            break;
        case 'l':
            times = std::stoll(optarg);
            break;
        case 'l':
            times = std::stoll(optarg);
            break;
        case '?':
            std::cout << "*1" << std::endl;
        default:
@@ -141,24 +142,23 @@
            exit(0);
        }
    }
    for (std::uint64_t i = 0; i < times; i++)
    {
        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;
        }
    }
    for (std::uint64_t i = 0; i < times; i++) {
        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;
        }
    }
}
json.cpp
@@ -192,7 +192,7 @@
RejectMessage* reject(rapidjson::Value& d)
{
    return new RejectMessage(mapTypes[d["type"].GetString()], "",
    return new RejectMessage(mapTypes[d["type"].GetString()], "",
                             d["error"].GetString(), uint64_t(0), double(0));
}
@@ -393,9 +393,9 @@
std::string RejectMessage::as_string()
{
    return "{\"type\": \"REJECT\", \"product\": \"" + this->product +
               "\", \"error\": \"" + this->error +
               "\", \"sequence\": " + std::to_string(this->sequence) +
               ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
           "\", \"error\": \"" + this->error +
           "\", \"sequence\": " + std::to_string(this->sequence) +
           ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
}
TradeMessage::TradeMessage(MessageTypes type, std::string product, double price,
json.hpp
@@ -82,7 +82,7 @@
                    std::chrono::nanoseconds expiry, double aggFee,
                    double pasFee, double broFee, uint64_t sequence,
                    double timestamp);
    std::string as_string();
    std::string as_string();
};
struct SettleMessage : public FromExchange {
@@ -92,7 +92,7 @@
    SettleMessage(MessageTypes type, std::string product,
                  std::string stationName, std::chrono::nanoseconds expiry,
                  double price, uint64_t sequence, double timestamp);
    std::string as_string();
    std::string as_string();
};
struct AddMessage : public ToExchange {
@@ -110,10 +110,11 @@
    double price;
    uint64_t filled;
    uint64_t resting;
    std::string owner;
    std::string owner;
    AddedMessage(MessageTypes type, std::string product, std::string id,
                 book::OrderSideEnum side, double price, uint64_t filled,
                 uint64_t resting, std::string owner, uint64_t sequence, double timestamp);
                 uint64_t resting, std::string owner, uint64_t sequence,
                 double timestamp);
    std::string as_string();
};
protocol.cpp
@@ -175,57 +175,61 @@
                  json::DeletedMessage* message)
{
    if (message->side == book::Buy) {
        for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) {
                    if (i->id == message->id) {
                        bs[message->product].bidSide.erase(i);
                        break;
                    }
                }
        for (auto i = bs[message->product].bidSide.begin();
             i != bs[message->product].bidSide.end(); i++) {
            if (i->id == message->id) {
                bs[message->product].bidSide.erase(i);
                break;
            }
        }
    } else {
        for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) {
                    if (i->id == message->id) {
                        bs[message->product].askSide.erase(i);
                        break;
                    }
                }
        for (auto i = bs[message->product].askSide.begin();
             i != bs[message->product].askSide.end(); i++) {
            if (i->id == message->id) {
                bs[message->product].askSide.erase(i);
                break;
            }
        }
    }
}
void tradeOrder(std::unordered_map<std::string, book::Book>& bs,
                json::TradeMessage* message)
{
    if (message->tradeType == json::BUY_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].askSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].askSide.erase(i);
                    break;
                }
            }
        }
    } else if (message->tradeType == json::SELL_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].bidSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].bidSide.erase(i);
                    break;
                }
            }
        }
    }
    if (message->tradeType == json::BUY_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].askSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].askSide.begin();
                 i != bs[message->product].askSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].askSide.erase(i);
                    break;
                }
            }
        }
    } else if (message->tradeType == json::SELL_AGGRESSOR) {
        if (message->passiveOrderRemaining > 0) {
            for (auto& i : bs[message->product].bidSide) {
                if (i.id == message->passiveOrder) {
                    i.volume = message->passiveOrderRemaining;
                    break;
                }
            }
        } else {
            for (auto i = bs[message->product].bidSide.begin();
                 i != bs[message->product].bidSide.end(); i++) {
                if (i->id == message->passiveOrder) {
                    bs[message->product].bidSide.erase(i);
                    break;
                }
            }
        }
    }
}
void broker(std::unordered_map<std::string, book::Book>& bs,
@@ -238,7 +242,10 @@
    httplib::MultipartFormDataItems a = {{"message", message, "", ""},
                                         {"username", USER, "", ""},
                                         {"password", PASS, "", ""}};
    auto res = cli.Post("/execution", a);
    auto res = cli.Post("/execution",
                        "message=" + message + "&username=" + USER +
                            "&password=" + PASS,
                        "application/x-www-form-urlencoded");
    std::string b = res->body;
    std::queue<json::Message*> c = json::parse(b);
    return c.front();
strat.cpp
@@ -46,7 +46,8 @@
    while (true) {
        auto messages = protocol::catchUp(bs);
        // bom::updateBom(bs);
        printFeed(messages);
        // printFeed(messages);
        dumbHit(messages);
        freeMessages(messages);
    }
}
@@ -102,23 +103,30 @@
void dumbHit(std::deque<json::Message*>& m)
{
    for (auto& i : m) {
        if (i->type == json::ADDED) {
            json::AddedMessage* j = (json::AddedMessage*) i;
            if (j->owner == "jgrunbau") {
                book::OrderSideEnum s = j->side == book::Buy ? book::Sell : book::Buy;
                json::AddMessage a(json::ADD, j->product, j->price, s, j->resting);
                auto b = protocol::addOrder(a);
                if (b->type == json::ADDED && ((json::AddedMessage*) b)->resting > 0) {
                    json::DeleteMessage d(json::DELETE, j->product, ((json::AddedMessage*) b)->id);
                    delete protocol::deleteOrder(d);
                }
                if (b->type == json::ADDED) {
                    std::cout << "Hit " << j->owner << " for " << ((json::AddedMessage*) b)->filled << ", resting " << ((json::AddedMessage*) b)->resting << std::endl;
                }
                delete b;
            }
        }
    }
    for (auto& i : m) {
        if (i->type == json::ADDED) {
            json::AddedMessage* j = (json::AddedMessage*)i;
            if (j->owner == "jgrunbau") {
                book::OrderSideEnum s =
                    j->side == book::Buy ? book::Sell : book::Buy;
                json::AddMessage a(json::ADD, j->product, j->price, s,
                                   j->resting);
                auto b = protocol::addOrder(a);
                if (b->type == json::ADDED &&
                    ((json::AddedMessage*)b)->resting > 0) {
                    json::DeleteMessage d(json::DELETE, j->product,
                                          ((json::AddedMessage*)b)->id);
                    delete protocol::deleteOrder(d);
                }
                if (b->type == json::ADDED) {
                    std::cout << "Hit " << j->owner << " for "
                              << ((json::AddedMessage*)b)->filled
                              << ", resting "
                              << ((json::AddedMessage*)b)->resting << std::endl;
                }
                delete b;
            }
        }
    }
}
} // namespace strat
strat.hpp
@@ -2,7 +2,7 @@
namespace strat
{
    void initialise();
    void destroy();
    void mainLoop();
}
void initialise();
void destroy();
void mainLoop();
} // namespace strat
test.cpp
@@ -11,29 +11,28 @@
int main(void)
{
    std::chrono::nanoseconds time(0);
    for (int i = 0; i < trials; i++)
    {
        auto s = std::chrono::high_resolution_clock::now();
        book::Book b = book::testBook(100, false);
        auto e = std::chrono::high_resolution_clock::now();
        time += e - s;
    }
    std::cout << time.count() / trials << std::endl;
    std::chrono::nanoseconds time(0);
    for (int i = 0; i < trials; i++) {
        auto s = std::chrono::high_resolution_clock::now();
        book::Book b = book::testBook(100, false);
        auto e = std::chrono::high_resolution_clock::now();
        time += e - s;
    }
    std::cout << time.count() / trials << std::endl;
    // auto bs = protocol::recoverBook();
    //     protocol::catchUp(bs);
        // std::cout << bs.size() << std::endl;
        // for (auto i : bs) {
            // std::cout << i.first << std::endl;
            // i.second.printBook();
        // }
    // std::cout << bs.size() << std::endl;
    // for (auto i : bs) {
    // std::cout << i.first << std::endl;
    // i.second.printBook();
    // }
    // bom::initialise();
    // bom::updateBom(bs);
    // bom::destroy();
    // protocol::catchUp(bs);
    // std::cout << bs.size() << std::endl;
    // for (auto& i : bs) {
        // std::cout << i.first << ", " << i.second.expiry.count() << ", " << i.second.bomPrice << std::endl;
        // i.second.printBook();
    // std::cout << i.first << ", " << i.second.expiry.count() << ", " <<
    // i.second.bomPrice << std::endl; i.second.printBook();
    // }
}
test.py
@@ -6,7 +6,7 @@
import book
import protocol
test = False
test = True
HOST: str = 'sytev070'
if (test):
@@ -45,24 +45,29 @@
    so_far_today = f.read();
    for message in so_far_today:
        protocol.handleMessage(bs, message)
message = {"type": "ADD", "product": "F_THS_APP0119T2230", "price": 99.0,
           "side": "BUY", "volume": 1}
# print(json.dumps(message))
# print(send(message))
print(protocol.send(message))
# printRecoveryLog()
# logAllIncomingMessages()
# print(getInformation())
# wstest()
bs = {}
# bs = {}
# recoverBook(bs)
testRecoverBook(bs)
print(list(bs))
for i in bs:
    print(bs[i].product)
    bs[i].printBook()
    print()
    if (len(bs[i].askSide.levels) > 0):
        print(bs[i].askSide.levels[0])
    if (len(bs[i].bidSide.levels)):
        print(bs[i].bidSide.levels[0])
    print()
# testRecoverBook(bs)
# print(list(bs))
# for i in bs:
#     print(bs[i].product)
#     bs[i].printBook()
#     print()
#     if (len(bs[i].askSide.levels) > 0):
#         print(bs[i].askSide.levels[0])
#     if (len(bs[i].bidSide.levels)):
#         print(bs[i].bidSide.levels[0])
#     print()
# book.testBook()