From 9ae8b92ba549ab916c88e9004a95c1ed0cd16059 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 19 Jan 2022 10:48:57 +0000
Subject: [PATCH] Added loop to click trader and hit self for perf testing

---
 strat.cpp |    7 ++-
 click.cpp |   54 ++++++++++++++++----------
 json.cpp  |    4 +-
 3 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/click.cpp b/click.cpp
index 932e618..0ee5fdc 100644
--- a/click.cpp
+++ b/click.cpp
@@ -2,9 +2,11 @@
 #include "json.hpp"
 #include "protocol.hpp"
 
+#include <cstdint>
 #include <cstdlib>
 #include <getopt.h>
 #include <iostream>
+#include <string>
 #include <unordered_map>
 
 enum clickType { Buy, Sell, Flash, Delete };
@@ -30,6 +32,7 @@
 			  << "\t-p price" << std::endl
 			  << "\t-v volume" << std::endl
 			  << "\t-i id" << 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;
@@ -69,10 +72,12 @@
 	if (d->type == json::DELETED) {
 		std::cout << static_cast<json::DeletedMessage*>(d)->as_string()
 				  << std::endl;
-	} else {
+	} else if (d->type == json::REJECT) {
 		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;
+    }
 	delete b;
 	delete d;
 }
@@ -87,7 +92,7 @@
 	} else if (b->type == json::REJECT) {
 		std::cout << static_cast<json::RejectMessage*>(b)->as_string()
 				  << std::endl;
-	} else {
+	} else if (b->type == json::ERROR) {
 		std::cout << static_cast<json::ErrorMessage*>(b)->as_string()
 				  << std::endl;
 	}
@@ -100,11 +105,12 @@
 	double price;
 	clickType click, side;
 	uint64_t volume;
+    uint64_t times = 1;
 	initialise();
 	if (argc == 1) {
 		usage(), exit(0);
 	}
-	while ((c = getopt(argc, argv, "a::t::s::p::v::i::")) != -1) {
+	while ((c = getopt(argc, argv, "a::t::s::p::v::i::l::")) != -1) {
 		switch (c) {
 		case 'a':
 			product = std::string(optarg);
@@ -124,6 +130,9 @@
 		case 'i':
 			id = std::string(optarg);
 			break;
+        case 'l':
+            times = std::stoll(optarg);
+            break;
 		case '?':
 			std::cout << "*1" << std::endl;
 		default:
@@ -132,21 +141,24 @@
 			exit(0);
 		}
 	}
-	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;
+        }
+    }
 }
diff --git a/json.cpp b/json.cpp
index d486ddc..1e51602 100644
--- a/json.cpp
+++ b/json.cpp
@@ -192,7 +192,7 @@
 
 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));
 }
 
@@ -392,7 +392,7 @@
 
 std::string RejectMessage::as_string()
 {
-	return "{\"type\": \"REJECT\", \"product\": \"" + this->product =
+	return "{\"type\": \"REJECT\", \"product\": \"" + this->product +
 	           "\", \"error\": \"" + this->error +
 	           "\", \"sequence\": " + std::to_string(this->sequence) +
 	           ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
diff --git a/strat.cpp b/strat.cpp
index 2856c1a..4655787 100644
--- a/strat.cpp
+++ b/strat.cpp
@@ -105,14 +105,17 @@
     for (auto& i : m) {
         if (i->type == json::ADDED) {
             json::AddedMessage* j = (json::AddedMessage*) i;
-            if (j->owner == "nseow") {
+            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) {
+                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;
             }
         }

--
Gitblit v1.9.3