Skip to main content

Python client for HFT Simulator Gateway (handshake, WebSockets, high-level helpers)

Project description

market-client (Python)

A high‑level Python client for the HFT Simulator gateway (server/) that mirrors the website's behavior but hides the server details.

  • Automatically performs handshake and login
  • Keeps a persistent CMD WebSocket session
  • Optionally auto-subscribes to the MD stream and maintains simple in‑memory state (symbols, book snapshots)
  • Provides high‑level helpers (get_symbols, get_book, place_order, list_orders)
  • Minimizes HTTP usage: performs requests over a persistent CMD WebSocket session
  • MD-driven book maintenance and candle aggregation with minimal refreshes

Install

This package depends on httpx and websockets.

  • With pip (editable for local dev):
python3 -m venv .venv
. .venv/bin/activate
pip install -e ./market-client

Or install directly:

pip install httpx>=0.24 websockets>=10

Quickstart (high‑level)

import asyncio
from market_client import MarketClient

async def main():
    # One call connects: handshake -> CMD -> login -> MD
    client = await MarketClient.connect("http://localhost:8000", connect_md=True)

    # High-level helpers
    symbols = await client.get_symbols()
    print("symbols:", symbols[:3])

    book = await client.get_book(symbols[0]["sym"], depth=10)
    print("book:", book)

    # Candles: configure and read
    await client.configure_candles(symbols[0]["sym"], interval_ms=1000, depth=50)
    print("candles:", client.get_candles(symbols[0]["sym"])[:3])

    # Orders
    # await client.place_order(sym, side="buy", price="100.00", qty=1)
    # orders = await client.list_orders()
    # print("orders:", orders)

    await client.aclose()

asyncio.run(main())

See examples/simple.py for a runnable sample.

Notes

  • The client auto-fills the JSON envelope (type, ver, msg_id, ts) and matches responses by corr_id.
  • Tokens are handled automatically by connect().
  • When connect_md=True, the client starts a background task to process MD events and keeps a simple book snapshot per symbol. Future iterations can extend this to persistent candles, etc.

API

  • await MarketClient.connect(base_url, connect_md=True, on_md_event=None, on_cmd_event=None)
    • Performs handshake, opens CMD, logs in, and optionally subscribes to MD.
  • High‑level methods
    • await get_symbols(force_refresh=False) -> list of { sym, tick, min_qty, lot, status }
    • await get_book(sym, depth=20) -> { sym, seq, bids, asks } (arrays)
    • await configure_candles(sym, interval_ms=1000, depth=50) -> seeds window; MD updates candles
    • get_candles(sym) -> list of OHLC entries suitable for charting
    • await send(op, payload={}, as_role=None, timeout=5.0) -> generic WS request/response helper
    • await place_order(sym, side, price, qty, tif='GTC') -> payload dict
    • await cancel_order(order_id) -> payload dict
    • await list_orders() -> list of order dicts
    • book_snapshot(sym, depth=20) -> current in-memory book from MD
    • best_bid_ask(sym) -> dict with top-of-book {bid, ask}

Low‑level access (still available if needed):

  • handshake(), api_request(), open_cmd(), open_md()

Request session class

  • RequestSession: alias of the request-capable CMD WebSocket session used internally for minimal-latency requests.

Server assumptions

  • Matches the FastAPI server under server/ with routes:
    • POST /auth/handshake
    • POST /api/request
    • WS /ws/cmd
    • WS /ws/md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alodenhftmarketclient-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alodenhftmarketclient-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file alodenhftmarketclient-0.1.0.tar.gz.

File metadata

  • Download URL: alodenhftmarketclient-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.9 Darwin/25.0.0

File hashes

Hashes for alodenhftmarketclient-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c7c5a7c84e5dcc346477244cbb0ebee12eb6f230c3af6265f4414283d30f3b5e
MD5 3e584b3245f0086fb5732d3f409271a1
BLAKE2b-256 05544edffdc419a782947afa771d29c007ebf3344411044e729c77a6349bf418

See more details on using hashes here.

File details

Details for the file alodenhftmarketclient-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alodenhftmarketclient-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa2be79542fafa10c6f007919c7365ce212eb787fba20ccc13dd0ffe61b6cf46
MD5 3b0a6693028213a0b4b73c79277be616
BLAKE2b-256 cc673557d9b8c926bb05e03036289067497cdbfe38e267c43372593dc86fb5d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page