From 0bef191acd5a77544852e6a3daae2df05bd34a31 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 19 Jan 2022 11:20:45 +0000 Subject: [PATCH] Reformat and change send to x-www-form-urlencoded --- strat.cpp | 46 ++++--- click.cpp | 54 ++++---- test.cpp | 31 ++-- test.py | 33 +++-- book.cpp | 16 +- json.hpp | 9 strat.hpp | 8 bot.cpp | 17 +- json.cpp | 8 protocol.cpp | 99 ++++++++------- 10 files changed, 169 insertions(+), 152 deletions(-) diff --git a/book.cpp b/book.cpp index 1ee1d06..5a3fb9d 100644 --- a/book.cpp +++ b/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++; diff --git a/bot.cpp b/bot.cpp index 0a25597..9792a39 100644 --- a/bot.cpp +++ b/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(); } diff --git a/click.cpp b/click.cpp index 0ee5fdc..ba85093 100644 --- a/click.cpp +++ b/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; + } + } } diff --git a/json.cpp b/json.cpp index 1e51602..3e1ad07 100644 --- a/json.cpp +++ b/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, diff --git a/json.hpp b/json.hpp index bfe2c50..e896869 100644 --- a/json.hpp +++ b/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(); }; diff --git a/protocol.cpp b/protocol.cpp index f1083c0..f336597 100644 --- a/protocol.cpp +++ b/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(); diff --git a/strat.cpp b/strat.cpp index 4655787..a25ca17 100644 --- a/strat.cpp +++ b/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 diff --git a/strat.hpp b/strat.hpp index dd02bf5..3b37313 100644 --- a/strat.hpp +++ b/strat.hpp @@ -2,7 +2,7 @@ namespace strat { - void initialise(); - void destroy(); - void mainLoop(); -} +void initialise(); +void destroy(); +void mainLoop(); +} // namespace strat diff --git a/test.cpp b/test.cpp index 6226a44..b4c19b2 100644 --- a/test.cpp +++ b/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(); // } } diff --git a/test.py b/test.py index 76de53f..f95add8 100644 --- a/test.py +++ b/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() -- Gitblit v1.9.3