TraderSDKdocs

Concepts

The ExchangeTrader trait#

Every adapter implements ExchangeTrader from tradersdk-core. Code written against the trait works with any exchange. A new adapter that implements it works with the rest of the SDK.

use tradersdk::prelude::*;   // ExchangeTrader, create_trader, ExchangeCredentials, models

let trader: std::sync::Arc<dyn ExchangeTrader> =
    create_trader("okx", credentials, MarketType::Swap)?;

The trait is Send + Sync and uses async_trait, so a trader can be shared across tasks and stored as Arc<dyn ExchangeTrader>.

Required and optional methods#

Methods fall in two groups.

  • Required. Every adapter must implement them.
  • Optional with a default. The default either returns an error ("<method> not implemented for <exchange>") or provides a generic implementation. An adapter overrides the default when the exchange supports the feature.

Use capabilities to find out ahead of time what an adapter overrides.

Basic info#

Method Returns Notes
exchange_name() &str Short lowercase adapter name, for example "bybit".
market_type() MarketType Spot or Swap, fixed when the trader is created.
capabilities() ExchangeCapabilities Default is all Unknown. Adapters override it.

Connection and account#

Method Returns Notes
test_connection() Result<bool> Required.
get_server_time() Result<i64> Required. Milliseconds since epoch.
get_balance() Result<AccountBalance> Required. See Account and leverage.

Market data#

Method Returns Default
get_current_price(symbol) Result<f64> Required.
get_ticker(symbol) Result<TickerInfo> Error. Includes open interest and funding rate on perpetuals.
get_orderbook(symbol, depth) Result<Orderbook> Error. REST snapshot.
get_funding_rate_history(symbol, limit) Result<Vec<FundingRateEntry>> Error.
get_open_interest_history(symbol, interval, limit) Result<Vec<OpenInterestEntry>> Error. interval is an exchange-specific string.

See Market data.

Leverage#

set_leverage(symbol: Option<&str>, leverage: u32) -> Result<bool> is required. Spot traders generally treat it as a no-op that returns Ok(true). Behavior of None for the symbol depends on the adapter. See Account and leverage.

Trading#

Method Returns Default
place_market_order(symbol, side, quantity) Result<OrderResult> Required.
place_limit_order(symbol, side, quantity, price, reduce_only) Result<OrderResult> Required.
execute_fast_entry(symbol, direction, usdt_amount, leverage, tp_percent, sl_percent, current_price) Result<OrderResult> Required. See Fast entry.
cancel_order(symbol, order_id) Result<bool> Required.
get_order_status(symbol, order_id) Result<OrderInfo> Error.
close_position(symbol) Result<OrderResult> Required.

quantity is in base-asset units (for BTCUSDT, BTC), not USDT. See Placing orders.

Symbol formatting#

format_symbol(base_symbol) -> String has a generic default: uppercases the input, removes /, and appends USDT if the symbol does not already end with USDT or USD. So "btc", "BTC/USDT" and "BTCUSDT" all become "BTCUSDT". Adapters override it for exchanges with their own format. See Symbols and markets.

Dry-run and WebSocket checks#

Method Purpose
test_market_order(symbol, side, usdt_amount) Validate sizing without placing an order. Generic default uses the current price.
test_limit_order(symbol, side, usdt_amount, price) Same for limit orders.
test_ws_orderbook, test_ws_trades, test_ws_ticker Subscribe for a timeout and report latency and message count. Default: error.

See Dry-run checks.

Private WebSocket#

subscribe_order_updates(sender: broadcast::Sender<OrderUpdateEvent>) -> Result<JoinHandle<()>> spawns a background task that holds a private WebSocket connection, publishes order events on your channel, and reconnects with exponential backoff. Abort the returned handle to stop it. Default: error. See Order updates.

Edit this page on GitHub ↗