From 16b655e7c8cfb2e32e6bb839373f30ad63506f9a Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Sat, 08 Jan 2022 13:28:32 +0000
Subject: [PATCH] Added json parsing, protocol comprehension and began executables for algs

---
 book.hpp |  108 +++++++++++++++++++++--------------------------------
 1 files changed, 43 insertions(+), 65 deletions(-)

diff --git a/book.hpp b/book.hpp
index a167b32..2240290 100644
--- a/book.hpp
+++ b/book.hpp
@@ -1,89 +1,67 @@
 #pragma once
 
+#include <algorithm>
 #include <chrono>
+#include <cstddef>
 #include <iostream>
 #include <queue>
 #include <string>
 #include <vector>
 
+namespace book
+{
 enum OrderSideEnum { Buy, Sell };
 enum ProductTypeEnum { TEST, FUTURE, SPREAD, CALL, PUT };
 
 struct Order {
-  double price;
-  OrderSideEnum side;
-  int remaining_volume;
-  int filled_volume;
-  std::chrono::nanoseconds timestamp;
-  std::string id;
-  Order(double price, OrderSideEnum side, int volume,
-        std::chrono::nanoseconds timestamp, std::string id);
+    double price;
+    OrderSideEnum side;
+    int remaining_volume;
+    int filled_volume;
+    double timestamp;
+    std::string id;
+    Order(double price, OrderSideEnum side, int volume, double timestamp,
+          std::string id);
 };
 
 struct Level {
-  double price;
-  int volume;
-  OrderSideEnum side;
-  std::chrono::nanoseconds timestamp;
-  std::string id;
+    double price;
+    int volume;
+    OrderSideEnum side;
+    double timestamp;
+    std::string id;
 
-  Level(Order &order);
+    Level(Order& order);
 };
 
-bool operator>(const Level &a, const Level &b);
-bool operator<(const Level &a, const Level &b);
-bool operator>=(const Level &a, const Level &b);
-bool operator<=(const Level &a, const Level &b);
-bool operator==(const Level &a, const Level &b);
-
-template <class T>
-struct Side : public std::priority_queue<Level, std::vector<Level>, T> {
-public:
-  void deleteLevel(std::string orderId) {
-    for (auto i = this->c.begin(); i != this->c.end();) {
-      if (*i.id == orderId) {
-        this->c.erase(i);
-        std::make_heap(this->c.begin(), this->c.end(), this->comp);
-      }
-    }
-  }
-
-  void topRemoveVolume(int volume) { this->c[0].volume -= volume; }
-
-  void printTop(int num = 5) {
-    std::sort(this->c.begin(), this->c.end(), this->comp);
-    for (int i = 0; i < num && i < this->size(); i++) {
-      std::cout << "Price: " << this->c[i].price
-                << ", volume: " << this->c[i].volume
-                << ", time: " << this->c[i].timestamp.count()
-                << ", id: " << this->c[i].id << std::endl;
-    }
-    std::make_heap(this->c.begin(), this->c.end(), this->comp);
-  }
-};
-
-using AskSide = Side<std::greater<Level>>;
-using BidSide = Side<std::less<Level>>;
+bool operator>(const Level& a, const Level& b);
+bool operator<(const Level& a, const Level& b);
+bool operator>=(const Level& a, const Level& b);
+bool operator<=(const Level& a, const Level& b);
+bool operator==(const Level& a, const Level& b);
+std::ostream& operator<<(std::ostream& out, const Level& a);
 
 struct Book {
-  BidSide bidSide;
-  AskSide askSide;
-  ProductTypeEnum productType;
-  std::string product;
-  std::string stationId;
-  std::string unit;
-  std::chrono::nanoseconds expiry;
-  double aggFee;
-  double pasFee;
-  double broFee;
+    std::vector<Level> bidSide;
+    std::vector<Level> askSide;
+    ProductTypeEnum productType;
+    std::string product;
+    std::string stationId;
+    std::string unit;
+    std::chrono::nanoseconds expiry;
+    double aggFee;
+    double pasFee;
+    double broFee;
 
-  Book();
-  Book(ProductTypeEnum productType, std::string product, std::string stationId,
-       std::string unit, std::chrono::nanoseconds expiry, double aggFee,
-       double pasFee, double broFee);
-  void ask(Order &order);
-  void bid(Order &order);
-  void printBook();
+    Book();
+    Book(ProductTypeEnum productType, std::string product,
+         std::string stationId, std::string unit,
+         std::chrono::nanoseconds expiry, double aggFee, double pasFee,
+         double broFee);
+    void ask(Order& order);
+    void bid(Order& order);
+    void printBook(std::size_t numOrders = 10);
 };
 
 Book testBook(int orders = 10, bool printBook = true);
+} // namespace book

--
Gitblit v1.9.3