From 127d5af6f934b162c98a40ce414b98415d899cea Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Fri, 07 Jan 2022 10:43:34 +0000 Subject: [PATCH] finished book --- .gitignore | 15 +++-- main.cpp | 3 book.cpp | 74 ++++++++++++++++++------ strat.hpp | 9 +++ Makefile | 4 .clang-format | 8 ++ book.hpp | 29 ++------- secrets.hpp | 11 +++ 8 files changed, 102 insertions(+), 51 deletions(-) diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e6786f7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,8 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 4 +UseTab: AlignWithSpaces +AllowShortIfStatementsOnASingleLine: true +BreakBeforeBraces: Linux +IndentCaseLabels: false +--- \ No newline at end of file diff --git a/.gitignore b/.gitignore index b836a20..4e07316 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ -* -!*.py -!.gitignore -!*.cpp -!*.hpp -!Makefile +*~ +*\#* +*.o +exec +test +bot +bid +ask +oneshot diff --git a/Makefile b/Makefile index b57ea21..96fba56 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CC=clang -CXX=clang +CC=gcc +CXX=g++ CXXFLAGS=-g -Wall -std=c++20 .PHONY: default all diff --git a/book.cpp b/book.cpp index 2202639..9cb14e7 100644 --- a/book.cpp +++ b/book.cpp @@ -1,5 +1,6 @@ #include "book.hpp" #include <chrono> +#include <cstddef> #include <iostream> Order::Order(double price, OrderSideEnum side, int volume, @@ -14,7 +15,7 @@ bool operator<(const Level &a, const Level &b) { if (a.price < b.price) return true; - else if (a.price == b.price && a.timestamp < b.timestamp) + else if (a.price == b.price && (a.timestamp.count() > b.timestamp.count())) return true; else return false; @@ -23,7 +24,7 @@ bool operator>(const Level &a, const Level &b) { if (a.price > b.price) return true; - else if (a.price == b.price && a.timestamp > b.timestamp) + else if (a.price == b.price && (a.timestamp.count() > b.timestamp.count())) return true; else return false; @@ -32,7 +33,7 @@ bool operator<=(const Level &a, const Level &b) { if (a.price <= b.price) return true; - else if (a.price == b.price && a.timestamp <= b.timestamp) + else if (a.price == b.price && (a.timestamp.count() >= b.timestamp.count())) return true; else return false; @@ -41,17 +42,48 @@ bool operator>=(const Level &a, const Level &b) { if (a.price >= b.price) return true; - else if (a.price == b.price && a.timestamp >= b.timestamp) + else if (a.price == b.price && (a.timestamp.count() >= b.timestamp.count())) return true; else return false; } bool operator==(const Level &a, const Level &b) { - if (a.price == b.price && a.timestamp == b.timestamp) + if (a.price == b.price && (a.timestamp.count() == b.timestamp.count())) return true; else return false; +} + +std::ostream& operator<<(std::ostream& out, const Level& a) +{ + return out << "Price: " << a.price << ", volume: " << a.volume << ", time: " << a.timestamp.count() << ", id: " << a.id; +} + +template<class T> +void Side<T>::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); + } + } +} + +template<class T> +void Side<T>::topRemoveVolume(int volume) { this->c[0].volume -= volume; } + +template<class T> +void Side<T>::printTop(std::size_t num) +{ + std::vector<Level> copy(this->c); + std::sort(copy.begin(), copy.end(), this->comp); + if (copy.size() && copy[0].side == Buy) + std::reverse(copy.begin(), copy.end()); + for (std::size_t i = 0; i < copy.size() && i < num; i++) { + std::cout << copy[i] << std::endl; + } } Book::Book() @@ -68,52 +100,54 @@ pasFee(pasFee), broFee(broFee) {} void Book::ask(Order &order) { - while (this->bidSide.size() && this->bidSide.top().price > order.price) { + while (this->bidSide.size() && this->bidSide.top().price >= order.price) { if (this->bidSide.top().volume > order.remaining_volume) { int temp = this->bidSide.top().volume; order.filled_volume += temp; this->bidSide.topRemoveVolume(order.remaining_volume); order.remaining_volume -= temp; + break; } else { order.remaining_volume -= this->bidSide.top().volume; order.filled_volume += this->bidSide.top().volume; this->bidSide.pop(); } - if (order.remaining_volume > 0) { - this->askSide.emplace(order); - } + } + if (order.remaining_volume > 0) { + this->askSide.emplace(order); } } void Book::bid(Order &order) { - while (this->askSide.size() && this->askSide.top().price > order.price) { + while (this->askSide.size() && this->askSide.top().price <= order.price) { if (this->askSide.top().volume > order.remaining_volume) { int temp = this->askSide.top().volume; order.filled_volume += temp; this->askSide.topRemoveVolume(order.remaining_volume); order.remaining_volume -= temp; + break; } else { order.remaining_volume -= this->askSide.top().volume; order.filled_volume += this->askSide.top().volume; this->askSide.pop(); } - if (order.remaining_volume > 0) { - this->bidSide.emplace(order); - } + } + if (order.remaining_volume > 0) { + this->bidSide.emplace(order); } } -void Book::printBook() { - std::cout << "Sell side"; - this->askSide.printTop(); - std::cout << "Buy side"; - this->bidSide.printTop(); +void Book::printBook(std::size_t numOrders) { + std::cout << "Sell side: " << this->askSide.size() << std::endl; + this->askSide.printTop(numOrders); + std::cout << "Buy side: " << this->bidSide.size() << std::endl; + this->bidSide.printTop(numOrders); } Book testBook(int orders, bool printBook) { Book b = Book(); std::chrono::nanoseconds time(1); - for (int i = 0; i < orders; i++) { + for (int i = 1; i < orders; i++) { Order t(i, Buy, 10, time++, "a"); b.bid(t); } @@ -122,6 +156,6 @@ b.ask(t); } if (printBook) - b.printBook(); + b.printBook(orders - 1); return b; } diff --git a/book.hpp b/book.hpp index a167b32..9e3a89c 100644 --- a/book.hpp +++ b/book.hpp @@ -1,10 +1,12 @@ #pragma once #include <chrono> +#include <cstddef> #include <iostream> #include <queue> #include <string> #include <vector> +#include <algorithm> enum OrderSideEnum { Buy, Sell }; enum ProductTypeEnum { TEST, FUTURE, SPREAD, CALL, PUT }; @@ -35,31 +37,14 @@ 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); 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); - } + void deleteLevel(std::string orderId); + void topRemoveVolume(int volume); + void printTop(std::size_t num = 5); }; using AskSide = Side<std::greater<Level>>; @@ -83,7 +68,7 @@ double pasFee, double broFee); void ask(Order &order); void bid(Order &order); - void printBook(); + void printBook(std::size_t numOrders = 10); }; Book testBook(int orders = 10, bool printBook = true); diff --git a/main.cpp b/main.cpp index b05ddec..612f52f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include "book.hpp" +#include <chrono> int main(void) { - testBook(); + Book b = testBook(10, true); } diff --git a/secrets.hpp b/secrets.hpp new file mode 100644 index 0000000..b0622e5 --- /dev/null +++ b/secrets.hpp @@ -0,0 +1,11 @@ +#pragma once + +#define HOST "sytev070" +#ifdef TEST +#define PORT "9005" +#else +#define PORT "9000" +#endif + +#define USER "jgrunbau" +#define PASS "b7d630945a0854581d9f86ba147f34a5" diff --git a/strat.hpp b/strat.hpp new file mode 100644 index 0000000..212f323 --- /dev/null +++ b/strat.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "book.hpp" +#include "secrets.hpp" + +Book recoverBook() +{ + +} -- Gitblit v1.9.3