From e8c910e52d1807e2fcca3b43d80a9df6acac5387 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 12 Jan 2022 06:26:46 +0000 Subject: [PATCH] write price level to each book --- .gitignore | 3 click.cpp | 64 ++++++++------- test.cpp | 29 ++++-- book.cpp | 26 +++--- bom.cpp | 26 +++--- book.hpp | 2 json.cpp | 38 ++++---- 7 files changed, 102 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 9a70274..495435f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ oneshot build test -click \ No newline at end of file +click +bom_data.xml diff --git a/bom.cpp b/bom.cpp index 5642cfc..64058ba 100644 --- a/bom.cpp +++ b/bom.cpp @@ -35,21 +35,23 @@ std::ifstream fs("bom_data.xml"); std::vector<char> buffer{std::istreambuf_iterator<char>(fs), istreambuf_iterator<char>()}; - buffer.push_back('\0'); + buffer.push_back('\0'); rapidxml::xml_document<> d; d.parse<0>(&buffer[0]); - // Walk stations + // Walk stations for (rapidxml::xml_node<>* n = d.first_node()->last_node()->first_node(); n; n = n->next_sibling()) { - int bom_id = std::stoi(n->first_attribute()->next_attribute()->value()); - for (auto &i : bs) { - if (i.second.stationId == bom_id) { - // Should be apparent temp - i.second.bomPrice = std::stod(n->first_node()->first_node()->first_node()->first_node()->value()); - std::cout << i.second.product << ", " << i.second.stationId << ", " << i.second.bomPrice << std::endl; - break; - } - } - } + int bom_id = std::stoi(n->first_attribute()->next_attribute()->value()); + for (auto& i : bs) { + if (i.second.stationId == bom_id) { + // Should be apparent temp + i.second.bomPrice = std::stod(n->first_node() + ->first_node() + ->first_node() + ->first_node() + ->value()); + } + } + } } } // namespace bom diff --git a/book.cpp b/book.cpp index 09d096c..573fa75 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,27 +92,26 @@ void Book::ask(Order& order) { - auto a = - this->askSide.emplace(order.id, order); - if (!a.second) { - std::cout << order.id << "already exists" << std::endl; - } + auto a = this->askSide.emplace(order.id, order); + if (!a.second) { + std::cout << order.id << "already exists" << std::endl; + } } void Book::bid(Order& order) { - auto a = - this->bidSide.emplace(order.id, order); - if (!a.second) { - std::cout << order.id << "already exists" << std::endl; - } + auto a = this->bidSide.emplace(order.id, order); + if (!a.second) { + std::cout << order.id << "already exists" << std::endl; + } } void Book::printBook(std::size_t numOrders) { std::cout << "Sell side: " << this->askSide.size() << std::endl; std::vector<Level> askCopy; - for (auto i : this->askSide) askCopy.push_back(i.second); + for (auto i : this->askSide) + askCopy.push_back(i.second); std::size_t count = 0; std::sort(askCopy.begin(), askCopy.end()); for (auto i = askCopy.rbegin(); i != askCopy.rend(); i++) { @@ -122,7 +121,8 @@ } std::cout << "Buy side: " << this->bidSide.size() << std::endl; std::vector<Level> bidCopy; - for (auto i : this->bidSide) bidCopy.push_back(i.second); + for (auto i : this->bidSide) + bidCopy.push_back(i.second); count = 0; std::sort(bidCopy.begin(), bidCopy.end()); for (auto i = bidCopy.rbegin(); i != bidCopy.rend(); i++) { diff --git a/book.hpp b/book.hpp index 27b6597..5d952a3 100644 --- a/book.hpp +++ b/book.hpp @@ -53,7 +53,7 @@ double aggFee; double pasFee; double broFee; - double bomPrice; + double bomPrice; Book(); Book(ProductTypeEnum productType, std::string product, int stationId, diff --git a/click.cpp b/click.cpp index 749d649..932e618 100644 --- a/click.cpp +++ b/click.cpp @@ -13,10 +13,10 @@ 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}}; + 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() @@ -38,27 +38,32 @@ 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)); + 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)); + 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) +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); + 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) { @@ -74,17 +79,18 @@ 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) { + 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; - } + std::cout << static_cast<json::ErrorMessage*>(b)->as_string() + << std::endl; + } } int main(int argc, char** argv) @@ -106,9 +112,9 @@ case 't': click = mapClick[optarg]; break; - case 's': - side = mapClick[optarg]; - break; + case 's': + side = mapClick[optarg]; + break; case 'p': price = std::stod(optarg); break; @@ -119,9 +125,9 @@ id = std::string(optarg); break; case '?': - std::cout << "*1" << std::endl; + std::cout << "*1" << std::endl; default: - std::cout << "*2 " << (char) c << std::endl; + std::cout << "*2 " << (char)c << std::endl; usage(); exit(0); } @@ -134,13 +140,13 @@ sell(product, price, volume); break; case Flash: - if (side == clickType::Buy) - flash(product, price, volume, book::Buy); - else - flash(product, price, volume, book::Sell); + if (side == clickType::Buy) + flash(product, price, volume, book::Buy); + else + flash(product, price, volume, book::Sell); break; - case Delete: - deleteOrder(product, id); - break; + case Delete: + deleteOrder(product, id); + break; } } diff --git a/json.cpp b/json.cpp index f64f677..dfcbbf4 100644 --- a/json.cpp +++ b/json.cpp @@ -49,7 +49,7 @@ mapTrade = {{"BUY_AGGRESSOR", BUY_AGGRESSOR}, {"SELL_AGGRESSOR", SELL_AGGRESSOR}, - {"BROKER_TRADE", BROKER_TRADE}}; + {"BROKER_TRADE", BROKER_TRADE}}; mapOrderSide = {{book::Buy, "BUY"}, {book::Sell, "SELL"}}; } @@ -279,9 +279,9 @@ { if (mapOrderSide.empty()) initialise(); return "{\"type\": \"ADD\", \"product\": \"" + this->product + - "\", \"price\": " + std::to_string(this->price) + ", \"side\": \"" + - mapOrderSide[this->side] + - "\", \"volume\": " + std::to_string(this->volume) + "}"; + "\", \"price\": " + std::to_string(this->price) + ", \"side\": \"" + + mapOrderSide[this->side] + + "\", \"volume\": " + std::to_string(this->volume) + "}"; } AddedMessage::AddedMessage(MessageTypes type, std::string product, @@ -296,13 +296,13 @@ std::string AddedMessage::as_string() { return "{\"type\": \"ADDED\", \"product\": \"" + this->product + - "\", \"product\": \"" + this->id + "\" \"side\": \"" + - mapOrderSide[this->side] + - "\", \"price\": " + std::to_string(this->price) + - "\"filled\": " + std::to_string(this->filled) + - ", \"resting\": " + std::to_string(this->resting) + - ", \"sequence\": " + std::to_string(this->sequence) + - ", \"timestamp\":" + std::to_string(this->timestamp) + "}"; + "\", \"product\": \"" + this->id + "\" \"side\": \"" + + mapOrderSide[this->side] + + "\", \"price\": " + std::to_string(this->price) + + "\"filled\": " + std::to_string(this->filled) + + ", \"resting\": " + std::to_string(this->resting) + + ", \"sequence\": " + std::to_string(this->sequence) + + ", \"timestamp\":" + std::to_string(this->timestamp) + "}"; } DeleteMessage::DeleteMessage(MessageTypes type, std::string product, @@ -315,7 +315,7 @@ { if (mapOrderSide.empty()) initialise(); return "{\"type\": \"DELETE\", \"product\": \"" + this->product + - "\", \"id\": \"" + this->id + "\"}"; + "\", \"id\": \"" + this->id + "\"}"; } DeletedMessage::DeletedMessage(MessageTypes type, std::string product, @@ -328,10 +328,10 @@ std::string DeletedMessage::as_string() { return "{\"type\": \"DELETED\", \"product\": \"" + this->product + - "\", \"product\": \"" + this->id + "\" \"side\": \"" + - mapOrderSide[this->side] + - ", \"sequence\": " + std::to_string(this->sequence) + - ", \"timestamp\":" + std::to_string(this->timestamp) + "}"; + "\", \"product\": \"" + this->id + "\" \"side\": \"" + + mapOrderSide[this->side] + + ", \"sequence\": " + std::to_string(this->sequence) + + ", \"timestamp\":" + std::to_string(this->timestamp) + "}"; } RejectMessage::RejectMessage(MessageTypes type, std::string product, @@ -344,9 +344,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/test.cpp b/test.cpp index ad954bc..b8df66a 100644 --- a/test.cpp +++ b/test.cpp @@ -1,22 +1,29 @@ +#include "bom.hpp" #include "book.hpp" #include "json.hpp" #include "protocol.hpp" -#include "bom.hpp" #include <chrono> +#include <iostream> #include <unistd.h> #include <unordered_map> int main(void) { // book::Book b = book::testBook(10, true); - 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(); -// } - bom::initialise(); - bom::updateBom(bs); - bom::destroy(); + 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(); + // } + 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.bomPrice << std::endl; + i.second.printBook(); + } } -- Gitblit v1.9.3