| | |
| | | #include "strat.hpp" |
| | | |
| | | namespace strat { |
| | | #include "bom.hpp" |
| | | #include "book.hpp" |
| | | #include "json.hpp" |
| | | #include "secrets.hpp" |
| | | |
| | | #include "protocol.hpp" |
| | | #include <deque> |
| | | #include <unordered_map> |
| | | |
| | | namespace strat |
| | | { |
| | | std::unordered_map<std::string, json::Message*> orders; |
| | | |
| | | void deleteOrder(json::AddedMessage* m); |
| | | void freeMessages(std::deque<json::Message*>& m); |
| | | void printFeed(std::deque<json::Message*>& m); |
| | | void dumbHit(std::deque<json::Message*>& m); |
| | | |
| | | void initialise() |
| | | { |
| | | bom::initialise(); |
| | | protocol::createWebSocket(); |
| | | } |
| | | |
| | | void destroy() |
| | | { |
| | | bom::destroy(); |
| | | for (auto& i : orders) { |
| | | switch (i.second->type) { |
| | | case json::FUTURE_TYPE: |
| | | case json::SPREAD_TYPE: |
| | | case json::CALL_TYPE: |
| | | case json::PUT_TYPE: |
| | | deleteOrder((json::AddedMessage*)i.second); |
| | | default:; |
| | | } |
| | | delete i.second; |
| | | } |
| | | } |
| | | |
| | | void mainLoop() |
| | | { |
| | | auto bs = protocol::recoverBook(); |
| | | std::cout << "ready" << std::endl; |
| | | while (true) { |
| | | auto messages = protocol::catchUp(bs); |
| | | // bom::updateBom(bs); |
| | | printFeed(messages); |
| | | // dumbHit(messages); |
| | | freeMessages(messages); |
| | | } |
| | | } |
| | | |
| | | void deleteOrder(json::AddedMessage* m) |
| | | { |
| | | json::DeleteMessage a(json::DELETE, m->product, m->id); |
| | | delete protocol::deleteOrder(a); |
| | | } |
| | | |
| | | void printFeed(std::deque<json::Message*>& m) |
| | | { |
| | | for (auto& i : m) { |
| | | switch (i->type) { |
| | | case json::FUTURE_TYPE: |
| | | case json::SPREAD_TYPE: |
| | | case json::CALL_TYPE: |
| | | case json::PUT_TYPE: |
| | | std::cout << ((json::AnnounceMessage*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::SETTLEMENT: |
| | | std::cout << ((json::SettleMessage*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::ADDED: |
| | | std::cout << ((json::AddedMessage*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::DELETED: |
| | | std::cout << ((json::DeletedMessage*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::TRADE: |
| | | std::cout << ((json::TradeMessage*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::BROKER_REQUEST: |
| | | std::cout << ((json::BrokerRequest*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::BROKER_ACK: |
| | | std::cout << ((json::BrokerAck*)i)->as_string() << std::endl; |
| | | break; |
| | | case json::BROKER_CONFIRM: |
| | | std::cout << ((json::BrokerConfirm*)i)->as_string() << std::endl; |
| | | break; |
| | | default:; |
| | | } |
| | | } |
| | | } |
| | | |
| | | void freeMessages(std::deque<json::Message*>& m) |
| | | { |
| | | for (auto i : m) { |
| | | delete i; |
| | | } |
| | | } |
| | | |
| | | 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 == "nseow") { |
| | | 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 |