Python#
The tradersdk Python module is a PyO3 extension. Every call runs in Rust; Python gets awaitables and plain dicts.
Only two exchanges are exposed so far: BingX (PyBingxTrader) and Bybit (PyBybitTrader). The other eighteen are Rust-only.
Build and install#
It is not on PyPI yet. Build it from the Rust workspace (Python 3.9+):
git clone https://github.com/suenot/tradersdk
cd tradersdk/crates/tradersdk-python
pip install maturin
maturin develop --release
Create a trader#
from tradersdk import PyBybitTrader
trader = PyBybitTrader("API_KEY", "API_SECRET", True, "swap") # testnet=True
Constructor arguments are api_key, api_secret, testnet, market_type. market_type is spot, swap, futures or perp, case-insensitive. Keep real keys in environment variables, not in source.
Call it#
All network methods are async:
import asyncio
from tradersdk import PyBybitTrader
async def main():
t = PyBybitTrader("API_KEY", "API_SECRET", True, "swap")
print(await t.test_connection())
print(await t.get_current_price("BTCUSDT"))
book = await t.get_orderbook("BTCUSDT", 10)
print(book["best_bid"], book["best_ask"], book["spread"])
await t.set_leverage("BTCUSDT", 5)
order = await t.place_limit_order("BTCUSDT", "buy", 0.001, 40000.0)
print(order)
await t.cancel_order("BTCUSDT", order["order_id"])
asyncio.run(main())
Fast entry works the same way as in Rust (see Fast entry):
result = await t.execute_fast_entry("BTCUSDT", "long", 100.0, 10, tp_percent=2.0, sl_percent=1.0)
Sides, errors, return types#
- Order
sideacceptsbuyorlong, andsellorshort. A bad side or market type raisesValueError. - Exchange and network errors raise
RuntimeErrorwith the adapter's message. - Order, ticker, balance and order-book results are dicts.
get_orderbookaddsbest_bid,best_askandspread. Each balance entry addstotal. get_prices_batch(symbols)asks one symbol at a time and returnsNonefor the ones that failed.execute_fast_entrydoes not validatedirectionin Python: anything exceptlongsells.
The two classes expose the same methods but are separate classes, not subclasses of each other. See the Python API reference for every signature.
Limits to know#
- Only the methods in the reference exist. Funding history, open interest, WebSocket streams, dry-run order tests and capability queries are Rust-only today.
- Each call crosses into a Tokio runtime owned by the extension. Do not create a trader per request; build one and reuse it.