From 441abf1e6a15a39f1ef7bf5e4ee2a4a43bff327b Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Thu, 20 Jan 2022 05:18:00 +0000
Subject: [PATCH] Added names and ready notice

---
 strat.cpp |  127 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/strat.cpp b/strat.cpp
index fdd367d..fe26c2e 100644
--- a/strat.cpp
+++ b/strat.cpp
@@ -1,6 +1,133 @@
 #include "strat.hpp"
 
+#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" || j->owner == "jgrunbau" || j->owner == "zoar" || j->owner == "aobrien") {
+				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

--
Gitblit v1.9.3