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