hotfix: Reject new orders when bybit gw not in READY state

This commit is contained in:
John Colvin committed 2025-08-15 13:42:33 +00:00
1 parent a6d23ef4ef
commit 164ca306f5
1 file changed
+81 -3
@@ -5,6 +5,8 @@ import time
from typing import Callable, Optional
import uuid
import websocket
from pybit_unified_trading import WebSocket, WebSocketTrading, HTTP
from pybit_websocket_stream import WebSocketError
from spark_core import (
@@ -717,8 +719,11 @@ class BybitExecutionGateway(ExecutionGateway):
self.logger.info("[bybit_gw_stop.json]%s" % SLog())
self.stop_event.set()
def create_order(self, req: PyCreateOrderRequest):
def _create_order(self, req: PyCreateOrderRequest):
self.logger.info("[bybit_gw_crt_ord.json]%s" % req)
if self.status != BybitGatewayStatus.READY:
self._on_abort(req.key(), "crt", "Status is %s" % self.status.name)
return
if not self.stream_cli or not self.trade_cli or not self.bridge:
self._on_abort(
req.key(),
@@ -769,8 +774,11 @@ class BybitExecutionGateway(ExecutionGateway):
self.logger.info("[bybit_gw_crt_ord_send.json]%s" % SLog(params=params))
self.trade_cli.place_order(on_ack, **params, error_callback=on_err)
def cancel_replace_order(self, req: PyCancelReplaceOrderRequest):
def _cancel_replace_order(self, req: PyCancelReplaceOrderRequest):
self.logger.info("[bybit_gw_cxlrep_ord.json]%s" % req)
if self.status != BybitGatewayStatus.READY:
self._on_abort(req.key(), "cxlrep", "Status is %s" % self.status.name)
return
if not self.stream_cli or not self.trade_cli:
self._on_abort(req.key(), "cxlrep", "WebSocket client not created.")
return
@@ -794,8 +802,11 @@ class BybitExecutionGateway(ExecutionGateway):
self.logger.info("[bybit_gw_cxlrep_ord_send.json]%s" % SLog(params=params))
self.trade_cli.amend_order(on_ack, **params, error_callback=on_err)
def cancel_order(self, req: PyCancelOrderRequest):
def _cancel_order(self, req: PyCancelOrderRequest):
self.logger.info("[bybit_gw_cxl_ord.json]%s" % req)
if self.status != BybitGatewayStatus.READY:
self._on_abort(req.key(), "cxl", "Status is %s" % self.status.name)
return
if not self.stream_cli or not self.trade_cli:
self._on_abort(req.key(), "cxl", "WebSocket client not created.")
return
@@ -813,6 +824,73 @@ class BybitExecutionGateway(ExecutionGateway):
self.logger.info("[bybit_gw_cxl_ord_send.json]%s" % SLog(params=params))
self.trade_cli.cancel_order(on_ack, **params, error_callback=on_err)
def create_order(self, req: PyCreateOrderRequest):
try:
with self.lock:
self._create_order(req)
return
except websocket.WebSocketConnectionClosedException as e:
self.logger.error("[bybit_crt_ord_err.json]%s" % SLog(err=str(e)))
er = PyExecutionReport()
ts = time.time_ns()
er.set_data(
key=req.key(),
account=self.account,
exchange_order_id="",
exchange_security_id=req.exchange_security_id(),
venue=req.venue(),
security_type=req.security_type(),
order_type=req.order_type(),
order_status=OrderStatus.REJECTED.name,
time_in_force=req.time_in_force(),
exec_type=ExecType.REJECTED.name,
is_buy=req.is_buy(),
price=req.price(),
quantity=req.quantity(),
filled_quantity=0.0,
last_filled_quantity=0.0,
last_filled_price=0.0,
avg_filled_price=0.0,
transact_id=ts,
transact_time=ts,
)
er.set_error_msg("RECONNECTING")
self.bridge.on_execution_report(er)
def cancel_replace_order(self, req: PyCancelReplaceOrderRequest):
try:
with self.lock:
self._cancel_replace_order(req)
return
except websocket.WebSocketConnectionClosedException as e:
self.logger.error("[bybit_cxlrep_ord_err.json]%s" % SLog(err=str(e)))
od = None
with self.lock:
od = self._get_order_data(req.exchange_order_id())
if not od:
return
od.status = OrderStatus.REJECTED
er = od.to_execution_report()
er.set_error_msg("RECONNECTING")
self.bridge.on_execution_report(er)
def cancel_order(self, req: PyCancelOrderRequest):
try:
with self.lock:
self._cancel_order(req)
return
except websocket.WebSocketConnectionClosedException as e:
self.logger.error("[bybit_cxl_ord_err.json]%s" % SLog(err=str(e)))
od = None
with self.lock:
od = self._get_order_data(req.exchange_order_id())
if not od:
return
od.status = OrderStatus.REJECTED
er = od.to_execution_report()
er.set_error_msg("RECONNECTING")
self.bridge.on_execution_report(er)
def on_securities(self, securities: PySecurityList):
self.logger.info("[bybit_gw_sec.json]%s" % securities)
self.securities = {sec.name(): sec for sec in securities}