diag: Add bybit order ticket webapp for testing purposes
This commit is contained in:
1 parent
fc94ec4d46
commit
63c94e09e7
3 files changed
+707
No files matched your search
@@ -172,6 +172,7 @@ class BybitExecutionGateway(ExecutionGateway):
|
||||
name = "bybit_1"
|
||||
self.name = name
|
||||
cfg = load_config([f"py_{name}"])
|
||||
self.config = cfg
|
||||
self.logger = configure_logger(
|
||||
logging.getLogger(name), "OrderMan", **(cfg.get("logging") or {})
|
||||
)
|
||||
@@ -197,6 +198,9 @@ class BybitExecutionGateway(ExecutionGateway):
|
||||
# sparkd with a special flag. Some algos may keep working through cancels due to disconnect
|
||||
# (instead of the usual 'Cancel on unexpected child cancel' behavior).
|
||||
self.pending_reconnect_cxls: set[str] = set()
|
||||
self.order_ack_callback: Optional[
|
||||
Callable[[dict, Optional[OrderData]], None]
|
||||
] = None
|
||||
self.status: BybitGatewayStatus = BybitGatewayStatus.IDLE
|
||||
self.lock = threading.RLock()
|
||||
|
||||
@@ -259,6 +263,8 @@ class BybitExecutionGateway(ExecutionGateway):
|
||||
'[bybit_gw_ord_ack_err.json]{ "typ": "%s", "exch_id": "%s", "err": "No order data found with exch id", "msg": "%s" }'
|
||||
% (typ, exch_id, msg)
|
||||
)
|
||||
if self.order_ack_callback:
|
||||
self.order_ack_callback(msg, ord_data)
|
||||
|
||||
return (on_ack, on_err)
|
||||
|
||||
@@ -810,3 +816,50 @@ class BybitExecutionGateway(ExecutionGateway):
|
||||
def on_securities(self, securities: PySecurityList):
|
||||
self.logger.info("[bybit_gw_sec.json]%s" % securities)
|
||||
self.securities = {sec.name(): sec for sec in securities}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
from fake_execution_gateway_bridge import FakeExecutionGatewayBridge
|
||||
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--currencies", nargs="+")
|
||||
ap.add_argument("--futures", nargs="+")
|
||||
log_levels = list(logging._nameToLevel)
|
||||
ap.add_argument("--console-level", choices=log_levels)
|
||||
ap.add_argument("--file-level", choices=log_levels)
|
||||
ap.add_argument("--dev", action="store_true", dest="use_testnet")
|
||||
ap.add_argument("--prod", action="store_false", dest="use_testnet")
|
||||
ap.add_argument("--show-config", action="store_true", dest="show_config")
|
||||
ap.set_defaults(
|
||||
currencies=None,
|
||||
futures=None,
|
||||
console_level=None,
|
||||
file_level=None,
|
||||
use_testnet=None,
|
||||
show_config=False,
|
||||
)
|
||||
args = ap.parse_args()
|
||||
gw = BybitExecutionGateway()
|
||||
config = gw.config
|
||||
logging_overrides = {}
|
||||
if args.console_level:
|
||||
logging_overrides["console_level"] = args.console_level
|
||||
if args.file_level:
|
||||
logging_overrides["file_level"] = args.file_level
|
||||
if logging_overrides:
|
||||
config["logging"].update(logging_overrides)
|
||||
if args.use_testnet is not None:
|
||||
config["use_testnet"] = args.use_testnet
|
||||
logger = configure_logger(
|
||||
logging.getLogger("bybit_execution_gateway_app"),
|
||||
"OrderMan",
|
||||
**(config.get("logging") or {}),
|
||||
)
|
||||
if args.show_config:
|
||||
print(json.dumps(config, indent=2))
|
||||
exit()
|
||||
gw_bridge = FakeExecutionGatewayBridge(logger)
|
||||
gw.start(gw_bridge) # type: ignore
|
||||
while True:
|
||||
time.sleep(1)
|
||||
@@ -0,0 +1,420 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple Bybit Order Ticket (Flask)
|
||||
---------------------------------
|
||||
A minimal web UI to place/cancel orders. All actual exchange actions are
|
||||
abstracted behind the TradingBackend class — replace its placeholder methods
|
||||
with calls into your own module.
|
||||
|
||||
Run:
|
||||
pip install flask pydantic
|
||||
python bybit_order_ticket_app.py
|
||||
Then open http://127.0.0.1:5000
|
||||
|
||||
Notes:
|
||||
- This app does NO auth/session management and is meant for a local, trusted
|
||||
environment. Add authentication and CSRF protection before exposing it.
|
||||
- Input validation is performed with Pydantic on the API boundary.
|
||||
- The UI toggles Limit/Market inputs and shows basic responses.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
import flask_socketio
|
||||
# from flask_socketio import SocketIO, send, emit, join_room, leave_room
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from flask import Flask, jsonify, render_template_string, request
|
||||
from pydantic import BaseModel, Field, validator
|
||||
|
||||
from bybit_execution_gateway import BybitExecutionGateway, OrderData
|
||||
from bybit_md_provider import BybitMdProvider
|
||||
from python_interop_module import PyBookList, PyContractQuantityInfo, PyContractQuantityInfoList, PyContractSpecification, PyCreateOrderRequest, PyExecutionReport, PyFuture, PyPositionInfo, PyPositionInfoList, PySecurity, PySecurityList, PyTradeList
|
||||
from fake_execution_gateway_bridge import FakeExecutionGatewayBridge
|
||||
from spark_core import StructuredLog as SLog, configure_logger
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
# -------------- Backend integration points -----------------
|
||||
|
||||
class TradingBackend:
|
||||
"""Hook these methods up to your Bybit wrapper.
|
||||
|
||||
Replace bodies with calls into your own module. Keep the signatures.
|
||||
Raise exceptions when the exchange reports an error; they'll be surfaced
|
||||
as JSON {"ok": false, "error": "..."} to the UI.
|
||||
"""
|
||||
|
||||
def __init__(self, gw:BybitExecutionGateway, md: BybitMdProvider, logger: logging.Logger):
|
||||
self.gw = gw
|
||||
self.gw.order_ack_callback = self.on_order_ack
|
||||
self.logger = logger
|
||||
self.order_ack_event = threading.Event()
|
||||
self.order_acks: list[tuple[dict, Optional[OrderData]]] = []
|
||||
self.books: dict[str, PyBookList] = {}
|
||||
self.trades: list[PyTradeList] = []
|
||||
self.securities: dict[str, PySecurity] = {}
|
||||
|
||||
def disconnect_order_stream(self, req = None):
|
||||
self.gw.stream_cli.ws.close()
|
||||
|
||||
def on_order_ack(self, msg: dict, ord: Optional[OrderData]):
|
||||
self.order_acks.append((msg, ord))
|
||||
self.order_ack_event.set()
|
||||
|
||||
def list_symbols(self) -> List[str]:
|
||||
# TODO: Replace with your symbol discovery (e.g., via /v5/market/instruments)
|
||||
return [
|
||||
"BTCUSDT",
|
||||
"ETHUSDT",
|
||||
"SOLUSDT",
|
||||
"XRPUSDT",
|
||||
]
|
||||
|
||||
def place_order(
|
||||
self,
|
||||
symbol: str,
|
||||
side: str, # "Buy" or "Sell"
|
||||
order_type: str, # "Limit" or "Market"
|
||||
qty: float,
|
||||
price: Optional[float],
|
||||
tif: str, # GTC/IOC/FOK/PostOnly
|
||||
reduce_only: bool = False,
|
||||
post_only: bool = False,
|
||||
client_order_id: Optional[str] = None,
|
||||
extra: Optional[dict[str, Any]] = None,
|
||||
) -> dict[str, Any]:
|
||||
req = PyCreateOrderRequest()
|
||||
if client_order_id:
|
||||
req.set_key(int(client_order_id))
|
||||
req.set_venue("BYBIT")
|
||||
req.set_order_type(order_type.upper())
|
||||
req.set_exchange_security_id(symbol)
|
||||
req.set_security_type("FUTURE")
|
||||
req.set_quantity(qty)
|
||||
req.set_is_buy(side == "Buy")
|
||||
if price is not None:
|
||||
req.set_price(price)
|
||||
req.set_time_in_force(tif)
|
||||
self.order_ack_event.clear()
|
||||
try:
|
||||
self.gw.create_order(req)
|
||||
except Exception as e:
|
||||
self.logger.error("[bybit_order_ticket_ord_err.json]%s" % SLog(err=str(e)))
|
||||
if not self.order_ack_event:
|
||||
self.logger.error("[bybit_order_ticket_ord_err.json]%s" % SLog(err="order_ack_event is None"))
|
||||
return {}
|
||||
got_ack = self.order_ack_event.wait(5)
|
||||
if not got_ack:
|
||||
self.logger.error("[bybit_order_ticket_ord_err.json]%s" % SLog(err="Timed out waiting for ack"))
|
||||
return {}
|
||||
ack, ord = self.order_acks[-1]
|
||||
del self.order_acks[-1]
|
||||
return ack
|
||||
# return {
|
||||
# "status": "accepted",
|
||||
# "symbol": symbol,
|
||||
# "side": side,
|
||||
# "type": order_type,
|
||||
# "qty": qty,
|
||||
# "price": price,
|
||||
# "tif": tif,
|
||||
# "reduce_only": reduce_only,
|
||||
# "post_only": post_only,
|
||||
# "client_order_id": client_order_id,
|
||||
# "order_id": "test-oid-12345",
|
||||
# }
|
||||
|
||||
def cancel_order(
|
||||
self,
|
||||
*,
|
||||
symbol: str,
|
||||
order_id: Optional[str] = None,
|
||||
client_order_id: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
# TODO: Call your module to cancel; return the exchange response
|
||||
return {
|
||||
"status": "cancel_requested",
|
||||
"symbol": symbol,
|
||||
"order_id": order_id,
|
||||
"client_order_id": client_order_id,
|
||||
}
|
||||
|
||||
def get_balances(self) -> dict[str, Any]:
|
||||
# TODO: Plug into your account/balance endpoint
|
||||
return {"USDT": {"available": 1000.0, "total": 1000.0}}
|
||||
|
||||
def get_positions(self) -> list[dict[str, Any]]:
|
||||
# TODO: Plug into your positions endpoint
|
||||
return []
|
||||
|
||||
def on_execution_report(self, er: PyExecutionReport) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[exe_rep.json]%s" % er)
|
||||
|
||||
def on_is_ready(self, is_ready: bool) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[is_ready.json]%s" % SLog(is_ready=is_ready))
|
||||
|
||||
def on_position_info(self, pi: PyPositionInfoList) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[pos_info.json]%s" % pi)
|
||||
|
||||
def on_books(self, books: PyBookList) -> None:
|
||||
if self.logger:
|
||||
self.logger.debug("[books.json]%s" % books)
|
||||
for b in books:
|
||||
self.books[b.exchange_security_id()] = b
|
||||
|
||||
def on_securities(self, securities: PySecurityList) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[securities.json]%s" % securities)
|
||||
for s in securities:
|
||||
self.securities[s.exchange_id()] = s
|
||||
|
||||
def on_trades(self, trades: PyTradeList):
|
||||
if self.logger:
|
||||
self.logger.info("[trades.json]%s" % trades)
|
||||
for t in trades:
|
||||
self.trades.insert(0, t)
|
||||
if len(self.trades) > 100:
|
||||
self.trades = self.trades[:100]
|
||||
|
||||
|
||||
backend: Optional[TradingBackend] = None
|
||||
|
||||
# -------------- Validation models -----------------
|
||||
|
||||
class PlaceOrderBody(BaseModel):
|
||||
symbol: str
|
||||
side: str = Field(pattern=r"^(Buy|Sell)$")
|
||||
order_type: str = Field(pattern=r"^(Limit|Market)$")
|
||||
qty: float = Field(gt=0)
|
||||
price: Optional[float] = Field(default=None)
|
||||
tif: str = Field(pattern=r"^(GTC|IOC|FOK|PostOnly)$")
|
||||
reduce_only: bool = False
|
||||
post_only: bool = False
|
||||
client_order_id: Optional[str] = None
|
||||
is_reconnecting: bool = False
|
||||
|
||||
@validator("price", always=True)
|
||||
def price_required_for_limit(cls, v, values):
|
||||
if values.get("order_type") == "Limit" and (v is None or v <= 0):
|
||||
raise ValueError("price is required and > 0 for Limit orders")
|
||||
return v
|
||||
|
||||
|
||||
class CancelOrderBody(BaseModel):
|
||||
symbol: str
|
||||
order_id: Optional[str] = None
|
||||
client_order_id: Optional[str] = None
|
||||
|
||||
@validator("client_order_id")
|
||||
def one_of_ids(cls, v, values):
|
||||
if not v and not values.get("order_id"):
|
||||
raise ValueError("Provide order_id or client_order_id")
|
||||
return v
|
||||
|
||||
|
||||
# -------------- Flask App -----------------
|
||||
app = Flask(__name__)
|
||||
socketio = flask_socketio.SocketIO(app, async_mode="threading")
|
||||
gw: Optional[BybitExecutionGateway] = None
|
||||
|
||||
|
||||
# -- Pages --
|
||||
@app.get("/")
|
||||
def index():
|
||||
html = ""
|
||||
with open("/workspaces/spark/src/lib_python_interop/python_interop_plugins/bybit_order_ticket_app.xml") as f:
|
||||
html = f.read()
|
||||
return render_template_string(html, symbols=backend.list_symbols())
|
||||
|
||||
|
||||
# -- APIs --
|
||||
# @app.post("/api/reconnect")
|
||||
# def handle_reconnect():
|
||||
# print("Simulating reconnect")
|
||||
# if gw.stream_cli:
|
||||
# gw.stream_cli.ws.close()
|
||||
# return jsonify({"ok": True})
|
||||
# return jsonify({"ok": False})
|
||||
# # socketio.send(f"Echo: {msg}", broadcast=True)
|
||||
|
||||
@socketio.on('message')
|
||||
def handle_reconnect(msg):
|
||||
logger.info("[reconnect_req.json]%s" % msg)
|
||||
req = json.loads(msg)
|
||||
delay_secs = int(req.get("reconnect_after") or 1)
|
||||
socketio.send(f"Reconnecting in {delay_secs}s")
|
||||
if backend:
|
||||
def delay_impl():
|
||||
# time.sleep(delay_secs)
|
||||
backend.disconnect_order_stream(req)
|
||||
# threading.Thread(target=delay_impl)
|
||||
delay_impl()
|
||||
|
||||
@app.get("/api/symbols")
|
||||
def api_symbols():
|
||||
return jsonify({"ok": True, "symbols": backend.list_symbols()})
|
||||
|
||||
|
||||
@app.get("/api/balances")
|
||||
def api_balances():
|
||||
try:
|
||||
return jsonify({"ok": True, "balances": backend.get_balances()})
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@app.get("/api/positions")
|
||||
def api_positions():
|
||||
try:
|
||||
return jsonify({"ok": True, "positions": backend.get_positions()})
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@app.post("/api/order")
|
||||
def api_place_order():
|
||||
try:
|
||||
data = PlaceOrderBody(**request.get_json(force=True))
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": f"Invalid request: {e}"}), 400
|
||||
|
||||
if backend is None:
|
||||
return jsonify({"ok": False, "result": "Backend not ready"})
|
||||
try:
|
||||
resp = backend.place_order(
|
||||
symbol=data.symbol,
|
||||
side=data.side,
|
||||
order_type=data.order_type,
|
||||
qty=data.qty,
|
||||
price=data.price,
|
||||
tif=data.tif,
|
||||
reduce_only=data.reduce_only,
|
||||
post_only=data.post_only,
|
||||
client_order_id=data.client_order_id,
|
||||
)
|
||||
return jsonify({"ok": True, "result": resp})
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@app.post("/api/cancel")
|
||||
def api_cancel_order():
|
||||
try:
|
||||
data = CancelOrderBody(**request.get_json(force=True))
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": f"Invalid request: {e}"}), 400
|
||||
|
||||
try:
|
||||
resp = backend.cancel_order(
|
||||
symbol=data.symbol,
|
||||
order_id=data.order_id,
|
||||
client_order_id=data.client_order_id,
|
||||
)
|
||||
return jsonify({"ok": True, "result": resp})
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
class MyExecutionGatewayBridge:
|
||||
def __init__(self, logger: Optional[logging.Logger] = logging.getLogger()):
|
||||
self.logger = logger
|
||||
|
||||
def on_execution_report(self, er: PyExecutionReport) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[exe_rep.json]%s" % er)
|
||||
|
||||
def on_is_ready(self, is_ready: bool) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[is_ready.json]%s" % SLog(is_ready=is_ready))
|
||||
|
||||
def on_position_info(self, pi: PyPositionInfoList) -> None:
|
||||
if self.logger:
|
||||
self.logger.info("[pos_info.json]%s" % pi)
|
||||
|
||||
|
||||
def run_gw(gw: BybitExecutionGateway, bridge):
|
||||
gw.start(bridge)
|
||||
secs = PySecurityList()
|
||||
sec = PySecurity()
|
||||
sec.set_id(2)
|
||||
sec.set_exchange_id("BTCUSDT")
|
||||
sec.set_currency("USDT")
|
||||
sec.set_group("BTC")
|
||||
sec.set_name("BTCUSDT")
|
||||
sec.set_asset("BTC")
|
||||
fut = PyFuture()
|
||||
fut.set_expiration("2100-01-01")
|
||||
sec.set_future(fut)
|
||||
spec = PyContractSpecification()
|
||||
qty_infos = PyContractQuantityInfoList()
|
||||
qi = PyContractQuantityInfo()
|
||||
qi.set_tick_base(100)
|
||||
qi.set_tick_unit(1)
|
||||
qi.set_max_price(150000)
|
||||
qi.set_min_value(0.003)
|
||||
qi.set_max_value(1)
|
||||
qi.set_multiplier(1)
|
||||
qty_infos.append(qi)
|
||||
spec.set_quantity_infos(qty_infos)
|
||||
nqty_infos = PyContractQuantityInfoList()
|
||||
spec.set_notional_quantity_infos(nqty_infos)
|
||||
secs.append(sec)
|
||||
gw.on_securities(secs)
|
||||
|
||||
|
||||
def run_md(md: BybitMdProvider, bridge):
|
||||
md.start(bridge)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--port", type=int, help="Web server port")
|
||||
log_levels = list(logging._nameToLevel)
|
||||
ap.add_argument("--console-level", choices=log_levels)
|
||||
ap.add_argument("--file-level", choices=log_levels)
|
||||
ap.add_argument("--dev", action="store_true", dest="use_testnet")
|
||||
ap.add_argument("--prod", action="store_false", dest="use_testnet")
|
||||
ap.add_argument("--show-config", action="store_true", dest="show_config")
|
||||
ap.set_defaults(
|
||||
currencies=None,
|
||||
futures=None,
|
||||
console_level=None,
|
||||
file_level=None,
|
||||
use_testnet=None,
|
||||
show_config=False,
|
||||
)
|
||||
args = ap.parse_args()
|
||||
gw = BybitExecutionGateway()
|
||||
config = gw.config
|
||||
logging_overrides = {}
|
||||
if args.console_level:
|
||||
logging_overrides["console_level"] = args.console_level
|
||||
if args.file_level:
|
||||
logging_overrides["file_level"] = args.file_level
|
||||
if logging_overrides:
|
||||
config["logging"].update(logging_overrides)
|
||||
if args.use_testnet is not None:
|
||||
config["use_testnet"] = args.use_testnet
|
||||
if args.show_config:
|
||||
print(json.dumps(config, indent=2))
|
||||
exit()
|
||||
logger = configure_logger(
|
||||
logging.getLogger("bybit_execution_gateway_app"), "OrderMan", **(config.get("logging") or {})
|
||||
)
|
||||
md = BybitMdProvider()
|
||||
backend = TradingBackend(gw, md, logger)
|
||||
run_gw(gw, backend)
|
||||
# run_md(md, backend)
|
||||
app.run(host=os.getenv("HOST", "127.0.0.1"), port=int(os.getenv("PORT", "5000")), debug=os.getenv("FLASK_DEBUG", "0") == "1")
|
||||
@@ -0,0 +1,234 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Bybit Order Ticket</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
|
||||
<style>
|
||||
:root { --bg:#0e1117; --card:#161a22; --muted:#8b949e; --fg:#e6edf3; --accent:#3b82f6; --danger:#ef4444; }
|
||||
* { box-sizing: border-box; }
|
||||
body { margin:0; font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif; background:var(--bg); color:var(--fg); }
|
||||
.wrap { max-width: 920px; margin: 24px auto; padding: 0 16px; }
|
||||
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
||||
.card { background: var(--card); border-radius: 16px; padding: 16px; box-shadow: 0 8px 24px rgba(0,0,0,0.25); }
|
||||
label { display:block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
|
||||
input, select { width: 100%; padding: 10px 12px; background: #0b0f15; color: var(--fg); border: 1px solid #2a2f39; border-radius: 10px; }
|
||||
input:disabled { opacity: .6; }
|
||||
.row { display:grid; grid-template-columns: 1fr 1fr; gap: 12px; }
|
||||
.btnbar { display:flex; gap: 10px; }
|
||||
button { padding: 10px 14px; border:0; border-radius: 12px; cursor:pointer; color:#fff; }
|
||||
.buy { background:#16a34a; }
|
||||
.sell { background:#dc2626; }
|
||||
.ghost { background: #2a2f39; color: var(--fg); }
|
||||
.muted { color: var(--muted); font-size: 12px; }
|
||||
.toast { position: fixed; right: 16px; bottom: 16px; background:#0b0f15; border:1px solid #2a2f39; padding: 12px 14px; border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.4); display:none; }
|
||||
table { width:100%; border-collapse: collapse; }
|
||||
th, td { text-align:left; padding:8px 6px; border-bottom:1px solid #232833; font-size: 13px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<h2 style="margin: 6px 0 18px 0;">Bybit Order Ticket</h2>
|
||||
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<form id="ticket">
|
||||
<div class="row">
|
||||
<div>
|
||||
<label>Symbol</label>
|
||||
<select id="symbol" required>
|
||||
{% for s in symbols %}
|
||||
<option value="{{s}}">{{s}}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label>Order Type</label>
|
||||
<select id="otype">
|
||||
<option value="Limit" selected>Limit</option>
|
||||
<option value="Market">Market</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row" style="margin-top: 12px;">
|
||||
<div>
|
||||
<label>Quantity</label>
|
||||
<input id="qty" type="number" min="0" step="any" placeholder="e.g. 0.01" required />
|
||||
</div>
|
||||
<div>
|
||||
<label>Limit Price</label>
|
||||
<input id="price" type="number" min="0" step="any" placeholder="e.g. 65000" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row" style="margin-top: 12px;">
|
||||
<div>
|
||||
<label>Time In Force</label>
|
||||
<select id="tif">
|
||||
<option value="GTC">GTC</option>
|
||||
<option value="IOC">IOC</option>
|
||||
<option value="FOK">FOK</option>
|
||||
<option value="PostOnly">PostOnly</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label>Client Order ID (optional)</label>
|
||||
<input id="cid" type="text" placeholder="my-oid-123" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row" style="margin-top: 12px;">
|
||||
<div>
|
||||
<label><input type="checkbox" id="reduce_only"/> Reduce-Only</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="checkbox" id="post_only"/> Post-Only</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="btnbar" style="margin-top: 14px;">
|
||||
<button type="button" class="buy" onclick="submitOrder('Buy')">Buy</button>
|
||||
<button type="button" class="sell" onclick="submitOrder('Sell')">Sell</button>
|
||||
<button type="button" class="ghost" onclick="cancelByClientId()">Cancel by ClientId</button>
|
||||
<button type="button" class="Reconnect" onclick="reconnect()">Reconnect websocket</button>
|
||||
</div>
|
||||
<div class="muted" style="margin-top:8px;">Tip: Limit orders require price; Market ignores it. PostOnly is only meaningful for Limit.</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:6px;">
|
||||
<strong>Balances</strong>
|
||||
<button class="ghost" onclick="refreshBalances()">Refresh</button>
|
||||
</div>
|
||||
<div id="balances">—</div>
|
||||
|
||||
<div style="display:flex; justify-content:space-between; align-items:center; margin:16px 0 6px 0;">
|
||||
<strong>Positions</strong>
|
||||
<button class="ghost" onclick="refreshPositions()">Refresh</button>
|
||||
</div>
|
||||
<div id="positions">—</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top:16px;">
|
||||
<div style="display:flex; justify-content:space-between; align-items:center;">
|
||||
<strong>API Result</strong>
|
||||
<button class="ghost" onclick="clearResult()">Clear</button>
|
||||
</div>
|
||||
<pre id="result" style="white-space:pre-wrap; overflow:auto; max-height: 280px; background:#0b0f15; padding:12px; border-radius:10px; margin-top:8px;"></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="toast" id="toast"></div>
|
||||
|
||||
<script>
|
||||
const otype = document.getElementById('otype');
|
||||
const price = document.getElementById('price');
|
||||
var socket = io();
|
||||
// const ws = new WebSocket('ws://127.0.0.1:5000/gateway/status');
|
||||
// ws.addEventListener('message', (event) => {
|
||||
// console.log("Recv ws: ", event.data);
|
||||
// });
|
||||
|
||||
socket.on("message", function(data) {
|
||||
console.log("Socket recv - ", data);
|
||||
});
|
||||
|
||||
function updatePriceState(){
|
||||
const isLimit = otype.value === 'Limit';
|
||||
price.disabled = !isLimit;
|
||||
if (!isLimit) price.value = '';
|
||||
}
|
||||
otype.addEventListener('change', updatePriceState);
|
||||
updatePriceState();
|
||||
|
||||
function toast(msg) {
|
||||
const t = document.getElementById('toast');
|
||||
t.textContent = msg;
|
||||
t.style.display = 'block';
|
||||
setTimeout(()=> t.style.display = 'none', 2500);
|
||||
}
|
||||
|
||||
function getTicketPayload(side) {
|
||||
const payload = {
|
||||
symbol: document.getElementById('symbol').value,
|
||||
side,
|
||||
order_type: document.getElementById('otype').value,
|
||||
qty: parseFloat(document.getElementById('qty').value),
|
||||
price: document.getElementById('price').value ? parseFloat(document.getElementById('price').value) : null,
|
||||
tif: document.getElementById('tif').value,
|
||||
reduce_only: document.getElementById('reduce_only').checked,
|
||||
post_only: document.getElementById('post_only').checked,
|
||||
client_order_id: document.getElementById('cid').value || null,
|
||||
};
|
||||
return payload;
|
||||
}
|
||||
|
||||
async function submitOrder(side) {
|
||||
const body = getTicketPayload(side);
|
||||
const res = await fetch('/api/order', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body) });
|
||||
const json = await res.json();
|
||||
document.getElementById('result').textContent = JSON.stringify(json, null, 2);
|
||||
toast(json.ok ? 'Order sent' : `Error: ${json.error}`);
|
||||
}
|
||||
|
||||
async function cancelByClientId(){
|
||||
const symbol = document.getElementById('symbol').value;
|
||||
const cid = document.getElementById('cid').value;
|
||||
if (!cid) { toast('Enter a Client Order ID first'); return; }
|
||||
const res = await fetch('/api/cancel', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ symbol, client_order_id: cid }) });
|
||||
const json = await res.json();
|
||||
document.getElementById('result').textContent = JSON.stringify(json, null, 2);
|
||||
toast(json.ok ? 'Cancel requested' : `Error: ${json.error}`);
|
||||
}
|
||||
|
||||
async function reconnect() {
|
||||
// const body = { reconnect_after: 5 };
|
||||
// const res = await fetch('/api/reconnect', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body)});
|
||||
const body = { reconnect_after: 5 };
|
||||
socket.send(JSON.stringify(body));
|
||||
// const res = await fetch('/api/reconnect', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body) });
|
||||
// const json = await res.json();
|
||||
}
|
||||
|
||||
function renderTable(containerId, obj) {
|
||||
const el = document.getElementById(containerId);
|
||||
if (!obj || (Array.isArray(obj) && obj.length === 0)) { el.textContent = '—'; return; }
|
||||
if (Array.isArray(obj)) {
|
||||
const keys = Array.from(new Set(obj.flatMap(o => Object.keys(o))));
|
||||
const rows = obj.map(o => `<tr>${keys.map(k=>`<td>${escapeHtml(String(o[k] ?? ''))}</td>`).join('')}</tr>`).join('');
|
||||
el.innerHTML = `<div style="overflow:auto"><table><thead><tr>${keys.map(k=>`<th>${escapeHtml(k)}</th>`).join('')}</tr></thead><tbody>${rows}</tbody></table></div>`;
|
||||
} else {
|
||||
const rows = Object.entries(obj).map(([k,v])=>`<tr><th>${escapeHtml(k)}</th><td>${escapeHtml(typeof v === 'object' ? JSON.stringify(v) : String(v))}</td></tr>`).join('');
|
||||
el.innerHTML = `<div style="overflow:auto"><table><tbody>${rows}</tbody></table></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(s){return s.replace(/[&<>"']/g, c=>({'&':'&','<':'<','>':'>','\"':'"','\'':'''}[c]))}
|
||||
|
||||
async function refreshBalances(){
|
||||
const res = await fetch('/api/balances');
|
||||
const json = await res.json();
|
||||
if (json.ok) renderTable('balances', json.balances); else toast('Error loading balances');
|
||||
}
|
||||
|
||||
async function refreshPositions(){
|
||||
const res = await fetch('/api/positions');
|
||||
const json = await res.json();
|
||||
if (json.ok) renderTable('positions', json.positions); else toast('Error loading positions');
|
||||
}
|
||||
|
||||
// Initial load
|
||||
refreshBalances();
|
||||
refreshPositions();
|
||||
|
||||
function clearResult(){ document.getElementById('result').textContent = ''; }
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user