MarketData

Trait MarketData 

Source
pub trait MarketData: PublicExchange {
Show 20 methods // Required methods fn fetch_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Market>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn load_markets_with_reload<'life0, 'async_trait>( &'life0 self, reload: bool, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn market<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Arc<Market>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Arc<HashMap<String, Arc<Market>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn fetch_ticker<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Ticker>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fetch_tickers<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, symbols: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = Result<Vec<Ticker>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn fetch_order_book_with_params<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, params: OrderBookParams, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fetch_trades_with_limit<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, since: Option<i64>, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fetch_ohlcv_with_params<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, params: OhlcvParams, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; // Provided methods fn load_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } fn reload_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } fn has_symbol<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_all_tickers<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ticker>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } fn fetch_order_book<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_order_book_with_depth<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, limit: u32, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_trades<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_trades_with_limit_only<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_ohlcv_u64<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, timeframe: Timeframe, since: Option<u64>, limit: Option<u32>, until: Option<u64>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_trades_u64<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, since: Option<u64>, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fetch_ohlcv<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, timeframe: Timeframe, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... }
}
Expand description

Trait for fetching public market data.

This trait provides methods for accessing public market information that doesn’t require authentication. All methods are async and return Result<T> for proper error handling.

§Timestamp Format

All timestamp parameters and fields in returned data structures use:

  • Type: i64
  • Unit: Milliseconds since Unix epoch (January 1, 1970, 00:00:00 UTC)
  • Example: 1609459200000 represents January 1, 2021, 00:00:00 UTC

§Supertrait

Requires PublicExchange as a supertrait to access exchange metadata and capabilities.

§Thread Safety

This trait requires Send + Sync bounds (inherited from PublicExchange) to ensure safe usage across thread boundaries in async contexts.

Required Methods§

Source

fn fetch_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Market>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Fetch all available markets from the exchange.

Returns a vector of Market structures containing information about each trading pair including symbols, precision, limits, and fees.

§Example
let markets = exchange.fetch_markets().await?;
for market in markets {
    println!("{}: {} ({})", market.symbol, market.base, market.quote);
}
Source

fn load_markets_with_reload<'life0, 'async_trait>( &'life0 self, reload: bool, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Internal method to load markets with optional reload.

Implementations should cache markets and only fetch from the API when reload is true or the cache is empty.

Source

fn market<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Arc<Market>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a specific market by symbol.

Returns the market information for the given symbol, or an error if the symbol is not found.

Source

fn markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Arc<HashMap<String, Arc<Market>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get all cached markets.

Returns the currently cached markets without fetching from the API.

Source

fn fetch_ticker<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Ticker>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch ticker for a single symbol.

Returns current price and volume information for the specified symbol.

§Arguments
  • symbol - The trading pair symbol (e.g., “BTC/USDT”)
§Example
let ticker = exchange.fetch_ticker("BTC/USDT").await?;
println!("Last price: {:?}", ticker.last);
Source

fn fetch_tickers<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, symbols: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = Result<Vec<Ticker>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Fetch tickers for multiple symbols.

Returns tickers for the specified symbols. If symbols is empty, returns tickers for all available symbols.

§Arguments
  • symbols - Slice of trading pair symbols
§Example
// Fetch specific symbols
let tickers = exchange.fetch_tickers(&["BTC/USDT", "ETH/USDT"]).await?;

// Fetch all tickers
let all_tickers = exchange.fetch_all_tickers().await?;
Source

fn fetch_order_book_with_params<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, params: OrderBookParams, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch order book with full parameters.

§Arguments
  • symbol - The trading pair symbol
  • params - Order book parameters including depth limit
Source

fn fetch_trades_with_limit<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, since: Option<i64>, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch recent trades with limit and optional timestamp filtering.

Returns recent trades for the specified symbol, optionally filtered by timestamp.

§Arguments
  • symbol - The trading pair symbol (e.g., “BTC/USDT”)
  • since - Optional start timestamp in milliseconds (i64) since Unix epoch
  • limit - Maximum number of trades to return
§Timestamp Format

The since parameter uses i64 milliseconds since Unix epoch:

  • 1609459200000 = January 1, 2021, 00:00:00 UTC
  • chrono::Utc::now().timestamp_millis() = Current time
  • chrono::Utc::now().timestamp_millis() - 3600000 = 1 hour ago
§Example
// Fetch recent trades without timestamp filter
let trades = exchange.fetch_trades_with_limit("BTC/USDT", None, Some(100)).await?;

// Fetch trades from the last hour
let since = chrono::Utc::now().timestamp_millis() - 3600000;
let trades = exchange.fetch_trades_with_limit("BTC/USDT", Some(since), Some(50)).await?;
Source

fn fetch_ohlcv_with_params<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, params: OhlcvParams, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch OHLCV with full parameters.

Allows specifying additional parameters like start time, limit, and price type (for futures).

§Arguments
  • symbol - The trading pair symbol (e.g., “BTC/USDT”)
  • params - OHLCV parameters including timeframe, since, limit, until
§Timestamp Format

All timestamp parameters in OhlcvParams use i64 milliseconds since Unix epoch:

  • since: Start time for historical data
  • until: End time for historical data
  • Returned Ohlcv structures have timestamp field as i64
§Example
use ccxt_core::types::params::OhlcvParams;

// Fetch recent candles
let candles = exchange.fetch_ohlcv_with_params(
    "BTC/USDT",
    OhlcvParams::new(Timeframe::H1).limit(100)
).await?;

// Fetch historical candles with timestamp range
let since = chrono::Utc::now().timestamp_millis() - (7 * 24 * 60 * 60 * 1000); // 7 days ago
let until = chrono::Utc::now().timestamp_millis();
let candles = exchange.fetch_ohlcv_with_params(
    "BTC/USDT",
    OhlcvParams::new(Timeframe::D1)
        .since(since)
        .until(until)
        .limit(7)
).await?;

Provided Methods§

Source

fn load_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Load and cache markets.

This method fetches markets if not already cached, or returns the cached markets. Use reload_markets() to force a refresh.

§Example
// First call loads from API
let markets = exchange.load_markets().await?;

// Subsequent calls use cache
let markets = exchange.load_markets().await?;
Source

fn reload_markets<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Arc<HashMap<String, Arc<Market>>>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Force reload markets from the API.

Clears the cache and fetches fresh market data.

Source

fn has_symbol<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a symbol exists and is active.

Returns true if the symbol exists in the market cache and is active.

Source

fn fetch_all_tickers<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ticker>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Fetch all tickers.

Convenience method that calls fetch_tickers with an empty slice.

Source

fn fetch_order_book<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch order book with default depth.

Returns the current order book (bids and asks) for the specified symbol.

§Arguments
  • symbol - The trading pair symbol
§Example
let orderbook = exchange.fetch_order_book("BTC/USDT").await?;
println!("Best bid: {:?}", orderbook.bids.first());
println!("Best ask: {:?}", orderbook.asks.first());
Source

fn fetch_order_book_with_depth<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, limit: u32, ) -> Pin<Box<dyn Future<Output = Result<OrderBook>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch order book with custom depth.

§Arguments
  • symbol - The trading pair symbol
  • limit - Maximum number of price levels to return
Source

fn fetch_trades<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch recent public trades.

Returns recent trades for the specified symbol.

§Arguments
  • symbol - The trading pair symbol
§Example
let trades = exchange.fetch_trades("BTC/USDT").await?;
for trade in trades {
    println!("{}: {} @ {}", trade.side, trade.amount, trade.price);
}
Source

fn fetch_trades_with_limit_only<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch recent trades with limit only.

§Arguments
  • symbol - The trading pair symbol
  • limit - Maximum number of trades to return
Source

fn fetch_ohlcv_u64<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, timeframe: Timeframe, since: Option<u64>, limit: Option<u32>, until: Option<u64>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

👎Deprecated since 0.1.0: Use fetch_ohlcv_with_params with i64 timestamps. Convert using TimestampUtils::u64_to_i64()

Fetch OHLCV candlestick data with u64 timestamps (deprecated).

DEPRECATED: Use fetch_ohlcv_with_params with i64 timestamps instead. This method is provided for backward compatibility during migration.

§Migration
// Old code (deprecated)
let candles = exchange.fetch_ohlcv_u64("BTC/USDT", Timeframe::H1, Some(1609459200000u64), Some(100), None).await?;

// New code (recommended)
let params = OhlcvParams::new(Timeframe::H1).since(1609459200000i64).limit(100);
let candles = exchange.fetch_ohlcv_with_params("BTC/USDT", params).await?;
Source

fn fetch_trades_u64<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, since: Option<u64>, limit: Option<u32>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Trade>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

👎Deprecated since 0.1.0: Use fetch_trades_with_limit with i64 timestamps. Convert using TimestampUtils::u64_to_i64()

Fetch recent trades with u64 timestamp filtering (deprecated).

DEPRECATED: Use fetch_trades_with_limit with i64 timestamps instead. This method is provided for backward compatibility during migration.

§Migration
// Old code (deprecated)
let trades = exchange.fetch_trades_u64("BTC/USDT", Some(1609459200000u64), Some(100)).await?;

// New code (recommended)
let trades = exchange.fetch_trades_with_limit("BTC/USDT", Some(1609459200000i64), Some(100)).await?;
Source

fn fetch_ohlcv<'life0, 'life1, 'async_trait>( &'life0 self, symbol: &'life1 str, timeframe: Timeframe, ) -> Pin<Box<dyn Future<Output = Result<Vec<Ohlcv>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch OHLCV candlestick data.

Returns candlestick data for the specified symbol and timeframe.

§Arguments
  • symbol - The trading pair symbol
  • timeframe - The candlestick timeframe
§Example
let candles = exchange.fetch_ohlcv("BTC/USDT", Timeframe::H1).await?;
for candle in candles {
    println!("O: {} H: {} L: {} C: {}",
        candle.open, candle.high, candle.low, candle.close);
}

Implementors§