|
| 1 | +import asyncio |
| 2 | +from datetime import datetime |
| 3 | +from decimal import Decimal |
| 4 | +from typing import Callable |
| 5 | +from uuid import UUID |
| 6 | + |
| 7 | +import httpx |
| 8 | +from loguru import logger |
| 9 | + |
| 10 | +from ctenex.bot.orders_generators.basic import orders_generator |
| 11 | +from ctenex.domain.entities import OrderSide, OrderType |
| 12 | +from ctenex.domain.order_book.order.schemas import ( |
| 13 | + OrderAddRequest, |
| 14 | + OrderAddResponse, |
| 15 | + OrderGetResponse, |
| 16 | +) |
| 17 | +from ctenex.settings.application import get_app_settings |
| 18 | +from ctenex.utils.contracts import validate_contract_id |
| 19 | + |
| 20 | +settings = get_app_settings() |
| 21 | + |
| 22 | + |
| 23 | +BOT_TRADER_ID = UUID("c4adb8ee-1425-4a10-bd10-87dd587670d3") |
| 24 | + |
| 25 | + |
| 26 | +class InternalStateError(Exception): ... |
| 27 | + |
| 28 | + |
| 29 | +class MatchingBot: |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + trader_id: UUID, |
| 33 | + contract_id: str, |
| 34 | + base_url: str, |
| 35 | + poll_interval: float = 1.0, |
| 36 | + poll_size: int = 5, |
| 37 | + number_of_orders: int = 2, |
| 38 | + orders_generator: Callable = orders_generator, |
| 39 | + ): |
| 40 | + # Configuration |
| 41 | + self.base_url = base_url |
| 42 | + self.trader_id = trader_id |
| 43 | + self.contract_id = contract_id |
| 44 | + self.poll_interval = poll_interval |
| 45 | + self.poll_size = poll_size |
| 46 | + self.number_of_orders = number_of_orders |
| 47 | + |
| 48 | + # Dependencies |
| 49 | + self.exchange_client = httpx.AsyncClient(base_url=base_url) |
| 50 | + self.orders_generator = orders_generator |
| 51 | + |
| 52 | + # State |
| 53 | + self.last_processed_order_timestamp: datetime = datetime.now() |
| 54 | + self.mid_price: Decimal = Decimal(0.00) |
| 55 | + self.best_bid: Decimal = Decimal(0.00) |
| 56 | + self.best_ask: Decimal = Decimal(0.00) |
| 57 | + self.best_bid_quantity: Decimal = Decimal(0.00) |
| 58 | + self.best_ask_quantity: Decimal = Decimal(0.00) |
| 59 | + self.tick_size: Decimal = Decimal(0.00) |
| 60 | + self.spread: Decimal = Decimal(0.00) |
| 61 | + |
| 62 | + async def validate_contract_id(self, contract_id: str) -> None: |
| 63 | + contracts = validate_contract_id(contract_id, self.base_url) |
| 64 | + self.tick_size = next( |
| 65 | + (c.tick_size for c in contracts if c.contract_id == contract_id), |
| 66 | + Decimal(0.00), |
| 67 | + ) |
| 68 | + |
| 69 | + async def get_orders(self, contract_id: str, status: str) -> list[OrderGetResponse]: |
| 70 | + logger.info(f"Polling {status} orders for contract {contract_id}") |
| 71 | + |
| 72 | + response = await self.exchange_client.get( |
| 73 | + url="/v1/stateless/orders", |
| 74 | + params={ |
| 75 | + "contract_id": contract_id, |
| 76 | + "status": status, |
| 77 | + "limit": self.poll_size, |
| 78 | + "page": 1, |
| 79 | + }, |
| 80 | + ) |
| 81 | + response.raise_for_status() |
| 82 | + return [OrderGetResponse(**order) for order in response.json()] |
| 83 | + |
| 84 | + async def place_order(self, order: OrderAddRequest) -> OrderAddResponse: |
| 85 | + response = await self.exchange_client.post( |
| 86 | + url="/v1/stateless/orders", json=order.model_dump(mode="json") |
| 87 | + ) |
| 88 | + response.raise_for_status() |
| 89 | + logger.info(f"Placed order: {response.json()}") |
| 90 | + return OrderAddResponse(**response.json()) |
| 91 | + |
| 92 | + async def process_orders(self, orders: list[OrderGetResponse], status: str) -> None: |
| 93 | + if not orders: |
| 94 | + logger.info("No orders to process") |
| 95 | + return |
| 96 | + |
| 97 | + logger.info( |
| 98 | + f"Processing {len(orders)} {status} order(s) for contract {self.contract_id}" |
| 99 | + ) |
| 100 | + |
| 101 | + for order in orders: |
| 102 | + # Skip our own orders or already processed orders |
| 103 | + if order.trader_id == self.trader_id: |
| 104 | + continue |
| 105 | + |
| 106 | + # TODO: Handle market orders |
| 107 | + # Market orders sound like the ideal opportunity to make a profit. |
| 108 | + # How do we make sure the bot offers a fair price even for market orders? |
| 109 | + order_price = order.price if order.price is not None else self.mid_price |
| 110 | + if order.type == OrderType.MARKET and order.price is None: |
| 111 | + continue |
| 112 | + |
| 113 | + # Update state for order price and quantity decision |
| 114 | + order_quantity = ( |
| 115 | + order.remaining_quantity |
| 116 | + if order.remaining_quantity is not None |
| 117 | + else order.quantity |
| 118 | + ) |
| 119 | + |
| 120 | + self.update_state( |
| 121 | + price=order_price, |
| 122 | + side=order.side, |
| 123 | + quantity=order_quantity, |
| 124 | + ) |
| 125 | + |
| 126 | + # Generate orders around the mid-price and considering the spread |
| 127 | + generated_orders = self.orders_generator( |
| 128 | + contract_id=self.contract_id, |
| 129 | + trader_id=self.trader_id, |
| 130 | + number_of_orders=self.number_of_orders, |
| 131 | + side=order.side, |
| 132 | + price=self.mid_price, |
| 133 | + tick_size=self.tick_size, |
| 134 | + spread=self.spread, |
| 135 | + ) |
| 136 | + |
| 137 | + # Place orders |
| 138 | + for generated_order in generated_orders: |
| 139 | + try: |
| 140 | + await self.place_order(generated_order) |
| 141 | + self.last_processed_order_timestamp = datetime.now() |
| 142 | + except Exception as e: |
| 143 | + logger.error(f"Failed to place matching order: {e}") |
| 144 | + |
| 145 | + def update_state( |
| 146 | + self, |
| 147 | + price: Decimal, |
| 148 | + side: OrderSide, |
| 149 | + quantity: Decimal, |
| 150 | + ) -> None: |
| 151 | + if side == OrderSide.BUY and price > self.best_bid: |
| 152 | + self.best_bid = price |
| 153 | + self.best_bid_quantity = quantity |
| 154 | + elif side == OrderSide.SELL and ( |
| 155 | + price < self.best_ask or self.best_ask == Decimal(0.00) |
| 156 | + ): |
| 157 | + self.best_ask = price |
| 158 | + self.best_ask_quantity = quantity |
| 159 | + |
| 160 | + if self.best_bid == Decimal(0.00): |
| 161 | + self.mid_price = self.best_ask |
| 162 | + elif self.best_ask == Decimal(0.00): |
| 163 | + self.mid_price = self.best_bid |
| 164 | + else: |
| 165 | + self.mid_price = (self.best_bid + self.best_ask) / 2 |
| 166 | + self.spread = abs(self.best_ask - self.best_bid) |
| 167 | + |
| 168 | + async def run(self) -> None: |
| 169 | + logger.info(f"Starting matching bot for contract {self.contract_id}") |
| 170 | + while True: |
| 171 | + open_orders = await self.get_orders( |
| 172 | + self.contract_id, |
| 173 | + status="open", |
| 174 | + ) |
| 175 | + |
| 176 | + open_orders = [ |
| 177 | + order for order in open_orders if order.trader_id != self.trader_id |
| 178 | + ] |
| 179 | + await self.process_orders(open_orders, status="open") |
| 180 | + |
| 181 | + partially_filled_orders = await self.get_orders( |
| 182 | + self.contract_id, |
| 183 | + status="partially_filled", |
| 184 | + ) |
| 185 | + partially_filled_orders = [ |
| 186 | + order |
| 187 | + for order in partially_filled_orders |
| 188 | + if order.trader_id != self.trader_id |
| 189 | + ] |
| 190 | + await self.process_orders( |
| 191 | + partially_filled_orders, status="partially_filled" |
| 192 | + ) |
| 193 | + |
| 194 | + await asyncio.sleep(self.poll_interval) |
| 195 | + # break |
| 196 | + |
| 197 | + async def close(self) -> None: |
| 198 | + await self.exchange_client.aclose() |
| 199 | + |
| 200 | + |
| 201 | +async def main(): |
| 202 | + bot = MatchingBot( |
| 203 | + trader_id=BOT_TRADER_ID, |
| 204 | + base_url=str(settings.api.base_url), |
| 205 | + contract_id="UK-BL-MAR-25", |
| 206 | + ) |
| 207 | + try: |
| 208 | + await bot.validate_contract_id("UK-BL-MAR-25") |
| 209 | + await bot.run() |
| 210 | + except KeyboardInterrupt: |
| 211 | + logger.info("Bot stopped by user") |
| 212 | + finally: |
| 213 | + await bot.close() |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + asyncio.run(main()) |
0 commit comments