52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
import time
|
|
from collections import deque
|
|
from ETC import ExchangeConnection, Dir
|
|
from position import PositionManager
|
|
|
|
class OrderManager:
|
|
def __init__(self, exchange: ExchangeConnection, positionMan: PositionManager):
|
|
self.exchange = exchange
|
|
self._order_size = 0
|
|
|
|
self._send_timestamps = deque(maxlen=500)
|
|
|
|
self.positionMan = positionMan
|
|
|
|
def _next_order(self):
|
|
self._order_size += 1
|
|
return self._order_size
|
|
|
|
def _attempt_send(self, action_type: str, order_id: int, symbol: str, dir: Dir, price: int, size: int) -> bool:
|
|
"""
|
|
주문 전송을 시도합니다.
|
|
제한에 걸려 무시되면 False를, 정상 전송되면 True를 반환합니다.
|
|
"""
|
|
now = time.time()
|
|
|
|
if len(self._send_timestamps) == 500:
|
|
elapsed = now - self._send_timestamps[0]
|
|
if elapsed < 1.01:
|
|
return False
|
|
|
|
# 전송 조건 통과 시 실제 통신 수행
|
|
if action_type == "add":
|
|
self.exchange.send_add_message(symbol=symbol, order_id=order_id, dir=dir, price=price, size=size)
|
|
elif action_type == "cancel":
|
|
self.exchange.send_cancel_message(order_id=order_id)
|
|
elif action_type == "convert":
|
|
self.exchange.send_convert_message(symbol=symbol, order_id=order_id, dir=dir, size=size)
|
|
|
|
self._send_timestamps.append(now)
|
|
return True
|
|
|
|
def sell(self, symbol: str, price: int, size: int):
|
|
return self._attempt_send("add", order_id=self._next_order(), symbol=symbol, dir=Dir.SELL, price=price, size=size)
|
|
|
|
def buy(self, symbol: str, price: int, size: int):
|
|
return self._attempt_send("add", order_id=self._next_order(), symbol=symbol, dir=Dir.BUY, price=price, size=size)
|
|
|
|
def convert(self, symbol: str, dir: Dir, size: int):
|
|
return self._attempt_send("convert", order_id=self._next_order(), symbol=symbol, dir=dir, size=size)
|
|
|
|
def cancel(self, order_id: int):
|
|
return self._attempt_send("cancel", order_id=order_id) |