commit
This commit is contained in:
@@ -49,15 +49,16 @@ def main():
|
|||||||
BOND_ORDER_SIZE = 80 # 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 차익거래 단위 (포지션 한도 10)
|
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 (바스켓→XLF)
|
# IDLE → BUYING_BASKET → CONVERTING → SELLING_XLF → IDLE
|
||||||
# IDLE → BUYING_XLF → CONVERTING → SELLING_BASKET → IDLE (XLF→바스켓)
|
# 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_VALBZ → CONVERTING → SELLING_VALE → IDLE
|
||||||
@@ -65,6 +66,7 @@ def main():
|
|||||||
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)
|
||||||
@@ -99,11 +101,9 @@ def main():
|
|||||||
adjustment = abs(position) // 5
|
adjustment = abs(position) // 5
|
||||||
|
|
||||||
if position < 0:
|
if position < 0:
|
||||||
# 숏 포지션 → 매수를 더 많이
|
|
||||||
buy_size = min(base_size + adjustment, 100 - position)
|
buy_size = min(base_size + adjustment, 100 - 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, 100 + position)
|
sell_size = min(base_size + adjustment, 100 + position)
|
||||||
else:
|
else:
|
||||||
@@ -134,7 +134,7 @@ def main():
|
|||||||
|
|
||||||
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
|
||||||
@@ -163,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()
|
||||||
@@ -176,6 +177,7 @@ 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)
|
||||||
@@ -195,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
|
||||||
@@ -217,7 +219,6 @@ 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
|
||||||
|
|
||||||
# 포지션 한도 내에서 최대 size 계산
|
|
||||||
valbz_pos = om.positions["VALBZ"]
|
valbz_pos = om.positions["VALBZ"]
|
||||||
vale_pos = om.positions["VALE"]
|
vale_pos = om.positions["VALE"]
|
||||||
|
|
||||||
@@ -228,6 +229,7 @@ def main():
|
|||||||
print(f" VALE 차익(VALBZ→VALE) 시작, 예상수익:{profit1 * arb_size1}, size:{arb_size1}")
|
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, arb_size1)
|
exchange.send_add_message(oid, "VALBZ", Dir.BUY, valbz_ask, arb_size1)
|
||||||
@@ -241,6 +243,7 @@ def main():
|
|||||||
print(f" VALE 차익(VALE→VALBZ) 시작, 예상수익:{profit2 * arb_size2}, size:{arb_size2}")
|
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, arb_size2)
|
exchange.send_add_message(oid, "VALE", Dir.BUY, vale_ask, arb_size2)
|
||||||
@@ -258,22 +261,19 @@ def main():
|
|||||||
del vale_pending[order_id]
|
del vale_pending[order_id]
|
||||||
|
|
||||||
if vale_state == "BUYING_VALBZ" and not vale_pending:
|
if vale_state == "BUYING_VALBZ" and not vale_pending:
|
||||||
# 체결된 수량만큼 변환
|
print(" VALBZ 매수 완료 → VALE 변환 시작")
|
||||||
size = qty # 마지막 체결 수량 대신 전체 수량 필요 → arb_size 저장 필요
|
|
||||||
print(f" VALBZ 매수 완료 → VALE 변환 시작")
|
|
||||||
vale_state = "CONVERTING"
|
vale_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "VALE", Dir.BUY, qty)
|
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(f" VALE 매수 완료 → VALBZ 변환 시작")
|
print(" VALE 매수 완료 → VALBZ 변환 시작")
|
||||||
vale_state = "CONVERTING"
|
vale_state = "CONVERTING"
|
||||||
exchange.send_convert_message(next_id(), "VALE", Dir.SELL, qty)
|
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
|
||||||
# of the VALE market.
|
# of the VALE market.
|
||||||
vale_last_print_time = time.time()
|
vale_last_print_time = time.time()
|
||||||
vale_arb_total_size = 0 # 변환할 총 수량 추적
|
|
||||||
|
|
||||||
# Here is the main loop of the program. It will continue to read and
|
# Here is the main loop of the program. It will continue to read and
|
||||||
# process messages in a loop until a "close" message is received. You
|
# process messages in a loop until a "close" message is received. You
|
||||||
@@ -329,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()
|
||||||
@@ -348,27 +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()
|
||||||
# 변환된 수량 = VALBZ 매수 수량
|
|
||||||
converted = sum(
|
|
||||||
v for v in vale_pending.values()
|
|
||||||
) if vale_pending else VALE_ARB_SIZE
|
|
||||||
sell_size = abs(om.positions["VALE"])
|
|
||||||
sell_size = min(sell_size, 10)
|
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], sell_size
|
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], vale_arb_size
|
||||||
)
|
)
|
||||||
vale_pending[oid] = sell_size
|
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()
|
||||||
sell_size = abs(om.positions["VALBZ"])
|
|
||||||
sell_size = min(sell_size, 10)
|
|
||||||
exchange.send_add_message(
|
exchange.send_add_message(
|
||||||
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], sell_size
|
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], vale_arb_size
|
||||||
)
|
)
|
||||||
vale_pending[oid] = sell_size
|
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