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