#!/usr/bin/env python3 import argparse, socket, json, time from enum import Enum from state import StateManager team_name = "HanyangFloorFunction" # 🔥 핵심 파라미터 ORDER_SIZE = 5 MAX_POS = 50 KILL_POS = 70 ARB_THRESHOLD = 6 REFRESH = 0.5 class Dir(str, Enum): BUY = "BUY" SELL = "SELL" # ===================== class ExchangeConnection: def __init__(self, args): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((args.exchange_hostname, args.port)) self.reader = s.makefile("r", 1) self.writer = s self._write({"type": "hello", "team": team_name.upper()}) def read(self): m = json.loads(self.reader.readline()) if "dir" in m: m["dir"] = Dir(m["dir"]) return m def send(self, m): self.writer.send((json.dumps(m)+"\n").encode()) def add(self, oid, sym, d, px, sz, tif="DAY"): self.send({"type":"add","order_id":oid,"symbol":sym,"dir":d,"price":px,"size":sz,"tif":tif}) def ioc(self, oid, sym, d, px, sz): self.add(oid, sym, d, px, sz, "IOC") def cancel(self, oid): self.send({"type":"cancel","order_id":oid}) def convert(self, oid, sym, d, sz): self.send({"type":"convert","order_id":oid,"symbol":sym,"dir":d,"size":sz}) # ===================== def parse_arguments(): p = argparse.ArgumentParser() g = p.add_mutually_exclusive_group(required=True) g.add_argument("--production", action="store_true") g.add_argument("--test", type=str, default="prod-like") a = p.parse_args() if a.production: a.exchange_hostname, a.port = "production", 25000 else: a.exchange_hostname, a.port = "test-exch-"+team_name, 22000 return a # ===================== def main(): args = parse_arguments() ex = ExchangeConnection(args) st = StateManager() hello = ex.read() pos = {s["symbol"]: s["position"] for s in hello["symbols"]} oid = 0 def nid(): nonlocal oid; oid += 1; return oid active = {} last_refresh = 0 # 🔥 imbalance 계산 def imbalance(sym): bid = st.bid_depths.get(sym, 0) ask = st.ask_depths.get(sym, 0) return bid - ask # 🔥 alpha 방향 결정 def alpha(sym): imb = imbalance(sym) if imb > 0: return 1 # 상승 elif imb < 0: return -1 # 하락 return 0 # 🔥 Adaptive MM def market_make(sym): b = st.bid_prices.get(sym) a = st.ask_prices.get(sym) if b is None or a is None: return p = pos.get(sym, 0) if abs(p) > MAX_POS: return spread = a - b direction = alpha(sym) # 🔥 방향 반영 if direction > 0: buy_px = b + 2 sell_px = a - 1 elif direction < 0: buy_px = b + 1 sell_px = a - 2 else: buy_px = b + 1 sell_px = a - 1 if buy_px >= sell_px: return buy_sz = ORDER_SIZE sell_sz = ORDER_SIZE if p > 0: sell_sz += 2 elif p < 0: buy_sz += 2 o = nid(); ex.add(o, sym, Dir.BUY, buy_px, buy_sz); active[o]=sym o = nid(); ex.add(o, sym, Dir.SELL, sell_px, sell_sz); active[o]=sym # 🔥 XLF arb (유지) def xlf_arb(): bond_ask = st.ask_prices.get("BOND") gs_ask = st.ask_prices.get("GS") ms_ask = st.ask_prices.get("MS") wfc_ask = st.ask_prices.get("WFC") xlf_bid = st.bid_prices.get("XLF") if None in [bond_ask, gs_ask, ms_ask, wfc_ask, xlf_bid]: return cost = bond_ask*3 + gs_ask*2 + ms_ask*3 + wfc_ask*2 profit = xlf_bid*10 - cost if profit > ARB_THRESHOLD: ex.ioc(nid(),"BOND",Dir.BUY,bond_ask,3) ex.ioc(nid(),"GS",Dir.BUY,gs_ask,2) ex.ioc(nid(),"MS",Dir.BUY,ms_ask,3) ex.ioc(nid(),"WFC",Dir.BUY,wfc_ask,2) ex.convert(nid(),"XLF",Dir.BUY,10) ex.ioc(nid(),"XLF",Dir.SELL,xlf_bid,10) # 🔥 리스크 컷 def risk(): for s, p in pos.items(): if abs(p) > KILL_POS: if p > 0: ex.ioc(nid(), s, Dir.SELL, st.bid_prices.get(s,1), abs(p)) else: ex.ioc(nid(), s, Dir.BUY, st.ask_prices.get(s,99999), abs(p)) # ================= LOOP ================= while True: m = ex.read() if m["type"] == "close": break elif m["type"] == "book": sym = m["symbol"] b = m["buy"][0][0] if m["buy"] else None a = m["sell"][0][0] if m["sell"] else None st.update_bid_ask_price(sym, b, a) st.update_depth(sym, m["buy"][0][1] if m["buy"] else 0, m["sell"][0][1] if m["sell"] else 0 ) now = time.time() if now - last_refresh > REFRESH: last_refresh = now # 전체 cancel for o in list(active.keys()): ex.cancel(o) active.pop(o, None) for s in ["GS","MS","WFC"]: market_make(s) xlf_arb() risk() elif m["type"] == "fill": s, q, d = m["symbol"], m["size"], m["dir"] pos[s] += q if d == Dir.BUY else -q # 🔥 초고속 재주문 if s in ["GS","MS","WFC"]: market_make(s) if __name__ == "__main__": main()