some change
This commit is contained in:
65
bot.py
65
bot.py
@@ -87,40 +87,49 @@ def on_trade(message: dict, orderman: OrderManager, state: StateManager):
|
||||
|
||||
state.set_last_price(symbol, price)
|
||||
|
||||
# 거래도 진행 // VALBZ와 VALE의 가격이 모두 존재할 때만 거래 시도
|
||||
if symbol == "VALBZ" or symbol == "VALE":
|
||||
print(
|
||||
f"Current prices - VALBZ: {state.get_last_price('VALBZ')}, VALE: {state.get_last_price('VALE')}"
|
||||
)
|
||||
p_valbz = state.get_last_price("VALBZ")
|
||||
p_vale = state.get_last_price("VALE")
|
||||
execute_arb(orderman, state)
|
||||
|
||||
|
||||
def execute_arb(orderman: OrderManager, state: StateManager):
|
||||
p_valbz = state.get_fair_value("VALBZ")
|
||||
p_vale = state.get_fair_value("VALE")
|
||||
p_bond = state.get_fair_value("BOND")
|
||||
p_gs = state.get_fair_value("GS")
|
||||
p_ms = state.get_fair_value("MS")
|
||||
p_wfc = state.get_fair_value("WFC")
|
||||
p_xlf = state.get_fair_value("XLF")
|
||||
|
||||
if p_vale is None or p_valbz is None:
|
||||
print("NONE")
|
||||
return
|
||||
|
||||
vale_to_valbz = p_valbz - p_vale
|
||||
valbz_to_vale = p_vale - p_valbz
|
||||
|
||||
FEE = 10
|
||||
SZ = 10
|
||||
if p_vale + 1 < p_valbz - 1:
|
||||
print("Good")
|
||||
if (p_vale + 1) * SZ + FEE < (p_valbz - 1) * SZ:
|
||||
if abs(orderman.positions["VALE"] + SZ) > orderman.POSITIONS_LIMIT["VALE"] or abs(orderman.positions["VALBZ"] - SZ) > orderman.POSITIONS_LIMIT["VALBZ"]:
|
||||
print("포지션 제한으로 인해 주문이 무시되었습니다.")
|
||||
return
|
||||
if vale_to_valbz > 10:
|
||||
orderman.buy("VALE", Dir.BUY, p_vale, 10)
|
||||
orderman.convert("VALE", Dir.SELL, 10)
|
||||
orderman.sell("VALBZ", Dir.SELL, p_valbz, 10)
|
||||
elif valbz_to_vale > 10:
|
||||
orderman.buy("VALBZ", Dir.BUY, p_valbz, 10)
|
||||
orderman.convert("VALE", Dir.BUY, 10)
|
||||
orderman.sell("VALE", Dir.SELL, p_vale, 10)
|
||||
|
||||
orderman.buy("VALE", p_vale + 1, -SZ)
|
||||
orderman.sell_convert("VALE", SZ)
|
||||
orderman.sell("VALBZ", p_valbz - 1, SZ)
|
||||
elif p_vale - 1 > p_valbz + 1:
|
||||
print("Good")
|
||||
if (p_vale - 1) * SZ - FEE > (p_valbz + 1) * SZ:
|
||||
if abs(orderman.positions["VALE"] - SZ) > orderman.POSITIONS_LIMIT["VALE"] or abs(orderman.positions["VALBZ"] + SZ) > orderman.POSITIONS_LIMIT["VALBZ"]:
|
||||
print("포지션 제한으로 인해 주문이 무시되었습니다.")
|
||||
return
|
||||
orderman.sell("VALE", p_vale - 1, -SZ)
|
||||
orderman.buy_convert("VALE", SZ)
|
||||
orderman.buy("VALBZ", p_valbz + 1, SZ)
|
||||
if p_xlf is not None and all(p is not None for p in [p_bond, p_gs, p_ms, p_wfc]):
|
||||
components = p_bond * 3 + p_gs * 2 + p_ms * 3 + p_wfc * 2
|
||||
xlf_fair = components / 10
|
||||
|
||||
if p_xlf > xlf_fair + 10:
|
||||
orderman.sell("XLF", Dir.SELL, p_xlf, 10)
|
||||
orderman.buy("BOND", Dir.BUY, p_bond, 30)
|
||||
orderman.buy("GS", Dir.BUY, p_gs, 20)
|
||||
orderman.buy("MS", Dir.BUY, p_ms, 30)
|
||||
orderman.buy("WFC", Dir.BUY, p_wfc, 20)
|
||||
elif xlf_fair > p_xlf + 10:
|
||||
orderman.buy("XLF", Dir.BUY, p_xlf, 10)
|
||||
orderman.sell("BOND", Dir.SELL, p_bond, 30)
|
||||
orderman.sell("GS", Dir.SELL, p_gs, 20)
|
||||
orderman.sell("MS", Dir.SELL, p_ms, 30)
|
||||
orderman.sell("WFC", Dir.SELL, p_wfc, 20)
|
||||
|
||||
# ~~~~~============== PROVIDED CODE ==============~~~~~
|
||||
|
||||
|
||||
44
order.py
44
order.py
@@ -1,5 +1,11 @@
|
||||
import time
|
||||
from collections import deque
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Dir(str, Enum):
|
||||
BUY = "BUY"
|
||||
SELL = "SELL"
|
||||
|
||||
|
||||
class OrderManager:
|
||||
@@ -27,13 +33,13 @@ class OrderManager:
|
||||
self.positions = {s: 0 for s in symbols}
|
||||
self.future_positions = {s: 0 for s in symbols}
|
||||
|
||||
def add_order(self, order_id: int, symbol: str, size: int):
|
||||
self.orders[order_id] = size
|
||||
def add_order(self, order_id: int, symbol: str, quantity: int):
|
||||
self.orders[order_id] = {"symbol": symbol, "quantity": quantity}
|
||||
|
||||
def get_order(self, order_id: int):
|
||||
return self.orders.get(order_id)
|
||||
|
||||
def _next_order(self):
|
||||
def next_order(self):
|
||||
self._order_size += 1
|
||||
return self._order_size
|
||||
|
||||
@@ -55,6 +61,9 @@ class OrderManager:
|
||||
# 전송 조건 통과 시 실제 통신 수행
|
||||
if action_type == "add":
|
||||
size = kwargs["size"]
|
||||
dir = kwargs["dir"]
|
||||
if dir == Dir.SELL:
|
||||
size = -size
|
||||
if (
|
||||
self.future_positions[kwargs["symbol"]] + size
|
||||
> self.POSITIONS_LIMIT[kwargs["symbol"]]
|
||||
@@ -72,12 +81,22 @@ class OrderManager:
|
||||
self._send_timestamps.append(now)
|
||||
return True
|
||||
|
||||
def sell(self, symbol: str, price: int, size: int):
|
||||
def sell(self, symbol: str, dir: Dir, price: int, size: int):
|
||||
return self._attempt_send(
|
||||
"add",
|
||||
order_id=self._next_order(),
|
||||
order_id=self.next_order(),
|
||||
symbol=symbol,
|
||||
dir="SELL",
|
||||
dir=dir,
|
||||
price=price,
|
||||
size=size,
|
||||
)
|
||||
|
||||
def buy(self, symbol: str, dir: Dir, price: int, size: int):
|
||||
return self._attempt_send(
|
||||
"add",
|
||||
order_id=self.next_order(),
|
||||
symbol=symbol,
|
||||
dir=dir,
|
||||
price=price,
|
||||
size=size,
|
||||
)
|
||||
@@ -89,7 +108,7 @@ class OrderManager:
|
||||
self.positions["VALE"] -= size
|
||||
self.positions["VALBZ"] += size
|
||||
return self.exchange.send_convert_message(
|
||||
order_id=self._next_order(),
|
||||
order_id=self.next_order(),
|
||||
symbol=symbol,
|
||||
dir="BUY",
|
||||
size=size,
|
||||
@@ -102,19 +121,18 @@ class OrderManager:
|
||||
self.positions["VALE"] += size
|
||||
self.positions["VALBZ"] -= size
|
||||
return self.exchange.send_convert_message(
|
||||
order_id=self._next_order(),
|
||||
order_id=self.next_order(),
|
||||
symbol=symbol,
|
||||
dir="SELL",
|
||||
size=size,
|
||||
)
|
||||
|
||||
def buy(self, symbol: str, price: int, size: int):
|
||||
def convert(self, symbol: str, dir: str, size: int):
|
||||
return self._attempt_send(
|
||||
"add",
|
||||
order_id=self._next_order(),
|
||||
"convert",
|
||||
order_id=self.next_order(),
|
||||
symbol=symbol,
|
||||
dir="BUY",
|
||||
price=price,
|
||||
dir=dir,
|
||||
size=size,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user