Compare commits
2 Commits
46255a8c05
...
7b2824219f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b2824219f | |||
| 90a024fa83 |
119
bot bond sell.py
119
bot bond sell.py
@@ -46,28 +46,35 @@ def main():
|
|||||||
# 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 = 80 # BOND 주문당 수량 (크게 설정해서 체결 기회 극대화)
|
||||||
XLF_CONVERSION_FEE = 100 # XLF 변환 비용
|
XLF_CONVERSION_FEE = 100 # XLF 변환 비용
|
||||||
VALE_CONVERSION_FEE = 10 # VALE 변환 비용
|
VALE_CONVERSION_FEE = 10 # VALE 변환 비용
|
||||||
|
VALE_ARB_SIZE = 10 # VALE 차익거래 단위
|
||||||
REFRESH_INTERVAL = 5.0 # 주문 갱신 주기 (초)
|
REFRESH_INTERVAL = 5.0 # 주문 갱신 주기 (초)
|
||||||
|
|
||||||
# XLF state machine
|
# XLF state machine
|
||||||
|
# IDLE → BUYING_BASKET → CONVERTING → SELLING_XLF → IDLE
|
||||||
|
# IDLE → BUYING_XLF → CONVERTING → SELLING_BASKET → IDLE
|
||||||
xlf_state = "IDLE"
|
xlf_state = "IDLE"
|
||||||
xlf_pending = {}
|
xlf_pending = {}
|
||||||
xlf_direction = None
|
xlf_direction = None
|
||||||
|
xlf_arb_size = 0 # 현재 차익거래 수량 추적
|
||||||
|
|
||||||
# VALE state machine
|
# VALE state machine
|
||||||
|
# IDLE → BUYING_VALBZ → CONVERTING → SELLING_VALE → IDLE
|
||||||
|
# IDLE → BUYING_VALE → CONVERTING → SELLING_VALBZ → IDLE
|
||||||
vale_state = "IDLE"
|
vale_state = "IDLE"
|
||||||
vale_pending = {}
|
vale_pending = {}
|
||||||
vale_direction = None
|
vale_direction = None
|
||||||
|
vale_arb_size = 0 # 현재 차익거래 수량 추적
|
||||||
|
|
||||||
state = StateManager()
|
state = StateManager()
|
||||||
om = OrderManager(exchange)
|
om = OrderManager(exchange)
|
||||||
market_open = False
|
market_open = False
|
||||||
active_orders = {} # BOND 전용 {order_id: {"dir": ..., "price": ...}}
|
active_orders = {} # BOND 전용 {order_id: {"dir": ..., "price": ...}}
|
||||||
|
|
||||||
last_refresh = time.time()
|
last_refresh = time.time()
|
||||||
vale_last_print = time.time()
|
vale_last_print = time.time()
|
||||||
|
|
||||||
def next_id():
|
def next_id():
|
||||||
return om.next_order()
|
return om.next_order()
|
||||||
@@ -85,10 +92,11 @@ def main():
|
|||||||
|
|
||||||
cancel_all_bond_orders()
|
cancel_all_bond_orders()
|
||||||
|
|
||||||
buy_price = BOND_FAIR_VALUE - 1
|
buy_price = BOND_FAIR_VALUE - 1 # 999
|
||||||
sell_price = BOND_FAIR_VALUE + 1
|
sell_price = BOND_FAIR_VALUE + 1 # 1001
|
||||||
position = om.positions["BOND"]
|
position = om.positions["BOND"]
|
||||||
|
|
||||||
|
# 포지션에 따라 size 비대칭 조정
|
||||||
base_size = BOND_ORDER_SIZE
|
base_size = BOND_ORDER_SIZE
|
||||||
adjustment = abs(position) // 5
|
adjustment = abs(position) // 5
|
||||||
|
|
||||||
@@ -102,25 +110,31 @@ def main():
|
|||||||
buy_size = base_size
|
buy_size = base_size
|
||||||
sell_size = base_size
|
sell_size = base_size
|
||||||
|
|
||||||
bid = next_id()
|
# 포지션 한도 초과 방지
|
||||||
if exchange.send_add_message(
|
buy_size = min(buy_size, 100 - position)
|
||||||
order_id=bid, symbol="BOND",
|
sell_size = min(sell_size, 100 + position)
|
||||||
dir=Dir.BUY, price=buy_price, size=buy_size
|
|
||||||
) is not False:
|
if buy_size > 0:
|
||||||
|
bid = next_id()
|
||||||
|
exchange.send_add_message(
|
||||||
|
order_id=bid, symbol="BOND",
|
||||||
|
dir=Dir.BUY, price=buy_price, size=buy_size
|
||||||
|
)
|
||||||
active_orders[bid] = {"dir": Dir.BUY, "price": buy_price}
|
active_orders[bid] = {"dir": Dir.BUY, "price": buy_price}
|
||||||
|
|
||||||
ask = next_id()
|
if sell_size > 0:
|
||||||
if exchange.send_add_message(
|
ask = next_id()
|
||||||
order_id=ask, symbol="BOND",
|
exchange.send_add_message(
|
||||||
dir=Dir.SELL, price=sell_price, size=sell_size
|
order_id=ask, symbol="BOND",
|
||||||
) is not False:
|
dir=Dir.SELL, price=sell_price, size=sell_size
|
||||||
|
)
|
||||||
active_orders[ask] = {"dir": Dir.SELL, "price": sell_price}
|
active_orders[ask] = {"dir": Dir.SELL, "price": sell_price}
|
||||||
|
|
||||||
print(f" BOND 주문 → 매수:{buy_price} x{buy_size}, 매도:{sell_price} x{sell_size}, 포지션:{position}")
|
print(f" BOND 주문 → 매수:{buy_price} x{buy_size}, 매도:{sell_price} x{sell_size}, 포지션:{position}")
|
||||||
|
|
||||||
def try_xlf_arb():
|
def try_xlf_arb():
|
||||||
"""XLF 차익거래 시도 - IDLE 상태일 때만"""
|
"""XLF 차익거래 시도 - IDLE 상태일 때만"""
|
||||||
nonlocal xlf_state, xlf_direction, xlf_pending
|
nonlocal xlf_state, xlf_direction, xlf_pending, xlf_arb_size
|
||||||
|
|
||||||
if not market_open or xlf_state != "IDLE":
|
if not market_open or xlf_state != "IDLE":
|
||||||
return
|
return
|
||||||
@@ -149,6 +163,7 @@ def main():
|
|||||||
print(f" XLF 차익(바스켓→XLF) 시작, 예상수익:{profit1}")
|
print(f" XLF 차익(바스켓→XLF) 시작, 예상수익:{profit1}")
|
||||||
xlf_state = "BUYING_BASKET"
|
xlf_state = "BUYING_BASKET"
|
||||||
xlf_direction = "BASKET_TO_XLF"
|
xlf_direction = "BASKET_TO_XLF"
|
||||||
|
xlf_arb_size = 10
|
||||||
xlf_pending.clear()
|
xlf_pending.clear()
|
||||||
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()
|
||||||
@@ -162,13 +177,14 @@ def main():
|
|||||||
print(f" XLF 차익(XLF→바스켓) 시작, 예상수익:{profit2}")
|
print(f" XLF 차익(XLF→바스켓) 시작, 예상수익:{profit2}")
|
||||||
xlf_state = "BUYING_XLF"
|
xlf_state = "BUYING_XLF"
|
||||||
xlf_direction = "XLF_TO_BASKET"
|
xlf_direction = "XLF_TO_BASKET"
|
||||||
|
xlf_arb_size = 10
|
||||||
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, symbol, dir_, qty):
|
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:
|
||||||
@@ -181,16 +197,16 @@ def main():
|
|||||||
if xlf_state == "BUYING_BASKET" and not xlf_pending:
|
if xlf_state == "BUYING_BASKET" and not xlf_pending:
|
||||||
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, xlf_arb_size)
|
||||||
|
|
||||||
elif xlf_state == "BUYING_XLF" and not xlf_pending:
|
elif xlf_state == "BUYING_XLF" and not xlf_pending:
|
||||||
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, xlf_arb_size)
|
||||||
|
|
||||||
def try_vale_arb():
|
def try_vale_arb():
|
||||||
"""VALE/VALBZ 차익거래 시도 - IDLE 상태일 때만"""
|
"""VALE/VALBZ 차익거래 시도 - IDLE 상태일 때만"""
|
||||||
nonlocal vale_state, vale_direction, vale_pending
|
nonlocal vale_state, vale_direction, vale_pending, vale_arb_size
|
||||||
|
|
||||||
if not market_open or vale_state != "IDLE":
|
if not market_open or vale_state != "IDLE":
|
||||||
return
|
return
|
||||||
@@ -203,31 +219,38 @@ def main():
|
|||||||
if None in [vale_bid, vale_ask, valbz_bid, valbz_ask]:
|
if None in [vale_bid, vale_ask, valbz_bid, valbz_ask]:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
valbz_pos = om.positions["VALBZ"]
|
||||||
|
vale_pos = om.positions["VALE"]
|
||||||
|
|
||||||
# 케이스 1: VALE가 비쌀 때 → VALBZ 매수 → VALE 변환 → VALE 매도
|
# 케이스 1: VALE가 비쌀 때 → VALBZ 매수 → VALE 변환 → VALE 매도
|
||||||
profit1 = vale_bid - valbz_ask - VALE_CONVERSION_FEE
|
profit1 = vale_bid - valbz_ask - VALE_CONVERSION_FEE
|
||||||
if profit1 > 0 and om.check_pos_limit("VALBZ"):
|
arb_size1 = min(VALE_ARB_SIZE, 10 - valbz_pos)
|
||||||
print(f" VALE 차익(VALBZ→VALE) 시작, 예상수익:{profit1}")
|
if profit1 > 0 and arb_size1 > 0:
|
||||||
|
print(f" VALE 차익(VALBZ→VALE) 시작, 예상수익:{profit1 * arb_size1}, size:{arb_size1}")
|
||||||
vale_state = "BUYING_VALBZ"
|
vale_state = "BUYING_VALBZ"
|
||||||
vale_direction = "VALBZ_TO_VALE"
|
vale_direction = "VALBZ_TO_VALE"
|
||||||
|
vale_arb_size = arb_size1
|
||||||
vale_pending.clear()
|
vale_pending.clear()
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(oid, "VALBZ", Dir.BUY, valbz_ask, 1)
|
exchange.send_add_message(oid, "VALBZ", Dir.BUY, valbz_ask, arb_size1)
|
||||||
vale_pending[oid] = 1
|
vale_pending[oid] = arb_size1
|
||||||
return
|
return
|
||||||
|
|
||||||
# 케이스 2: VALBZ가 비쌀 때 → VALE 매수 → VALBZ 변환 → VALBZ 매도
|
# 케이스 2: VALBZ가 비쌀 때 → VALE 매수 → VALBZ 변환 → VALBZ 매도
|
||||||
profit2 = valbz_bid - vale_ask - VALE_CONVERSION_FEE
|
profit2 = valbz_bid - vale_ask - VALE_CONVERSION_FEE
|
||||||
if profit2 > 0 and om.check_pos_limit("VALE"):
|
arb_size2 = min(VALE_ARB_SIZE, 10 - vale_pos)
|
||||||
print(f" VALE 차익(VALE→VALBZ) 시작, 예상수익:{profit2}")
|
if profit2 > 0 and arb_size2 > 0:
|
||||||
|
print(f" VALE 차익(VALE→VALBZ) 시작, 예상수익:{profit2 * arb_size2}, size:{arb_size2}")
|
||||||
vale_state = "BUYING_VALE"
|
vale_state = "BUYING_VALE"
|
||||||
vale_direction = "VALE_TO_VALBZ"
|
vale_direction = "VALE_TO_VALBZ"
|
||||||
|
vale_arb_size = arb_size2
|
||||||
vale_pending.clear()
|
vale_pending.clear()
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(oid, "VALE", Dir.BUY, vale_ask, 1)
|
exchange.send_add_message(oid, "VALE", Dir.BUY, vale_ask, arb_size2)
|
||||||
vale_pending[oid] = 1
|
vale_pending[oid] = arb_size2
|
||||||
|
|
||||||
def handle_vale_fill(order_id, symbol, dir_, qty):
|
def handle_vale_fill(order_id, symbol, dir_, qty):
|
||||||
"""VALE state machine 체결 처리"""
|
"""VALE state machine 체결 처리 (부분 체결 추적)"""
|
||||||
nonlocal vale_state, vale_pending
|
nonlocal vale_state, vale_pending
|
||||||
|
|
||||||
if order_id not in vale_pending:
|
if order_id not in vale_pending:
|
||||||
@@ -240,12 +263,12 @@ def main():
|
|||||||
if vale_state == "BUYING_VALBZ" and not vale_pending:
|
if vale_state == "BUYING_VALBZ" and not vale_pending:
|
||||||
print(" VALBZ 매수 완료 → VALE 변환 시작")
|
print(" VALBZ 매수 완료 → VALE 변환 시작")
|
||||||
vale_state = "CONVERTING"
|
vale_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "VALE", Dir.BUY, 1)
|
exchange.send_convert_message(next_id(), "VALE", Dir.BUY, vale_arb_size)
|
||||||
|
|
||||||
elif vale_state == "BUYING_VALE" and not vale_pending:
|
elif vale_state == "BUYING_VALE" and not vale_pending:
|
||||||
print(" VALE 매수 완료 → VALBZ 변환 시작")
|
print(" VALE 매수 완료 → VALBZ 변환 시작")
|
||||||
vale_state = "CONVERTING"
|
vale_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "VALE", Dir.SELL, 1)
|
exchange.send_convert_message(next_id(), "VALE", Dir.SELL, vale_arb_size)
|
||||||
|
|
||||||
# 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
|
||||||
@@ -306,13 +329,25 @@ def main():
|
|||||||
if xlf_state == "CONVERTING":
|
if xlf_state == "CONVERTING":
|
||||||
print(" XLF 변환 완료 → 매도 시작")
|
print(" XLF 변환 완료 → 매도 시작")
|
||||||
if xlf_direction == "BASKET_TO_XLF":
|
if xlf_direction == "BASKET_TO_XLF":
|
||||||
|
# 변환: BOND -3, GS -2, MS -3, WFC -2, XLF +10
|
||||||
|
om.update_position("BOND", -1, -3)
|
||||||
|
om.update_position("GS", -1, -2)
|
||||||
|
om.update_position("MS", -1, -3)
|
||||||
|
om.update_position("WFC", -1, -2)
|
||||||
|
om.update_position("XLF", -1, xlf_arb_size)
|
||||||
xlf_state = "SELLING_XLF"
|
xlf_state = "SELLING_XLF"
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
oid, "XLF", Dir.SELL, state.bid_prices["XLF"], 10
|
oid, "XLF", Dir.SELL, state.bid_prices["XLF"], xlf_arb_size
|
||||||
)
|
)
|
||||||
xlf_pending[oid] = 10
|
xlf_pending[oid] = xlf_arb_size
|
||||||
elif xlf_direction == "XLF_TO_BASKET":
|
elif xlf_direction == "XLF_TO_BASKET":
|
||||||
|
# 변환: XLF -10, BOND +3, GS +2, MS +3, WFC +2
|
||||||
|
om.update_position("XLF", -1, -xlf_arb_size)
|
||||||
|
om.update_position("BOND", -1, 3)
|
||||||
|
om.update_position("GS", -1, 2)
|
||||||
|
om.update_position("MS", -1, 3)
|
||||||
|
om.update_position("WFC", -1, 2)
|
||||||
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()
|
||||||
@@ -325,19 +360,25 @@ def main():
|
|||||||
elif vale_state == "CONVERTING":
|
elif vale_state == "CONVERTING":
|
||||||
print(" VALE 변환 완료 → 매도 시작")
|
print(" VALE 변환 완료 → 매도 시작")
|
||||||
if vale_direction == "VALBZ_TO_VALE":
|
if vale_direction == "VALBZ_TO_VALE":
|
||||||
|
# 변환: VALBZ -size, VALE +size
|
||||||
|
om.update_position("VALBZ", -1, -vale_arb_size)
|
||||||
|
om.update_position("VALE", -1, vale_arb_size)
|
||||||
vale_state = "SELLING_VALE"
|
vale_state = "SELLING_VALE"
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], 1
|
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], vale_arb_size
|
||||||
)
|
)
|
||||||
vale_pending[oid] = 1
|
vale_pending[oid] = vale_arb_size
|
||||||
elif vale_direction == "VALE_TO_VALBZ":
|
elif vale_direction == "VALE_TO_VALBZ":
|
||||||
|
# 변환: VALE -size, VALBZ +size
|
||||||
|
om.update_position("VALE", -1, -vale_arb_size)
|
||||||
|
om.update_position("VALBZ", -1, vale_arb_size)
|
||||||
vale_state = "SELLING_VALBZ"
|
vale_state = "SELLING_VALBZ"
|
||||||
oid = next_id()
|
oid = next_id()
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], 1
|
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], vale_arb_size
|
||||||
)
|
)
|
||||||
vale_pending[oid] = 1
|
vale_pending[oid] = vale_arb_size
|
||||||
|
|
||||||
elif message["type"] == "fill":
|
elif message["type"] == "fill":
|
||||||
print(message)
|
print(message)
|
||||||
|
|||||||
Reference in New Issue
Block a user