Skip to main content

Python client for the HFT Simulator bridge server (REST + WebSockets helpers)

Project description

market-client (Python)

High-level async Python client for the new Market bridge server (server/). The client hides HTTP and WebSocket details, logs in via /login, and exposes ergonomic helpers for common operations (symbols, book, orders, candles) plus simple streaming hooks for market data and per-user unicast events.

Key features:

  • Performs /login and reuses the session cookie automatically for subsequent REST calls.
  • Async helpers for /symbols, /book/{sym}, /order.place, /order.cancel, /orders, /order.history, /portfolio, and /candles.
  • Optional MD (/ws/md) and unicast (/ws/unicast) WebSocket listeners with simple hook registration.
  • Lightweight in-memory ring buffer that records unicast events; configurable capacity plus helper methods to inspect or clear the buffer.

Install

This package depends on httpx and websockets.

Editable install for local development:

python3 -m venv .venv
. .venv/bin/activate
pip install -e ./market-client

Or install the runtime requirements directly:

pip install httpx>=0.24 websockets>=10

Quickstart

import asyncio
from market_client import MarketClient


async def main():
    client = MarketClient("http://localhost:8000")
    await client.login()  # obtains gw_uid cookie + user/session ids

    symbols = await client.get_symbols()
    print("symbols:", [s["sym"] for s in symbols[:3]])

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

    # Optional streaming hooks
    async def on_unicast(evt):
        print("unicast:", evt.get("event"))
    await client.connect_unicast(on_event=on_unicast)

    # Clean up when done
    await client.aclose()

asyncio.run(main())

See examples/simple.py for a minimal sample and examples/order_flow.py for a more involved orchestrator that drives several agents concurrently.

API highlights

  • await MarketClient.connect(base_url, connect_md=False, connect_unicast=False, ...)
    • Convenience classmethod that instantiates the client, calls login(), and optionally attaches MD/unicast listeners.
  • await login() -> LoginInfo
    • Performs POST /login and stores user/session ids for later use (i.e., /ws/unicast?user=...).
  • High-level REST helpers (all async):
    • get_symbols(force_refresh=False) -> list of symbol dicts
    • get_book(sym, depth=20, side="both") -> book snapshot payload
    • place_order(...), cancel_order(...), list_orders(), order_history(...)
    • get_portfolio(), get_candles(...)
  • Streaming:
    • connect_md(symbols=None, on_event=None) / disconnect_md()
    • connect_unicast(on_event=None) / disconnect_unicast()
    • set_md_hook(handler) and set_event_hook(handler) register global callbacks invoked for each MD/unicast event before the per-stream handler.
  • send(op, payload) / api_request(...)
    • Compatibility helpers that translate well-known ops (e.g., symbols.list, book.get, order.place, candles.get, admin.*) into the appropriate REST call. Responses are wrapped in a legacy envelope ({"type":"response","payload":...}) so existing scripts that expect the older format keep working.
  • Transactions ring buffer (unicast events only):
    • configure_transactions(capacity=1000)
    • get_transactions(limit=None)
    • clear_transactions()

Transactions ring buffer example

from market_client import MarketClient

client = MarketClient("http://localhost:8000")
await client.login()
client.configure_transactions(capacity=5000)

def on_unicast(evt):
    print("evt:", evt.get("event"))

await client.connect_unicast(on_event=on_unicast)

# Later...
recent = client.get_transactions(limit=100)
client.clear_transactions()
await client.aclose()

Server assumptions

Matches the FastAPI bridge server in server/ with routes:

  • POST /login
  • GET /symbols
  • GET /book/{sym}
  • POST /order.place
  • POST /order.cancel
  • GET /orders
  • GET /order.history
  • GET /portfolio
  • GET /candles
  • POST /admin/* (requires as_role="admin" in send())
  • WebSockets: /ws/md?symbols=..., /ws/unicast?user=...

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.4.tar.gz (6.7 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.4-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alodenhftmarketclient-0.1.4.tar.gz
  • Upload date:
  • Size: 6.7 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.4.tar.gz
Algorithm Hash digest
SHA256 3e65e2cf5c35da8d83c034d81b37954de6f34326fd6943db8452ed16d9f014ae
MD5 7e7626548459f4c3072f5a5f6c3d8827
BLAKE2b-256 29537cb88da1395adb343370ca220be7cc13f5ef8a932bc7cbfdae0b175106bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alodenhftmarketclient-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1e93702c164286ff4bd6e69e8924452dc0b3f5f7a5066423bba26d23ba487ee6
MD5 c1aecb2e5bad8cd1a0a8cb343a7e2ac6
BLAKE2b-256 facbf28c2b009b41ad3fd1fc12354770e3ca6f44f41bc24700600f8b7eb12f03

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