This commit is contained in:
2026-05-09 14:31:44 +09:00
parent d8758fafe9
commit 5c5775338a

132
prac.py
View File

@@ -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())