Quickstart#
This walks through the common first steps on a testnet. Use testnet keys, not production keys.
1. Create a trader#
use tradersdk::prelude::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let credentials = ExchangeCredentials {
api_key: "your_key".into(),
api_secret: "your_secret".into(),
passphrase: None, // OKX, Bitget, KuCoin, Bitmart and WEEX require one
testnet: true,
};
let trader = create_trader("bybit", credentials, MarketType::Swap)?;
println!("{} / {}", trader.exchange_name(), trader.market_type());
Ok(())
}
create_trader returns an Arc<dyn ExchangeTrader>, so you can clone it cheaply and share it between tasks. The same builder pattern exists as ExchangeCredentials::new(key, secret).with_passphrase("...").with_testnet(true).
2. Read market data#
let price = trader.get_current_price("BTCUSDT").await?;
let ticker = trader.get_ticker("BTCUSDT").await?;
let book = trader.get_orderbook("BTCUSDT", 10).await?;
println!("last {price}, funding {:?}, top bid {:?}", ticker.funding_rate, book.bids.first());
Optional methods such as get_ticker return an error on adapters that do not implement them. Check capabilities first if you target more than one exchange.
3. Check the account#
let balance = trader.get_balance().await?;
println!("{} available of {} {}", balance.available_balance, balance.total_balance, balance.currency);
4. Validate an order without sending it#
let check = trader.test_market_order("BTCUSDT", OrderSide::Buy, 50.0).await?;
println!("would buy {} for about {} USDT", check.quantity, check.estimated_cost);
This computes quantity from the current price and does not place anything. See Dry-run checks.
5. Place a real order#
let result = trader.place_market_order("BTCUSDT", OrderSide::Buy, 0.001).await?;
if result.success {
println!("order {} filled at {:?}", result.order_id(), result.entry_price);
} else {
eprintln!("rejected: {:?}", result.error);
}
Always check result.success. A rejected order can come back as Ok with success == false and an error message. See Placing orders.
Where next#
- The ExchangeTrader trait: every method and its default.
- Fast entry: market entry with leverage and take-profit / stop-loss in one call.
- Order updates: stream fills over a private WebSocket.