Reformat and change send to x-www-form-urlencoded
| | |
| | | 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) |
| | | { |
| | | 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++; |
| | |
| | | |
| | | 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(); |
| | | } |
| | |
| | | << "\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; |
| | |
| | | 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; |
| | | } |
| | |
| | | double price; |
| | | clickType click, side; |
| | | uint64_t volume; |
| | | uint64_t times = 1; |
| | | uint64_t times = 1; |
| | | initialise(); |
| | | if (argc == 1) { |
| | | usage(), exit(0); |
| | |
| | | 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: |
| | |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | 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)); |
| | | } |
| | | |
| | |
| | | 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, |
| | |
| | | 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 { |
| | |
| | | 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 { |
| | |
| | | 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(); |
| | | }; |
| | | |
| | |
| | | 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, |
| | |
| | | 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(); |
| | |
| | | while (true) { |
| | | auto messages = protocol::catchUp(bs); |
| | | // bom::updateBom(bs); |
| | | printFeed(messages); |
| | | // printFeed(messages); |
| | | dumbHit(messages); |
| | | freeMessages(messages); |
| | | } |
| | | } |
| | |
| | | |
| | | 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 |
| | |
| | | |
| | | namespace strat |
| | | { |
| | | void initialise(); |
| | | void destroy(); |
| | | void mainLoop(); |
| | | } |
| | | void initialise(); |
| | | void destroy(); |
| | | void mainLoop(); |
| | | } // namespace strat |
| | |
| | | |
| | | 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(); |
| | | // } |
| | | } |
| | |
| | | import book |
| | | import protocol |
| | | |
| | | test = False |
| | | test = True |
| | | |
| | | HOST: str = 'sytev070' |
| | | if (test): |
| | |
| | | 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() |