Joel Grunbaum
2022-01-11 83c048d1295b9df59d90a7513953c99a267eb1f5
protocol.py
@@ -1,5 +1,19 @@
""".#protocol.py handling protocol."""
import book
from enum import Enum
import httpx
import json
test = True
HOST: str = "sytev070"
if (test):
    PORT = "9005"
else:
    PORT = "9000"
USER: str = "jgrunbau"
PASS: str = "b7d630945a0854581d9f86ba147f34a5"
def handleMessage(bs: {str, book.Book}, message: {str, str}):
@@ -11,7 +25,12 @@
    elif (message["type"] == "ADDED"):
        addOrder(bs, message)
    elif (message["type"] == "DELETED"):
        pass
        deleteOrder(bs, message)
    elif (message["type"] == "TRADE"):
        tradeOrder(bs, message)
    elif (message["type"] in {"BROKER_REQUEST", "BROKER_ACK",
                              "BROKER_CONFIRM"}):
        broker(bs, message)
def announce(bs: {str, book.Book}, message: {str, str}):
@@ -27,14 +46,85 @@
def settle(bs: {str, book.Book}, message: {str, str}):
    """Settle order."""
    bs.pop(message["product"])#.printBook()
    b = bs.pop(message["product"])
    if (False):
        b.printBook()
        print()
        if (len(b.askSide.levels) > 0):
            print(b.askSide.levels[0])
        if (len(b.bidSide.levels)):
            print(b.bidSide.levels[0])
        print()
def addOrder(bs: {str, book.Book}, message: {str, str}):
    """Order added to exchange."""
    price = message["price"]
    volume = message["resting"] + message["filled"]
    if (message["side"] == "BUY"):
        side = book.OrderSide.Buy
        bs[message["product"]].bid(
            book.Order(message["price"], book.OrderSide.Buy,
                       message["resting"],
                       message["timestamp"], message["id"]))
    else:
        side = book.OrderSide.Sell
    bs[message["product"]].addOrder(book.Order(price, side, volume))
        bs[message["product"]].ask(
            book.Order(message["price"], book.OrderSide.Sell,
                       message["resting"],
                       message["timestamp"], message["id"]))
def deleteOrder(bs: {str, book.Book}, message: {str, str}):
    """Delete order from market."""
    if (message["side"] == "BUY"):
        bs[message["product"]].bidSide.delete(message["id"])
    else:
        bs[message["product"]].askSide.delete(message["id"])
def tradeOrder(bs: {str, book.Book}, message: {str, str}):
    """Order traded."""
    if (message["tradeType"] == "BUY_AGGRESSOR"):
        bs[message["product"]].bidSide.delete(message["passiveOrder"])
    elif (message["tradeType"] == "SELL_AGGRESSOR"):
        bs[message["product"]].askSide.delete(message["passiveOrder"])
def broker(bs: {str, book.Book}, message: {str, str}):
    """Broker trades."""
    pass
def send(message):
    """Send message with exchange."""
    return httpx.post(f"http://{HOST}:{PORT}/execution",
                      data={"message": json.dumps(message),
                            "username": USER,
                            "password": PASS}).json()
class OrderEnum(Enum):
    """Enum for ordering from the exchange."""
    ADD = 0,
    DELETE = 1,
    BROKER_REQUEST = 2,
    BROKER_CONFIRM = 3
def order(orderType: OrderEnum, product: str, price: float = 0,
          side: book.OrderSide = book.OrderSide.Buy,
          volume: int = 0, orderId: str = "", counterparty: str = ""):
    """Order from exchange."""
    if (orderType == OrderEnum.ADD):
        resp = send({"type": "ADD",
                     "product": product,
                     "price": price,
                     "side": side,
                     "volume": volume})
    elif (orderType == OrderEnum.DELETE):
        resp = send({"type": "DELETE",
                     "product": product,
                     "id": id})
    elif (orderType == OrderEnum.BROKER_REQUEST):
        pass
    elif (orderType == OrderEnum.BROKER_CONFIRM):
        pass
    return resp