From f99dcdf422310400fe9f20f61697328e87485a34 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 19 Jan 2022 09:57:14 +0000
Subject: [PATCH] Insertion sort sides instead of hashmap

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

diff --git a/strat.cpp b/strat.cpp
index fdd367d..2856c1a 100644
--- a/strat.cpp
+++ b/strat.cpp
@@ -1,6 +1,121 @@
 #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();
+	while (true) {
+		auto messages = protocol::catchUp(bs);
+		// bom::updateBom(bs);
+		printFeed(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::DeleteMessage d(json::DELETE, j->product, ((json::AddedMessage*) b)->id);
+                    delete protocol::deleteOrder(d);
+                }
+                delete b;
+            }
+        }
+    }
+}
+} // namespace strat

--
Gitblit v1.9.3