commit
This commit is contained in:
163
bot bond sell.py
163
bot bond sell.py
@@ -45,24 +45,29 @@ def main():
|
|||||||
# unlikely it will be traded against. Maybe there is a better price to
|
# unlikely it will be traded against. Maybe there is a better price to
|
||||||
# pick? Also, you will need to send more orders over time.
|
# pick? Also, you will need to send more orders over time.
|
||||||
# --- 설정 ---
|
# --- 설정 ---
|
||||||
BOND_FAIR_VALUE = 1000 # BOND fair value (고정)
|
BOND_FAIR_VALUE = 1000 # BOND fair value (고정)
|
||||||
BOND_ORDER_SIZE = 30 # BOND 주문당 수량
|
BOND_ORDER_SIZE = 30 # BOND 주문당 수량
|
||||||
BOND_MAX_POSITION = 100 # BOND 최대 포지션 한도
|
BOND_MAX_POSITION = 100 # BOND 최대 포지션 한도
|
||||||
XLF_CONVERSION_FEE = 100 # XLF 변환 비용 (설명서: 100)
|
XLF_CONVERSION_FEE = 100 # XLF 변환 비용
|
||||||
XLF_MAX_POSITION = 100 # XLF 포지션 한도
|
XLF_MAX_POSITION = 100 # XLF 포지션 한도
|
||||||
REFRESH_INTERVAL = 5.0 # 주문 갱신 주기 (초)
|
VALE_CONVERSION_FEE = 10 # VALE 변환 비용
|
||||||
|
VALE_MAX_POSITION = 10 # VALE/VALBZ 포지션 한도
|
||||||
|
REFRESH_INTERVAL = 5.0 # 주문 갱신 주기 (초)
|
||||||
|
|
||||||
# XLF state machine
|
# XLF state machine
|
||||||
# IDLE → BUYING_BASKET → CONVERTING → SELLING_XLF → IDLE (바스켓→XLF)
|
|
||||||
# IDLE → BUYING_XLF → CONVERTING → SELLING_BASKET → IDLE (XLF→바스켓)
|
|
||||||
xlf_state = "IDLE"
|
xlf_state = "IDLE"
|
||||||
xlf_pending = {} # {order_id: remaining_size} 부분 체결 추적
|
xlf_pending = {}
|
||||||
xlf_direction = None # "BASKET_TO_XLF" or "XLF_TO_BASKET"
|
xlf_direction = None
|
||||||
|
|
||||||
state = StateManager()
|
# VALE state machine
|
||||||
om = OrderManager()
|
vale_state = "IDLE"
|
||||||
market_open = False
|
vale_pending = {}
|
||||||
active_orders = {} # BOND 전용 {order_id: {"dir": ..., "price": ...}}
|
vale_direction = None
|
||||||
|
|
||||||
|
state = StateManager()
|
||||||
|
om = OrderManager()
|
||||||
|
market_open = False
|
||||||
|
active_orders = {} # BOND 전용
|
||||||
|
|
||||||
last_refresh = time.time()
|
last_refresh = time.time()
|
||||||
vale_last_print = time.time()
|
vale_last_print = time.time()
|
||||||
@@ -83,20 +88,17 @@ def main():
|
|||||||
|
|
||||||
cancel_all_bond_orders()
|
cancel_all_bond_orders()
|
||||||
|
|
||||||
buy_price = BOND_FAIR_VALUE - 1 # 999
|
buy_price = BOND_FAIR_VALUE - 1
|
||||||
sell_price = BOND_FAIR_VALUE + 1 # 1001
|
sell_price = BOND_FAIR_VALUE + 1
|
||||||
position = state.get_position("BOND")
|
position = state.get_position("BOND")
|
||||||
|
|
||||||
# 포지션에 따라 size 비대칭 조정
|
|
||||||
base_size = BOND_ORDER_SIZE
|
base_size = BOND_ORDER_SIZE
|
||||||
adjustment = abs(position) // 5
|
adjustment = abs(position) // 5
|
||||||
|
|
||||||
if position < 0:
|
if position < 0:
|
||||||
# 숏 포지션 → 매수를 더 많이
|
|
||||||
buy_size = min(base_size + adjustment, BOND_MAX_POSITION - position)
|
buy_size = min(base_size + adjustment, BOND_MAX_POSITION - position)
|
||||||
sell_size = max(base_size - adjustment, 1)
|
sell_size = max(base_size - adjustment, 1)
|
||||||
elif position > 0:
|
elif position > 0:
|
||||||
# 롱 포지션 → 매도를 더 많이
|
|
||||||
buy_size = max(base_size - adjustment, 1)
|
buy_size = max(base_size - adjustment, 1)
|
||||||
sell_size = min(base_size + adjustment, BOND_MAX_POSITION + position)
|
sell_size = min(base_size + adjustment, BOND_MAX_POSITION + position)
|
||||||
else:
|
else:
|
||||||
@@ -133,7 +135,6 @@ def main():
|
|||||||
ms_ask = state.ask_prices["MS"]
|
ms_ask = state.ask_prices["MS"]
|
||||||
wfc_ask = state.ask_prices["WFC"]
|
wfc_ask = state.ask_prices["WFC"]
|
||||||
xlf_bid = state.bid_prices["XLF"]
|
xlf_bid = state.bid_prices["XLF"]
|
||||||
|
|
||||||
bond_bid = state.bid_prices["BOND"]
|
bond_bid = state.bid_prices["BOND"]
|
||||||
gs_bid = state.bid_prices["GS"]
|
gs_bid = state.bid_prices["GS"]
|
||||||
ms_bid = state.bid_prices["MS"]
|
ms_bid = state.bid_prices["MS"]
|
||||||
@@ -144,7 +145,6 @@ def main():
|
|||||||
bond_bid, gs_bid, ms_bid, wfc_bid, xlf_ask]:
|
bond_bid, gs_bid, ms_bid, wfc_bid, xlf_ask]:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 바스켓 비용/수익 계산 (10주 기준)
|
|
||||||
basket_ask = bond_ask*3 + gs_ask*2 + ms_ask*3 + wfc_ask*2
|
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
|
basket_bid = bond_bid*3 + gs_bid*2 + ms_bid*3 + wfc_bid*2
|
||||||
|
|
||||||
@@ -155,8 +155,6 @@ def main():
|
|||||||
xlf_state = "BUYING_BASKET"
|
xlf_state = "BUYING_BASKET"
|
||||||
xlf_direction = "BASKET_TO_XLF"
|
xlf_direction = "BASKET_TO_XLF"
|
||||||
xlf_pending.clear()
|
xlf_pending.clear()
|
||||||
|
|
||||||
# 바스켓 구성 종목 매수 (size 추적)
|
|
||||||
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(oid, sym, Dir.BUY, state.ask_prices[sym], qty)
|
exchange.send_add_message(oid, sym, Dir.BUY, state.ask_prices[sym], qty)
|
||||||
@@ -170,35 +168,90 @@ def main():
|
|||||||
xlf_state = "BUYING_XLF"
|
xlf_state = "BUYING_XLF"
|
||||||
xlf_direction = "XLF_TO_BASKET"
|
xlf_direction = "XLF_TO_BASKET"
|
||||||
xlf_pending.clear()
|
xlf_pending.clear()
|
||||||
|
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(oid, "XLF", Dir.BUY, xlf_ask, 10)
|
exchange.send_add_message(oid, "XLF", Dir.BUY, xlf_ask, 10)
|
||||||
xlf_pending[oid] = 10
|
xlf_pending[oid] = 10
|
||||||
|
|
||||||
def handle_xlf_fill(order_id: int, symbol: str, dir_: Dir, qty: int):
|
def handle_xlf_fill(order_id, symbol, dir_, qty):
|
||||||
"""XLF state machine 체결 처리 (부분 체결 추적)"""
|
"""XLF state machine 체결 처리"""
|
||||||
nonlocal xlf_state, xlf_pending
|
nonlocal xlf_state, xlf_pending
|
||||||
|
|
||||||
if order_id not in xlf_pending:
|
if order_id not in xlf_pending:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 부분 체결 차감
|
|
||||||
xlf_pending[order_id] -= qty
|
xlf_pending[order_id] -= qty
|
||||||
if xlf_pending[order_id] <= 0:
|
if xlf_pending[order_id] <= 0:
|
||||||
del xlf_pending[order_id]
|
del xlf_pending[order_id]
|
||||||
|
|
||||||
if xlf_state == "BUYING_BASKET" and not xlf_pending:
|
if xlf_state == "BUYING_BASKET" and not xlf_pending:
|
||||||
# 바스켓 전부 체결 → XLF로 변환
|
|
||||||
print(" 바스켓 매수 완료 → XLF 변환 시작")
|
print(" 바스켓 매수 완료 → XLF 변환 시작")
|
||||||
xlf_state = "CONVERTING"
|
xlf_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "XLF", Dir.BUY, 10)
|
exchange.send_convert_message(next_id(), "XLF", Dir.BUY, 10)
|
||||||
|
|
||||||
elif xlf_state == "BUYING_XLF" and not xlf_pending:
|
elif xlf_state == "BUYING_XLF" and not xlf_pending:
|
||||||
# XLF 매수 완료 → 바스켓으로 변환
|
|
||||||
print(" XLF 매수 완료 → 바스켓 변환 시작")
|
print(" XLF 매수 완료 → 바스켓 변환 시작")
|
||||||
xlf_state = "CONVERTING"
|
xlf_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "XLF", Dir.SELL, 10)
|
exchange.send_convert_message(next_id(), "XLF", Dir.SELL, 10)
|
||||||
|
|
||||||
|
def try_vale_arb():
|
||||||
|
"""VALE/VALBZ 차익거래 시도 - IDLE 상태일 때만"""
|
||||||
|
nonlocal vale_state, vale_direction, vale_pending
|
||||||
|
|
||||||
|
if not market_open or vale_state != "IDLE":
|
||||||
|
return
|
||||||
|
|
||||||
|
vale_bid = state.bid_prices["VALE"]
|
||||||
|
vale_ask = state.ask_prices["VALE"]
|
||||||
|
valbz_bid = state.bid_prices["VALBZ"]
|
||||||
|
valbz_ask = state.ask_prices["VALBZ"]
|
||||||
|
|
||||||
|
if None in [vale_bid, vale_ask, valbz_bid, valbz_ask]:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 케이스 1: VALE가 비쌀 때 → VALBZ 매수 → VALE 변환 → VALE 매도
|
||||||
|
profit1 = vale_bid - valbz_ask - VALE_CONVERSION_FEE
|
||||||
|
if profit1 > 0 and state.get_position("VALBZ") < VALE_MAX_POSITION:
|
||||||
|
print(f" VALE 차익(VALBZ→VALE) 시작, 예상수익:{profit1}")
|
||||||
|
vale_state = "BUYING_VALBZ"
|
||||||
|
vale_direction = "VALBZ_TO_VALE"
|
||||||
|
vale_pending.clear()
|
||||||
|
oid = next_id()
|
||||||
|
exchange.send_add_message(oid, "VALBZ", Dir.BUY, valbz_ask, 1)
|
||||||
|
vale_pending[oid] = 1
|
||||||
|
return
|
||||||
|
|
||||||
|
# 케이스 2: VALBZ가 비쌀 때 → VALE 매수 → VALBZ 변환 → VALBZ 매도
|
||||||
|
profit2 = valbz_bid - vale_ask - VALE_CONVERSION_FEE
|
||||||
|
if profit2 > 0 and state.get_position("VALE") < VALE_MAX_POSITION:
|
||||||
|
print(f" VALE 차익(VALE→VALBZ) 시작, 예상수익:{profit2}")
|
||||||
|
vale_state = "BUYING_VALE"
|
||||||
|
vale_direction = "VALE_TO_VALBZ"
|
||||||
|
vale_pending.clear()
|
||||||
|
oid = next_id()
|
||||||
|
exchange.send_add_message(oid, "VALE", Dir.BUY, vale_ask, 1)
|
||||||
|
vale_pending[oid] = 1
|
||||||
|
|
||||||
|
def handle_vale_fill(order_id, symbol, dir_, qty):
|
||||||
|
"""VALE state machine 체결 처리"""
|
||||||
|
nonlocal vale_state, vale_pending
|
||||||
|
|
||||||
|
if order_id not in vale_pending:
|
||||||
|
return
|
||||||
|
|
||||||
|
vale_pending[order_id] -= qty
|
||||||
|
if vale_pending[order_id] <= 0:
|
||||||
|
del vale_pending[order_id]
|
||||||
|
|
||||||
|
if vale_state == "BUYING_VALBZ" and not vale_pending:
|
||||||
|
print(" VALBZ 매수 완료 → VALE 변환 시작")
|
||||||
|
vale_state = "CONVERTING"
|
||||||
|
exchange.send_convert_message(next_id(), "VALE", Dir.BUY, 1)
|
||||||
|
|
||||||
|
elif vale_state == "BUYING_VALE" and not vale_pending:
|
||||||
|
print(" VALE 매수 완료 → VALBZ 변환 시작")
|
||||||
|
vale_state = "CONVERTING"
|
||||||
|
exchange.send_convert_message(next_id(), "VALE", Dir.SELL, 1)
|
||||||
|
|
||||||
# Set up some variables to track the bid and ask price of a symbol. Right
|
# Set up some variables to track the bid and ask price of a symbol. Right
|
||||||
# now this doesn't track much information, but it's enough to get a sense
|
# now this doesn't track much information, but it's enough to get a sense
|
||||||
# of the VALE market.
|
# of the VALE market.
|
||||||
@@ -242,19 +295,22 @@ def main():
|
|||||||
print(message)
|
print(message)
|
||||||
oid = message.get("order_id")
|
oid = message.get("order_id")
|
||||||
active_orders.pop(oid, None)
|
active_orders.pop(oid, None)
|
||||||
# XLF 주문 reject 시 IDLE로 복귀
|
|
||||||
if oid in xlf_pending:
|
if oid in xlf_pending:
|
||||||
print(f" XLF 주문 reject → IDLE 복귀")
|
print(" XLF 주문 reject → IDLE 복귀")
|
||||||
xlf_state = "IDLE"
|
xlf_state = "IDLE"
|
||||||
xlf_pending.clear()
|
xlf_pending.clear()
|
||||||
xlf_direction = None
|
xlf_direction = None
|
||||||
|
if oid in vale_pending:
|
||||||
|
print(" VALE 주문 reject → IDLE 복귀")
|
||||||
|
vale_state = "IDLE"
|
||||||
|
vale_pending.clear()
|
||||||
|
vale_direction = None
|
||||||
|
|
||||||
elif message["type"] == "ack":
|
elif message["type"] == "ack":
|
||||||
# 변환 ack 처리 (convert는 fill이 아닌 ack로 완료됨)
|
# XLF 변환 ack 처리
|
||||||
if xlf_state == "CONVERTING":
|
if xlf_state == "CONVERTING":
|
||||||
print(" 변환 완료 → 매도 시작")
|
print(" XLF 변환 완료 → 매도 시작")
|
||||||
if xlf_direction == "BASKET_TO_XLF":
|
if xlf_direction == "BASKET_TO_XLF":
|
||||||
# XLF 매도
|
|
||||||
xlf_state = "SELLING_XLF"
|
xlf_state = "SELLING_XLF"
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
@@ -262,7 +318,6 @@ def main():
|
|||||||
)
|
)
|
||||||
xlf_pending[oid] = 10
|
xlf_pending[oid] = 10
|
||||||
elif xlf_direction == "XLF_TO_BASKET":
|
elif xlf_direction == "XLF_TO_BASKET":
|
||||||
# 바스켓 각 종목 매도
|
|
||||||
xlf_state = "SELLING_BASKET"
|
xlf_state = "SELLING_BASKET"
|
||||||
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
@@ -271,6 +326,24 @@ def main():
|
|||||||
)
|
)
|
||||||
xlf_pending[oid] = qty
|
xlf_pending[oid] = qty
|
||||||
|
|
||||||
|
# VALE 변환 ack 처리
|
||||||
|
elif vale_state == "CONVERTING":
|
||||||
|
print(" VALE 변환 완료 → 매도 시작")
|
||||||
|
if vale_direction == "VALBZ_TO_VALE":
|
||||||
|
vale_state = "SELLING_VALE"
|
||||||
|
oid = next_id()
|
||||||
|
exchange.send_add_message(
|
||||||
|
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], 1
|
||||||
|
)
|
||||||
|
vale_pending[oid] = 1
|
||||||
|
elif vale_direction == "VALE_TO_VALBZ":
|
||||||
|
vale_state = "SELLING_VALBZ"
|
||||||
|
oid = next_id()
|
||||||
|
exchange.send_add_message(
|
||||||
|
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], 1
|
||||||
|
)
|
||||||
|
vale_pending[oid] = 1
|
||||||
|
|
||||||
elif message["type"] == "fill":
|
elif message["type"] == "fill":
|
||||||
print(message)
|
print(message)
|
||||||
qty = message["size"]
|
qty = message["size"]
|
||||||
@@ -293,13 +366,17 @@ def main():
|
|||||||
|
|
||||||
# XLF state machine 체결 처리
|
# XLF state machine 체결 처리
|
||||||
handle_xlf_fill(oid, sym, dir_, qty)
|
handle_xlf_fill(oid, sym, dir_, qty)
|
||||||
|
if xlf_state in ("SELLING_XLF", "SELLING_BASKET") and not xlf_pending:
|
||||||
|
print(" XLF 차익거래 완료 → IDLE 복귀")
|
||||||
|
xlf_state = "IDLE"
|
||||||
|
xlf_direction = None
|
||||||
|
|
||||||
# SELLING 단계에서 pending 소진 시 IDLE 복귀
|
# VALE state machine 체결 처리
|
||||||
if xlf_state in ("SELLING_XLF", "SELLING_BASKET"):
|
handle_vale_fill(oid, sym, dir_, qty)
|
||||||
if not xlf_pending:
|
if vale_state in ("SELLING_VALE", "SELLING_VALBZ") and not vale_pending:
|
||||||
print(" XLF 차익거래 완료 → IDLE 복귀")
|
print(" VALE 차익거래 완료 → IDLE 복귀")
|
||||||
xlf_state = "IDLE"
|
vale_state = "IDLE"
|
||||||
xlf_direction = None
|
vale_direction = None
|
||||||
|
|
||||||
elif message["type"] == "book":
|
elif message["type"] == "book":
|
||||||
sym = message["symbol"]
|
sym = message["symbol"]
|
||||||
@@ -322,6 +399,10 @@ def main():
|
|||||||
if sym in ["BOND", "GS", "MS", "WFC", "XLF"]:
|
if sym in ["BOND", "GS", "MS", "WFC", "XLF"]:
|
||||||
try_xlf_arb()
|
try_xlf_arb()
|
||||||
|
|
||||||
|
# VALE/VALBZ 호가 업데이트마다 차익거래 시도
|
||||||
|
if sym in ["VALE", "VALBZ"]:
|
||||||
|
try_vale_arb()
|
||||||
|
|
||||||
# 주기적으로 BOND 주문 갱신 (주문 만료 방지)
|
# 주기적으로 BOND 주문 갱신 (주문 만료 방지)
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if now - last_refresh > REFRESH_INTERVAL:
|
if now - last_refresh > REFRESH_INTERVAL:
|
||||||
|
|||||||
Reference in New Issue
Block a user