From d3d6ddeeb4075de6e76b099f501f705a4fec8e24 Mon Sep 17 00:00:00 2001 From: khwkim1111 Date: Sat, 9 May 2026 14:40:27 +0900 Subject: [PATCH] commit --- prac.py | 97 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/prac.py b/prac.py index 2a0a3b3..6101624 100644 --- a/prac.py +++ b/prac.py @@ -10,7 +10,8 @@ team_name = "HanyangFloorFunction" # ==================== ์„ค์ • ==================== ORDER_SIZE = 2 -MAX_POS = 30 +MAX_POS = 20 +ARB_THRESHOLD = 40 # ๐Ÿ”ฅ ๋ณด์ˆ˜์ ์œผ๋กœ (์ค‘์š”) # ==================== class Dir(str, Enum): @@ -25,19 +26,17 @@ def main(): state = StateManager() hello = exchange.read_message() - # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ง์ ‘ ๊ด€๋ฆฌ + # ํฌ์ง€์…˜ ์ง์ ‘ ๊ด€๋ฆฌ positions = {} for sym in hello["symbols"]: positions[sym["symbol"]] = sym["position"] - # ๐Ÿ”ฅ order id order_id = 0 def next_id(): nonlocal order_id order_id += 1 return order_id - # ๐Ÿ”ฅ active order ๊ด€๋ฆฌ (ํ•ต์‹ฌ) active_orders = {} # ==================== MARKET MAKING ==================== @@ -48,34 +47,34 @@ def main(): if bid is None or ask is None: return + if ask - bid <= 2: # ๐Ÿ”ฅ ์Šคํ”„๋ ˆ๋“œ ๋„ˆ๋ฌด ์ข์œผ๋ฉด ํŒจ์Šค + return + pos = positions.get(sym, 0) - # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ œํ•œ - if pos > MAX_POS: - return - if pos < -MAX_POS: + if abs(pos) > MAX_POS: return - # ๐Ÿ”ฅ ํ•ต์‹ฌ: ์Šคํ”„๋ ˆ๋“œ ๋‚ด๋ถ€์—์„œ๋งŒ ์ฃผ๋ฌธ buy_price = bid + 1 sell_price = ask - 1 - # ๐Ÿ”ฅ ํฌ์ง€์…˜ ๋ฐฉํ–ฅ ์ œ์–ด + if buy_price >= sell_price: + return + + # ํฌ์ง€์…˜ ์กฐ์ ˆ if pos > 0: buy_size = 1 - sell_size = ORDER_SIZE + 2 + sell_size = ORDER_SIZE + 1 elif pos < 0: - buy_size = ORDER_SIZE + 2 + buy_size = ORDER_SIZE + 1 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 @@ -85,15 +84,52 @@ def main(): 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, 5) + if ask and ask < 999: + exchange.send_add_message_ioc(next_id(), "BOND", Dir.BUY, ask, 3) - if bid and bid > 1000: - exchange.send_add_message_ioc(next_id(), "BOND", Dir.SELL, bid, 5) + if bid and bid > 1001: + exchange.send_add_message_ioc(next_id(), "BOND", Dir.SELL, bid, 3) + + # ==================== XLF ์ฐจ์ต๊ฑฐ๋ž˜ (์ดˆ์•ˆ์ „ ๋ฒ„์ „) ==================== + def xlf_arb(): + bond_ask = state.ask_prices.get("BOND") + gs_ask = state.ask_prices.get("GS") + ms_ask = state.ask_prices.get("MS") + wfc_ask = state.ask_prices.get("WFC") + xlf_bid = state.bid_prices.get("XLF") + + bond_bid = state.bid_prices.get("BOND") + gs_bid = state.bid_prices.get("GS") + ms_bid = state.bid_prices.get("MS") + wfc_bid = state.bid_prices.get("WFC") + xlf_ask = state.ask_prices.get("XLF") + + if None in [bond_ask, gs_ask, ms_ask, wfc_ask, xlf_bid, + bond_bid, gs_bid, ms_bid, wfc_bid, xlf_ask]: + return + + basket_ask = bond_ask*3 + gs_ask*2 + ms_ask*3 + wfc_ask*2 + basket_bid = bond_bid*3 + gs_bid*2 + ms_bid*3 + wfc_bid*2 + + pos = positions.get("XLF", 0) + + # ๐Ÿ”ฅ ํฌ์ง€์…˜ ์ œํ•œ + if abs(pos) > 10: + return + + # ๐Ÿ”ฅ ์ผ€์ด์Šค 1 + profit1 = xlf_bid * 10 - basket_ask + if profit1 > ARB_THRESHOLD: + exchange.send_add_message_ioc(next_id(), "XLF", Dir.SELL, xlf_bid, 10) + + # ๐Ÿ”ฅ ์ผ€์ด์Šค 2 + profit2 = basket_bid - xlf_ask * 10 + if profit2 > ARB_THRESHOLD: + exchange.send_add_message_ioc(next_id(), "XLF", Dir.BUY, xlf_ask, 10) # ==================== ๋ฆฌ์Šคํฌ ๊ด€๋ฆฌ ==================== def risk(): - for sym in ["GS", "MS", "WFC"]: + for sym in ["GS", "MS", "WFC", "XLF"]: pos = positions.get(sym, 0) if abs(pos) > MAX_POS: @@ -123,13 +159,14 @@ def main(): state.update_bid_ask_price(sym, bid, ask) - # ๐Ÿ”ฅ ๊ธฐ์กด ์ฃผ๋ฌธ ์ „๋ถ€ ์ทจ์†Œ (ํ•ต์‹ฌ) + # ๐Ÿ”ฅ ๋ชจ๋“  ์ฃผ๋ฌธ ์ทจ์†Œ (ํ•ต์‹ฌ) for oid in list(active_orders.keys()): exchange.send_cancel_message(oid) del active_orders[oid] # ์ „๋žต ์‹คํ–‰ bond_arb() + xlf_arb() if sym in ["GS", "MS", "WFC"]: market_make(sym) @@ -161,26 +198,10 @@ 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})