diff --git a/prac.py b/prac.py index 15acdee..2a0a3b3 100644 --- a/prac.py +++ b/prac.py @@ -9,10 +9,8 @@ from state import StateManager team_name = "HanyangFloorFunction" # ==================== ์„ค์ • ==================== -STOCK_SPREAD = 5 -ORDER_SIZE = 5 -MIN_PROFIT = 5 -MAX_POS = 50 +ORDER_SIZE = 2 +MAX_POS = 30 # ==================== class Dir(str, Enum): @@ -27,7 +25,7 @@ def main(): state = StateManager() hello = exchange.read_message() - # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ง์ ‘ ๊ด€๋ฆฌ (ํ•ต์‹ฌ) + # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ง์ ‘ ๊ด€๋ฆฌ positions = {} for sym in hello["symbols"]: positions[sym["symbol"]] = sym["position"] @@ -39,56 +37,61 @@ def main(): order_id += 1 return order_id - # ==================== ์ „๋žต ==================== + # ๐Ÿ”ฅ active order ๊ด€๋ฆฌ (ํ•ต์‹ฌ) + active_orders = {} + # ==================== MARKET MAKING ==================== + def market_make(sym): + bid = state.bid_prices.get(sym) + ask = state.ask_prices.get(sym) + + if bid is None or ask is None: + return + + pos = positions.get(sym, 0) + + # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ œํ•œ + if pos > MAX_POS: + return + if pos < -MAX_POS: + return + + # ๐Ÿ”ฅ ํ•ต์‹ฌ: ์Šคํ”„๋ ˆ๋“œ ๋‚ด๋ถ€์—์„œ๋งŒ ์ฃผ๋ฌธ + buy_price = bid + 1 + sell_price = ask - 1 + + # ๐Ÿ”ฅ ํฌ์ง€์…˜ ๋ฐฉํ–ฅ ์ œ์–ด + if pos > 0: + buy_size = 1 + sell_size = ORDER_SIZE + 2 + elif pos < 0: + buy_size = ORDER_SIZE + 2 + sell_size = 1 + else: + buy_size = sell_size = ORDER_SIZE + + # BUY + oid = next_id() + exchange.send_add_message(oid, sym, Dir.BUY, buy_price, buy_size) + active_orders[oid] = sym + + # SELL + oid = next_id() + exchange.send_add_message(oid, sym, Dir.SELL, sell_price, sell_size) + active_orders[oid] = sym + + # ==================== BOND ์•ˆ์ „ ์ˆ˜์ต ==================== def bond_arb(): bid = state.bid_prices.get("BOND") ask = state.ask_prices.get("BOND") if ask and ask < 1000: - exchange.send_add_message_ioc(next_id(), "BOND", Dir.BUY, ask, 10) + exchange.send_add_message_ioc(next_id(), "BOND", Dir.BUY, ask, 5) if bid and bid > 1000: - exchange.send_add_message_ioc(next_id(), "BOND", Dir.SELL, bid, 10) - - def vale_arb(): - vale_bid = state.bid_prices.get("VALE") - valbz_ask = state.ask_prices.get("VALBZ") - - if vale_bid and valbz_ask: - if vale_bid - valbz_ask > MIN_PROFIT: - exchange.send_add_message_ioc(next_id(), "VALBZ", Dir.BUY, valbz_ask, 1) - - def xlf_arb(): - fair = state.get_fair_value("XLF") - bid = state.bid_prices.get("XLF") - - if fair and bid: - if bid - fair > MIN_PROFIT: - exchange.send_add_message_ioc(next_id(), "XLF", Dir.SELL, bid, 10) - - def market_make(sym): - mid = state.get_mid_price(sym) - if mid is None: - return - - pos = positions.get(sym, 0) - - buy_price = mid - STOCK_SPREAD - sell_price = mid + STOCK_SPREAD - - if pos > 0: - buy_size = 1 - sell_size = ORDER_SIZE + 3 - elif pos < 0: - buy_size = ORDER_SIZE + 3 - sell_size = 1 - else: - buy_size = sell_size = ORDER_SIZE - - exchange.send_add_message(next_id(), sym, Dir.BUY, buy_price, buy_size) - exchange.send_add_message(next_id(), sym, Dir.SELL, sell_price, sell_size) + exchange.send_add_message_ioc(next_id(), "BOND", Dir.SELL, bid, 5) + # ==================== ๋ฆฌ์Šคํฌ ๊ด€๋ฆฌ ==================== def risk(): for sym in ["GS", "MS", "WFC"]: pos = positions.get(sym, 0) @@ -97,12 +100,12 @@ def main(): if pos > 0: exchange.send_add_message_ioc( next_id(), sym, Dir.SELL, - state.bid_prices.get(sym), abs(pos) + state.bid_prices.get(sym, 1), abs(pos) ) else: exchange.send_add_message_ioc( next_id(), sym, Dir.BUY, - state.ask_prices.get(sym), abs(pos) + state.ask_prices.get(sym, 99999), abs(pos) ) # ==================== LOOP ==================== @@ -119,12 +122,14 @@ def main(): ask = msg["sell"][0][0] if msg["sell"] else None state.update_bid_ask_price(sym, bid, ask) - state.update_predicted_price(sym) - state.update_fair_value() + # ๐Ÿ”ฅ ๊ธฐ์กด ์ฃผ๋ฌธ ์ „๋ถ€ ์ทจ์†Œ (ํ•ต์‹ฌ) + for oid in list(active_orders.keys()): + exchange.send_cancel_message(oid) + del active_orders[oid] + + # ์ „๋žต ์‹คํ–‰ bond_arb() - vale_arb() - xlf_arb() if sym in ["GS", "MS", "WFC"]: market_make(sym) @@ -156,10 +161,29 @@ class ExchangeConnection: return msg def send_add_message(self, oid, sym, dir, price, size): - self._write({"type":"add","order_id":oid,"symbol":sym,"dir":dir,"price":price,"size":size,"tif":"DAY"}) + self._write({ + "type":"add", + "order_id":oid, + "symbol":sym, + "dir":dir, + "price":price, + "size":size, + "tif":"DAY" + }) def send_add_message_ioc(self, oid, sym, dir, price, size): - self._write({"type":"add","order_id":oid,"symbol":sym,"dir":dir,"price":price,"size":size,"tif":"IOC"}) + self._write({ + "type":"add", + "order_id":oid, + "symbol":sym, + "dir":dir, + "price":price, + "size":size, + "tif":"IOC" + }) + + def send_cancel_message(self, oid): + self._write({"type": "cancel", "order_id": oid}) def _write(self, msg): self.writer.send((json.dumps(msg)+"\n").encode())