Skip to content

Rust SDK Overview

The tradectl SDK (tradectl-sdk) provides the building blocks for writing trading strategies.

Installation

Add to your Cargo.toml (or use tradectl init which sets this up automatically):

toml
[dependencies]
tradectl-sdk = "0.1"

Core Types

Events

  • TickerEvent — best bid/ask snapshot with quantities and timestamp
  • TradeEvent — individual trade (price, quantity, timestamp, is_buyer_maker)

Strategy

  • Strategy trait — on_ticker, on_trade, name, describe, params_schema, on_position_close
  • ActionHold, MarketOpen, LimitOpen, ClosePosition, CloseAll, CancelPending
  • StrategyContext — positions, balance, PnL, trade count, timestamp
  • PositionInfo — id, side, entry price, quantity, unrealized PnL
  • SideLong, Short
  • CloseReasonTakeProfit, StopLoss, ForceClose

Parameters

  • ParamsHashMap<String, f64> with .get(key, default) and .set(key, value)
  • ParamDef — schema definition (key, description, default, min, max, step)

Loading Strategies

  • declare_strategy! macro — exports the strategy as a dynamic library entry point

Managers

Composable components injected into strategies:

  • TPSLManager — take-profit / stop-loss management
  • KlineManager — build candles from tick data
  • RangeBarManager — build range bars from tick data

Indicators

Built-in technical indicators (all implement the Indicator trait with .update(value), .value(), .ready()):

  • Sma — Simple Moving Average
  • Ema — Exponential Moving Average
  • Rsi — Relative Strength Index
  • BollingerBands — Bollinger Bands (.upper(), .lower())
  • Macd — MACD (.signal(), .histogram())
  • Atr — Average True Range (.update_hlc(high, low, close))
  • StdDev — Standard Deviation
  • Vwap — Volume-Weighted Average Price (.update_pv(price, volume), .reset())

MarketAdapter

Unified async interface for all exchanges:

rust
pub trait MarketAdapter {
    async fn place_order(&self, order: Order) -> Result<OrderResult>;
    async fn cancel_order(&self, id: &str) -> Result<()>;
    async fn get_position(&self) -> Result<Position>;
    async fn get_balance(&self) -> Result<Balance>;
}

Also implemented by TestExchange for unit testing without network.

tradectl — Automate Crypto Trading