commit
This commit is contained in:
97
prac.py
97
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})
|
||||
|
||||
Reference in New Issue
Block a user