write price level to each book
| | |
| | | oneshot |
| | | build |
| | | test |
| | | click |
| | | click |
| | | bom_data.xml |
| | |
| | | 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 |
| | |
| | | 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() |
| | |
| | | |
| | | 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++) { |
| | |
| | | } |
| | | 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++) { |
| | |
| | | double aggFee; |
| | | double pasFee; |
| | | double broFee; |
| | | double bomPrice; |
| | | double bomPrice; |
| | | |
| | | Book(); |
| | | Book(ProductTypeEnum productType, std::string product, int stationId, |
| | |
| | | |
| | | 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() |
| | |
| | | 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) { |
| | |
| | | |
| | | 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) |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | |
| | | mapTrade = {{"BUY_AGGRESSOR", BUY_AGGRESSOR}, |
| | | {"SELL_AGGRESSOR", SELL_AGGRESSOR}, |
| | | {"BROKER_TRADE", BROKER_TRADE}}; |
| | | {"BROKER_TRADE", BROKER_TRADE}}; |
| | | |
| | | mapOrderSide = {{book::Buy, "BUY"}, {book::Sell, "SELL"}}; |
| | | } |
| | |
| | | { |
| | | 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, |
| | |
| | | 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, |
| | |
| | | { |
| | | if (mapOrderSide.empty()) initialise(); |
| | | return "{\"type\": \"DELETE\", \"product\": \"" + this->product + |
| | | "\", \"id\": \"" + this->id + "\"}"; |
| | | "\", \"id\": \"" + this->id + "\"}"; |
| | | } |
| | | |
| | | DeletedMessage::DeletedMessage(MessageTypes type, std::string product, |
| | |
| | | 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, |
| | |
| | | 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, |
| | |
| | | #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(); |
| | | } |
| | | } |