commit
This commit is contained in:
338
prac.py
338
prac.py
@@ -14,70 +14,44 @@ from state import StateManager
|
||||
from order import OrderManager
|
||||
|
||||
# ~~~~~============== CONFIGURATION ==============~~~~~
|
||||
# Replace "REPLACEME" with your team name!
|
||||
team_name = "HanyangFloorFunction"
|
||||
|
||||
# ~~~~~============== MAIN LOOP ==============~~~~~
|
||||
|
||||
# You should put your code here! We provide some starter code as an example,
|
||||
# but feel free to change/remove/edit/update any of it as you'd like. If you
|
||||
# have any questions about the starter code, or what to do next, please ask us!
|
||||
#
|
||||
# To help you get started, the sample code below tries to buy BOND for a low
|
||||
# price, and it prints the current prices for VALE every second. The sample
|
||||
# code is intended to be a working example, but it needs some improvement
|
||||
# before it will start making good trades!
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_arguments()
|
||||
|
||||
exchange = ExchangeConnection(args=args)
|
||||
|
||||
# Store and print the "hello" message received from the exchange. This
|
||||
# contains useful information about your positions. Normally you start with
|
||||
# all positions at zero, but if you reconnect during a round, you might
|
||||
# have already bought/sold symbols and have non-zero positions.
|
||||
hello_message = exchange.read_message()
|
||||
print("First message from exchange:", hello_message)
|
||||
|
||||
# Send an order for BOND at a good price, but it is low enough that it is
|
||||
# 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 = 50 # BOND 주문당 수량
|
||||
XLF_CONVERSION_FEE = 100 # XLF 변환 비용
|
||||
XLF_MIN_PROFIT = 10 # XLF 차익거래 최소 수익 (불확실한 거래 방지)
|
||||
VALE_CONVERSION_FEE = 10 # VALE 변환 비용
|
||||
VALE_MIN_PROFIT = 3 # VALE 차익거래 최소 수익
|
||||
VALE_ARB_SIZE = 10 # VALE 차익거래 단위
|
||||
REFRESH_INTERVAL = 5.0 # 주문 갱신 주기 (초)
|
||||
BOND_FAIR_VALUE = 1000
|
||||
BOND_ORDER_SIZE = 60
|
||||
XLF_CONVERSION_FEE = 100
|
||||
XLF_MIN_PROFIT = 5
|
||||
VALE_CONVERSION_FEE = 10
|
||||
VALE_MIN_PROFIT = 1
|
||||
VALE_ARB_SIZE = 10
|
||||
REFRESH_INTERVAL = 5.0
|
||||
|
||||
# XLF state machine
|
||||
# IDLE → BUYING_BASKET → CONVERTING → SELLING_XLF → IDLE
|
||||
# IDLE → BUYING_XLF → CONVERTING → SELLING_BASKET → IDLE
|
||||
xlf_state = "IDLE"
|
||||
xlf_pending = {}
|
||||
xlf_direction = None
|
||||
xlf_arb_size = 0
|
||||
xlf_convert_oid = None
|
||||
|
||||
# VALE state machine
|
||||
# IDLE → BUYING_VALBZ → CONVERTING → SELLING_VALE → IDLE
|
||||
# IDLE → BUYING_VALE → CONVERTING → SELLING_VALBZ → IDLE
|
||||
vale_state = "IDLE"
|
||||
vale_pending = {}
|
||||
vale_direction = None
|
||||
vale_arb_size = 0
|
||||
vale_convert_oid = None
|
||||
|
||||
state = StateManager()
|
||||
om = OrderManager(exchange)
|
||||
market_open = False
|
||||
active_orders = {} # BOND 전용 {order_id: {"dir": ..., "price": ..., "size": ...}}
|
||||
active_orders = {}
|
||||
|
||||
last_refresh = time.time()
|
||||
|
||||
# hello 메시지에서 기존 포지션 로드 (재접속 시 필수)
|
||||
for sym_info in hello_message["symbols"]:
|
||||
sym = sym_info["symbol"]
|
||||
pos = sym_info["position"]
|
||||
@@ -89,8 +63,16 @@ def main():
|
||||
def next_id():
|
||||
return om.next_order()
|
||||
|
||||
def cancel_bond_sell_orders():
|
||||
for oid in list(active_orders.keys()):
|
||||
order_info = active_orders[oid]
|
||||
if order_info["dir"] == Dir.SELL:
|
||||
size = order_info.get("size", 0)
|
||||
om.future_positions["BOND"] += size
|
||||
om.cancel(oid)
|
||||
active_orders.pop(oid, None)
|
||||
|
||||
def cancel_all_bond_orders():
|
||||
"""활성 BOND 주문 전부 취소 + future_positions 롤백"""
|
||||
for oid in list(active_orders.keys()):
|
||||
order_info = active_orders[oid]
|
||||
size = order_info.get("size", 0)
|
||||
@@ -102,17 +84,13 @@ def main():
|
||||
active_orders.pop(oid, None)
|
||||
|
||||
def place_bond_orders():
|
||||
"""포지션 한도 안에서 bid/ask 양방향 주문"""
|
||||
if not market_open:
|
||||
return
|
||||
|
||||
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 = om.positions["BOND"]
|
||||
|
||||
# 포지션에 따라 size 비대칭 조정
|
||||
base_size = BOND_ORDER_SIZE
|
||||
adjustment = abs(position) // 5
|
||||
|
||||
@@ -126,34 +104,25 @@ def main():
|
||||
buy_size = base_size
|
||||
sell_size = base_size
|
||||
|
||||
# 포지션 한도 초과 방지
|
||||
buy_size = max(0, min(buy_size, 100 - position))
|
||||
sell_size = max(0, min(sell_size, 100 + position))
|
||||
|
||||
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
|
||||
)
|
||||
exchange.send_add_message(order_id=bid, symbol="BOND", dir=Dir.BUY, price=buy_price, size=buy_size)
|
||||
om.future_positions["BOND"] += buy_size
|
||||
active_orders[bid] = {"dir": Dir.BUY, "price": buy_price, "size": buy_size}
|
||||
|
||||
if sell_size > 0:
|
||||
ask = next_id()
|
||||
exchange.send_add_message(
|
||||
order_id=ask, symbol="BOND",
|
||||
dir=Dir.SELL, price=sell_price, size=sell_size
|
||||
)
|
||||
exchange.send_add_message(order_id=ask, symbol="BOND", dir=Dir.SELL, price=sell_price, size=sell_size)
|
||||
om.future_positions["BOND"] -= sell_size
|
||||
active_orders[ask] = {"dir": Dir.SELL, "price": sell_price, "size": sell_size}
|
||||
|
||||
print(f" BOND 주문 → 매수:{buy_price} x{buy_size}, 매도:{sell_price} x{sell_size}, 포지션:{position}")
|
||||
|
||||
def try_xlf_arb():
|
||||
"""XLF 차익거래 시도 - IDLE 상태일 때만"""
|
||||
nonlocal xlf_state, xlf_direction, xlf_pending, xlf_arb_size
|
||||
|
||||
nonlocal xlf_state, xlf_direction, xlf_pending, xlf_arb_size, xlf_convert_oid
|
||||
if not market_open or xlf_state != "IDLE":
|
||||
return
|
||||
|
||||
@@ -168,22 +137,17 @@ def main():
|
||||
wfc_bid = state.bid_prices["WFC"]
|
||||
xlf_ask = state.ask_prices["XLF"]
|
||||
|
||||
if None in [bond_ask, gs_ask, ms_ask, wfc_ask, xlf_bid,
|
||||
bond_bid, gs_bid, ms_bid, wfc_bid, xlf_ask]:
|
||||
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
|
||||
|
||||
# 케이스 1: 바스켓 매수 → XLF 변환 → XLF 매도
|
||||
profit1 = xlf_bid * 10 - basket_ask - XLF_CONVERSION_FEE
|
||||
if profit1 > XLF_MIN_PROFIT and om.check_pos_limit("XLF"):
|
||||
print(f" XLF 차익(바스켓→XLF) 시작, 예상수익:{profit1}")
|
||||
# BOND 주문 먼저 취소 (XLF용 BOND 매수가 기존 매도에 상쇄되지 않도록)
|
||||
cancel_all_bond_orders()
|
||||
xlf_state = "BUYING_BASKET"
|
||||
xlf_direction = "BASKET_TO_XLF"
|
||||
xlf_arb_size = 10
|
||||
cancel_bond_sell_orders()
|
||||
xlf_state = "BUYING_BASKET"; xlf_direction = "BASKET_TO_XLF"; xlf_arb_size = 10
|
||||
xlf_pending.clear()
|
||||
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
||||
oid = next_id()
|
||||
@@ -191,44 +155,36 @@ def main():
|
||||
xlf_pending[oid] = qty
|
||||
return
|
||||
|
||||
# 케이스 2: XLF 매수 → 바스켓 변환 → 각 종목 매도
|
||||
profit2 = basket_bid - xlf_ask * 10 - XLF_CONVERSION_FEE
|
||||
if profit2 > XLF_MIN_PROFIT and om.check_pos_limit("XLF"):
|
||||
print(f" XLF 차익(XLF→바스켓) 시작, 예상수익:{profit2}")
|
||||
cancel_all_bond_orders()
|
||||
xlf_state = "BUYING_XLF"
|
||||
xlf_direction = "XLF_TO_BASKET"
|
||||
xlf_arb_size = 10
|
||||
xlf_state = "BUYING_XLF"; xlf_direction = "XLF_TO_BASKET"; xlf_arb_size = 10
|
||||
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, symbol, dir_, qty):
|
||||
"""XLF state machine 체결 처리 (부분 체결 추적)"""
|
||||
nonlocal xlf_state, xlf_pending
|
||||
|
||||
nonlocal xlf_state, xlf_pending, xlf_convert_oid
|
||||
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:
|
||||
print(" 바스켓 매수 완료 → XLF 변환 시작")
|
||||
xlf_state = "CONVERTING"
|
||||
exchange.send_convert_message(next_id(), "XLF", Dir.BUY, xlf_arb_size)
|
||||
|
||||
xlf_convert_oid = next_id()
|
||||
exchange.send_convert_message(xlf_convert_oid, "XLF", Dir.BUY, xlf_arb_size)
|
||||
elif xlf_state == "BUYING_XLF" and not xlf_pending:
|
||||
print(" XLF 매수 완료 → 바스켓 변환 시작")
|
||||
xlf_state = "CONVERTING"
|
||||
exchange.send_convert_message(next_id(), "XLF", Dir.SELL, xlf_arb_size)
|
||||
xlf_convert_oid = next_id()
|
||||
exchange.send_convert_message(xlf_convert_oid, "XLF", Dir.SELL, xlf_arb_size)
|
||||
|
||||
def try_vale_arb():
|
||||
"""VALE/VALBZ 차익거래 시도 - IDLE 상태일 때만"""
|
||||
nonlocal vale_state, vale_direction, vale_pending, vale_arb_size
|
||||
|
||||
nonlocal vale_state, vale_direction, vale_pending, vale_arb_size, vale_convert_oid
|
||||
if not market_open or vale_state != "IDLE":
|
||||
return
|
||||
|
||||
@@ -243,86 +199,55 @@ def main():
|
||||
valbz_pos = om.positions["VALBZ"]
|
||||
vale_pos = om.positions["VALE"]
|
||||
|
||||
# 케이스 1: VALE가 비쌀 때 → VALBZ 매수 → VALE 변환 → VALE 매도
|
||||
profit1 = vale_bid - valbz_ask - VALE_CONVERSION_FEE
|
||||
arb_size1 = min(VALE_ARB_SIZE, 10 - valbz_pos)
|
||||
if profit1 > VALE_MIN_PROFIT and arb_size1 > 0:
|
||||
print(f" VALE 차익(VALBZ→VALE) 시작, 예상수익:{profit1 * arb_size1}, size:{arb_size1}")
|
||||
vale_state = "BUYING_VALBZ"
|
||||
vale_direction = "VALBZ_TO_VALE"
|
||||
vale_arb_size = arb_size1
|
||||
vale_state = "BUYING_VALBZ"; vale_direction = "VALBZ_TO_VALE"; vale_arb_size = arb_size1
|
||||
vale_pending.clear()
|
||||
oid = next_id()
|
||||
exchange.send_add_message(oid, "VALBZ", Dir.BUY, valbz_ask, arb_size1)
|
||||
vale_pending[oid] = arb_size1
|
||||
return
|
||||
|
||||
# 케이스 2: VALBZ가 비쌀 때 → VALE 매수 → VALBZ 변환 → VALBZ 매도
|
||||
profit2 = valbz_bid - vale_ask - VALE_CONVERSION_FEE
|
||||
arb_size2 = min(VALE_ARB_SIZE, 10 - vale_pos)
|
||||
if profit2 > VALE_MIN_PROFIT and arb_size2 > 0:
|
||||
print(f" VALE 차익(VALE→VALBZ) 시작, 예상수익:{profit2 * arb_size2}, size:{arb_size2}")
|
||||
vale_state = "BUYING_VALE"
|
||||
vale_direction = "VALE_TO_VALBZ"
|
||||
vale_arb_size = arb_size2
|
||||
vale_state = "BUYING_VALE"; vale_direction = "VALE_TO_VALBZ"; vale_arb_size = arb_size2
|
||||
vale_pending.clear()
|
||||
oid = next_id()
|
||||
exchange.send_add_message(oid, "VALE", Dir.BUY, vale_ask, arb_size2)
|
||||
vale_pending[oid] = arb_size2
|
||||
|
||||
def handle_vale_fill(order_id, symbol, dir_, qty):
|
||||
"""VALE state machine 체결 처리 (부분 체결 추적)"""
|
||||
nonlocal vale_state, vale_pending
|
||||
|
||||
nonlocal vale_state, vale_pending, vale_convert_oid
|
||||
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, vale_arb_size)
|
||||
|
||||
vale_convert_oid = next_id()
|
||||
exchange.send_convert_message(vale_convert_oid, "VALE", Dir.BUY, vale_arb_size)
|
||||
elif vale_state == "BUYING_VALE" and not vale_pending:
|
||||
print(" VALE 매수 완료 → VALBZ 변환 시작")
|
||||
vale_state = "CONVERTING"
|
||||
exchange.send_convert_message(next_id(), "VALE", Dir.SELL, vale_arb_size)
|
||||
vale_convert_oid = next_id()
|
||||
exchange.send_convert_message(vale_convert_oid, "VALE", Dir.SELL, vale_arb_size)
|
||||
|
||||
# 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.
|
||||
vale_last_print_time = time.time()
|
||||
|
||||
# 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
|
||||
# should write to code handle more types of messages (and not just print
|
||||
# the message). Feel free to modify any of the starter code below.
|
||||
#
|
||||
# Note: a common mistake people make is to call write_message() at least
|
||||
# once for every read_message() response.
|
||||
#
|
||||
# Every message sent to the exchange generates at least one response
|
||||
# message. Sending a message in response to every exchange message will
|
||||
# cause a feedback loop where your bot's messages will quickly be
|
||||
# rate-limited and ignored. Please, don't do that!
|
||||
while True:
|
||||
message = exchange.read_message()
|
||||
|
||||
# Some of the message types below happen infrequently and contain
|
||||
# important information to help you understand what your bot is doing,
|
||||
# so they are printed in full. We recommend not always printing every
|
||||
# message because it can be a lot of information to read. Instead, let
|
||||
# your code handle the messages and just print the information
|
||||
# important for you!
|
||||
if message["type"] == "close":
|
||||
print("The round has ended")
|
||||
break
|
||||
|
||||
elif message["type"] == "open":
|
||||
# 시장이 열렸을 때 주문 시작 (open 전에 주문하면 reject됨)
|
||||
print("Market opened:", message)
|
||||
market_open = True
|
||||
place_bond_orders()
|
||||
@@ -336,84 +261,56 @@ def main():
|
||||
active_orders.pop(oid, None)
|
||||
if oid in xlf_pending:
|
||||
print(" XLF 주문 reject → IDLE 복귀")
|
||||
xlf_state = "IDLE"
|
||||
xlf_pending.clear()
|
||||
xlf_direction = None
|
||||
xlf_state = "IDLE"; xlf_pending.clear(); xlf_direction = None
|
||||
place_bond_orders()
|
||||
if oid in vale_pending:
|
||||
print(" VALE 주문 reject → IDLE 복귀")
|
||||
vale_state = "IDLE"
|
||||
vale_pending.clear()
|
||||
vale_direction = None
|
||||
vale_state = "IDLE"; vale_pending.clear(); vale_direction = None
|
||||
|
||||
elif message["type"] == "ack":
|
||||
# XLF 변환 ack 처리
|
||||
if xlf_state == "CONVERTING":
|
||||
ack_oid = message.get("order_id")
|
||||
|
||||
if xlf_state == "CONVERTING" and ack_oid == xlf_convert_oid:
|
||||
print(" XLF 변환 완료 → 매도 시작")
|
||||
if xlf_direction == "BASKET_TO_XLF":
|
||||
# 변환: BOND -3, GS -2, MS -3, WFC -2, XLF +10
|
||||
om.positions["BOND"] -= 3
|
||||
om.positions["GS"] -= 2
|
||||
om.positions["MS"] -= 3
|
||||
om.positions["WFC"] -= 2
|
||||
om.positions["BOND"] -= 3; om.positions["GS"] -= 2
|
||||
om.positions["MS"] -= 3; om.positions["WFC"] -= 2
|
||||
om.positions["XLF"] += xlf_arb_size
|
||||
om.future_positions["BOND"] -= 3
|
||||
om.future_positions["GS"] -= 2
|
||||
om.future_positions["MS"] -= 3
|
||||
om.future_positions["WFC"] -= 2
|
||||
om.future_positions["BOND"] -= 3; om.future_positions["GS"] -= 2
|
||||
om.future_positions["MS"] -= 3; om.future_positions["WFC"] -= 2
|
||||
om.future_positions["XLF"] += xlf_arb_size
|
||||
xlf_state = "SELLING_XLF"
|
||||
oid = next_id()
|
||||
exchange.send_add_message(
|
||||
oid, "XLF", Dir.SELL, state.bid_prices["XLF"], xlf_arb_size
|
||||
)
|
||||
exchange.send_add_message(oid, "XLF", Dir.SELL, state.bid_prices["XLF"], xlf_arb_size)
|
||||
xlf_pending[oid] = xlf_arb_size
|
||||
elif xlf_direction == "XLF_TO_BASKET":
|
||||
# 변환: XLF -10, BOND +3, GS +2, MS +3, WFC +2
|
||||
om.positions["XLF"] -= xlf_arb_size
|
||||
om.positions["BOND"] += 3
|
||||
om.positions["GS"] += 2
|
||||
om.positions["MS"] += 3
|
||||
om.positions["WFC"] += 2
|
||||
om.positions["BOND"] += 3; om.positions["GS"] += 2
|
||||
om.positions["MS"] += 3; om.positions["WFC"] += 2
|
||||
om.future_positions["XLF"] -= xlf_arb_size
|
||||
om.future_positions["BOND"] += 3
|
||||
om.future_positions["GS"] += 2
|
||||
om.future_positions["MS"] += 3
|
||||
om.future_positions["WFC"] += 2
|
||||
om.future_positions["BOND"] += 3; om.future_positions["GS"] += 2
|
||||
om.future_positions["MS"] += 3; om.future_positions["WFC"] += 2
|
||||
xlf_state = "SELLING_BASKET"
|
||||
for sym, qty in [("BOND", 3), ("GS", 2), ("MS", 3), ("WFC", 2)]:
|
||||
oid = next_id()
|
||||
exchange.send_add_message(
|
||||
oid, sym, Dir.SELL, state.bid_prices[sym], qty
|
||||
)
|
||||
exchange.send_add_message(oid, sym, Dir.SELL, state.bid_prices[sym], qty)
|
||||
xlf_pending[oid] = qty
|
||||
|
||||
# VALE 변환 ack 처리
|
||||
elif vale_state == "CONVERTING":
|
||||
elif vale_state == "CONVERTING" and ack_oid == vale_convert_oid:
|
||||
print(" VALE 변환 완료 → 매도 시작")
|
||||
if vale_direction == "VALBZ_TO_VALE":
|
||||
# 변환: VALBZ -size, VALE +size
|
||||
om.positions["VALBZ"] -= vale_arb_size
|
||||
om.positions["VALE"] += vale_arb_size
|
||||
om.future_positions["VALBZ"] -= vale_arb_size
|
||||
om.future_positions["VALE"] += vale_arb_size
|
||||
om.positions["VALBZ"] -= vale_arb_size; om.positions["VALE"] += vale_arb_size
|
||||
om.future_positions["VALBZ"] -= vale_arb_size; om.future_positions["VALE"] += vale_arb_size
|
||||
vale_state = "SELLING_VALE"
|
||||
oid = next_id()
|
||||
exchange.send_add_message(
|
||||
oid, "VALE", Dir.SELL, state.bid_prices["VALE"], vale_arb_size
|
||||
)
|
||||
exchange.send_add_message(oid, "VALE", Dir.SELL, state.bid_prices["VALE"], vale_arb_size)
|
||||
vale_pending[oid] = vale_arb_size
|
||||
elif vale_direction == "VALE_TO_VALBZ":
|
||||
# 변환: VALE -size, VALBZ +size
|
||||
om.positions["VALE"] -= vale_arb_size
|
||||
om.positions["VALBZ"] += vale_arb_size
|
||||
om.future_positions["VALE"] -= vale_arb_size
|
||||
om.future_positions["VALBZ"] += vale_arb_size
|
||||
om.positions["VALE"] -= vale_arb_size; om.positions["VALBZ"] += vale_arb_size
|
||||
om.future_positions["VALE"] -= vale_arb_size; om.future_positions["VALBZ"] += vale_arb_size
|
||||
vale_state = "SELLING_VALBZ"
|
||||
oid = next_id()
|
||||
exchange.send_add_message(
|
||||
oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], vale_arb_size
|
||||
)
|
||||
exchange.send_add_message(oid, "VALBZ", Dir.SELL, state.bid_prices["VALBZ"], vale_arb_size)
|
||||
vale_pending[oid] = vale_arb_size
|
||||
|
||||
elif message["type"] == "fill":
|
||||
@@ -423,7 +320,6 @@ def main():
|
||||
dir_ = message["dir"]
|
||||
oid = message["order_id"]
|
||||
|
||||
# 포지션 업데이트
|
||||
if dir_ == Dir.BUY:
|
||||
om.update_position(sym, oid, qty)
|
||||
else:
|
||||
@@ -431,26 +327,22 @@ def main():
|
||||
|
||||
print(f" 포지션 → {om.positions}")
|
||||
|
||||
# BOND 체결 시 무조건 재주문
|
||||
if sym == "BOND" and oid in active_orders:
|
||||
active_orders.pop(oid, None)
|
||||
place_bond_orders()
|
||||
|
||||
# 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
|
||||
# XLF 완료 후 BOND 주문 재배치
|
||||
xlf_state = "IDLE"; xlf_direction = None
|
||||
place_bond_orders()
|
||||
try_xlf_arb()
|
||||
|
||||
# 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
|
||||
vale_state = "IDLE"; vale_direction = None
|
||||
try_vale_arb()
|
||||
|
||||
elif message["type"] == "book":
|
||||
sym = message["symbol"]
|
||||
@@ -464,20 +356,13 @@ def main():
|
||||
now = time.time()
|
||||
if now > vale_last_print_time + 1:
|
||||
vale_last_print_time = now
|
||||
print({
|
||||
"vale_bid_price": state.bid_prices["VALE"],
|
||||
"vale_ask_price": state.ask_prices["VALE"],
|
||||
})
|
||||
print({"vale_bid_price": state.bid_prices["VALE"], "vale_ask_price": state.ask_prices["VALE"]})
|
||||
|
||||
# XLF 관련 심볼 호가 업데이트마다 차익거래 시도
|
||||
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:
|
||||
last_refresh = now
|
||||
@@ -486,11 +371,6 @@ def main():
|
||||
|
||||
# ~~~~~============== PROVIDED CODE ==============~~~~~
|
||||
|
||||
# You probably don't need to edit anything below this line, but feel free to
|
||||
# ask if you have any questions about what it is doing or how it works. If you
|
||||
# do need to change anything below this line, please feel free to
|
||||
|
||||
|
||||
class Dir(str, Enum):
|
||||
BUY = "BUY"
|
||||
SELL = "SELL"
|
||||
@@ -504,55 +384,27 @@ class ExchangeConnection:
|
||||
exchange_socket = self._connect(add_socket_timeout=args.add_socket_timeout)
|
||||
self.reader = exchange_socket.makefile("r", 1)
|
||||
self.writer = exchange_socket
|
||||
|
||||
self._write_message({"type": "hello", "team": team_name.upper()})
|
||||
|
||||
def read_message(self):
|
||||
"""Read a single message from the exchange"""
|
||||
message = json.loads(self.reader.readline())
|
||||
if "dir" in message:
|
||||
message["dir"] = Dir(message["dir"])
|
||||
return message
|
||||
|
||||
def send_add_message(
|
||||
self, order_id: int, symbol: str, dir: Dir, price: int, size: int
|
||||
):
|
||||
"""Add a new order"""
|
||||
self._write_message(
|
||||
{
|
||||
"type": "add",
|
||||
"order_id": order_id,
|
||||
"symbol": symbol,
|
||||
"dir": dir,
|
||||
"price": price,
|
||||
"size": size,
|
||||
"tif": "DAY", # 설명서 필수 필드: DAY or IOC
|
||||
}
|
||||
)
|
||||
def send_add_message(self, order_id: int, symbol: str, dir: Dir, price: int, size: int):
|
||||
self._write_message({"type": "add", "order_id": order_id, "symbol": symbol,
|
||||
"dir": dir, "price": price, "size": size, "tif": "DAY"})
|
||||
|
||||
def send_convert_message(self, order_id: int, symbol: str, dir: Dir, size: int):
|
||||
"""Convert between related symbols"""
|
||||
self._write_message(
|
||||
{
|
||||
"type": "convert",
|
||||
"order_id": order_id,
|
||||
"symbol": symbol,
|
||||
"dir": dir,
|
||||
"size": size,
|
||||
}
|
||||
)
|
||||
self._write_message({"type": "convert", "order_id": order_id, "symbol": symbol, "dir": dir, "size": size})
|
||||
|
||||
def send_cancel_message(self, order_id: int):
|
||||
"""Cancel an existing order"""
|
||||
self._write_message({"type": "cancel", "order_id": order_id})
|
||||
|
||||
def _connect(self, add_socket_timeout):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
if add_socket_timeout:
|
||||
# Automatically raise an exception if no data has been recieved for
|
||||
# multiple seconds. This should not be enabled on an "empty" test
|
||||
# exchange.
|
||||
s.settimeout(5)
|
||||
s.connect((self.exchange_hostname, self.port))
|
||||
return s
|
||||
@@ -561,53 +413,30 @@ class ExchangeConnection:
|
||||
what_to_write = json.dumps(message)
|
||||
if not what_to_write.endswith("\n"):
|
||||
what_to_write = what_to_write + "\n"
|
||||
|
||||
length_to_send = len(what_to_write)
|
||||
total_sent = 0
|
||||
while total_sent < length_to_send:
|
||||
sent_this_time = self.writer.send(
|
||||
what_to_write[total_sent:].encode("utf-8")
|
||||
)
|
||||
sent_this_time = self.writer.send(what_to_write[total_sent:].encode("utf-8"))
|
||||
if sent_this_time == 0:
|
||||
raise Exception("Unable to send data to exchange")
|
||||
total_sent += sent_this_time
|
||||
|
||||
now = time.time()
|
||||
self.message_timestamps.append(now)
|
||||
if len(
|
||||
self.message_timestamps
|
||||
) == self.message_timestamps.maxlen and self.message_timestamps[0] > (now - 1):
|
||||
print(
|
||||
"WARNING: You are sending messages too frequently. The exchange will start ignoring your messages. Make sure you are not sending a message in response to every exchange message."
|
||||
)
|
||||
if len(self.message_timestamps) == self.message_timestamps.maxlen and self.message_timestamps[0] > (now - 1):
|
||||
print("WARNING: You are sending messages too frequently. The exchange will start ignoring your messages. Make sure you are not sending a message in response to every exchange message.")
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
test_exchange_port_offsets = {"prod-like": 0, "slower": 1, "empty": 2}
|
||||
|
||||
parser = argparse.ArgumentParser(description="Trade on an ETC exchange!")
|
||||
exchange_address_group = parser.add_mutually_exclusive_group(required=True)
|
||||
exchange_address_group.add_argument(
|
||||
"--production", action="store_true", help="Connect to the production exchange."
|
||||
)
|
||||
exchange_address_group.add_argument(
|
||||
"--test",
|
||||
type=str,
|
||||
choices=test_exchange_port_offsets.keys(),
|
||||
help="Connect to a test exchange.",
|
||||
)
|
||||
|
||||
# Connect to a specific host. This is only intended to be used for debugging.
|
||||
exchange_address_group.add_argument(
|
||||
"--specific-address", type=str, metavar="HOST:PORT", help=argparse.SUPPRESS
|
||||
)
|
||||
|
||||
exchange_address_group.add_argument("--production", action="store_true")
|
||||
exchange_address_group.add_argument("--test", type=str, choices=test_exchange_port_offsets.keys())
|
||||
exchange_address_group.add_argument("--specific-address", type=str, metavar="HOST:PORT", help=argparse.SUPPRESS)
|
||||
args = parser.parse_args()
|
||||
args.add_socket_timeout = True
|
||||
|
||||
if args.production:
|
||||
args.exchange_hostname = "production"
|
||||
args.port = 25000
|
||||
args.exchange_hostname = "production"; args.port = 25000
|
||||
elif args.test:
|
||||
args.exchange_hostname = "test-exch-" + team_name
|
||||
args.port = 22000 + test_exchange_port_offsets[args.test]
|
||||
@@ -616,14 +445,9 @@ def parse_arguments():
|
||||
elif args.specific_address:
|
||||
args.exchange_hostname, port = args.specific_address.split(":")
|
||||
args.port = int(port)
|
||||
|
||||
return args
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Check that [team_name] has been updated.
|
||||
assert team_name != "REPLAC" + "EME", (
|
||||
"Please put your team name in the variable [team_name]."
|
||||
)
|
||||
|
||||
assert team_name != "REPLAC" + "EME"
|
||||
main()
|
||||
Reference in New Issue
Block a user