Joel Grunbaum
2022-01-07 127d5af6f934b162c98a40ce414b98415d899cea
finished book
3 files added
5 files modified
153 ■■■■■ changed files
.clang-format 8 ●●●●● patch | view | raw | blame | history
.gitignore 15 ●●●●● patch | view | raw | blame | history
Makefile 4 ●●●● patch | view | raw | blame | history
book.cpp 74 ●●●● patch | view | raw | blame | history
book.hpp 29 ●●●● patch | view | raw | blame | history
main.cpp 3 ●●●● patch | view | raw | blame | history
secrets.hpp 11 ●●●●● patch | view | raw | blame | history
strat.hpp 9 ●●●●● patch | view | raw | blame | history
.clang-format
New file
@@ -0,0 +1,8 @@
---
BasedOnStyle: LLVM
IndentWidth: 4
UseTab: AlignWithSpaces
AllowShortIfStatementsOnASingleLine: true
BreakBeforeBraces: Linux
IndentCaseLabels: false
---
.gitignore
@@ -1,6 +1,9 @@
*
!*.py
!.gitignore
!*.cpp
!*.hpp
!Makefile
*~
*\#*
*.o
exec
test
bot
bid
ask
oneshot
Makefile
@@ -1,5 +1,5 @@
CC=clang
CXX=clang
CC=gcc
CXX=g++
CXXFLAGS=-g -Wall -std=c++20
.PHONY: default all
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;
}
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);
main.cpp
@@ -1,6 +1,7 @@
#include "book.hpp"
#include <chrono>
int main(void)
{
    testBook();
    Book b = testBook(10, true);
}
secrets.hpp
New file
@@ -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"
strat.hpp
New file
@@ -0,0 +1,9 @@
#pragma once
#include "book.hpp"
#include "secrets.hpp"
Book recoverBook()
{
}