Skip to main content

AsyncDataProvider

Trait AsyncDataProvider 

Source
pub trait AsyncDataProvider: Send + Sync {
    // Required methods
    fn load<'a>(
        &'a self,
        query: &'a DataQuery,
    ) -> Pin<Box<dyn Future<Output = Result<DataFrame, AsyncDataError>> + Send + 'a>>;
    fn has_data(&self, symbol: &str, timeframe: &Timeframe) -> bool;
    fn symbols(&self) -> Vec<String>;

    // Provided methods
    fn timeframes(&self, symbol: &str) -> Vec<Timeframe> { ... }
    fn subscribe(
        &self,
        symbol: &str,
        timeframe: &Timeframe,
    ) -> Result<Receiver<DataFrame>, AsyncDataError> { ... }
    fn unsubscribe(
        &self,
        symbol: &str,
        timeframe: &Timeframe,
    ) -> Result<(), AsyncDataError> { ... }
}
Expand description

Async data provider trait for historical and live data

This trait provides an async interface for loading data. Implementations can support both historical data fetching and live data streaming.

§Object Safety

Uses Pin<Box<dyn Future>> instead of async fn to maintain object safety and allow dyn AsyncDataProvider trait objects.

§Example

use shape_core::data::{AsyncDataProvider, DataQuery, Timeframe};

async fn load_data(provider: &dyn AsyncDataProvider) {
    let query = DataQuery::new("AAPL", Timeframe::d1());
    let df = provider.load(&query).await.unwrap();
    println!("Loaded {} rows", df.row_count());
}

Required Methods§

Source

fn load<'a>( &'a self, query: &'a DataQuery, ) -> Pin<Box<dyn Future<Output = Result<DataFrame, AsyncDataError>> + Send + 'a>>

Load historical data matching the query

This method is async and can load data concurrently. The returned DataFrame contains all requested data in columnar format.

§Arguments
  • query - Specifies symbol, timeframe, range, and limits
§Returns

A DataFrame with the requested data, or an error if unavailable.

Source

fn has_data(&self, symbol: &str, timeframe: &Timeframe) -> bool

Check if data is available for a symbol/timeframe combination

This is a sync metadata query - doesn’t load actual data.

Source

fn symbols(&self) -> Vec<String>

List available symbols

This is a sync metadata query.

Provided Methods§

Source

fn timeframes(&self, symbol: &str) -> Vec<Timeframe>

List available timeframes for a symbol

Default implementation returns empty. Providers should override if they can provide this information.

Source

fn subscribe( &self, symbol: &str, timeframe: &Timeframe, ) -> Result<Receiver<DataFrame>, AsyncDataError>

Subscribe to live bar updates

Returns a channel receiver that yields new bars as they complete. Default implementation returns an error (live data not supported).

§Arguments
  • symbol - Symbol to subscribe to
  • timeframe - Timeframe for bars
§Returns

A receiver for DataFrames containing new bars, or an error.

§Example
let mut rx = provider.subscribe("AAPL", &Timeframe::m1())?;
while let Some(df) = rx.recv().await {
    println!("New bar: {}", df.row_count());
}
Source

fn unsubscribe( &self, symbol: &str, timeframe: &Timeframe, ) -> Result<(), AsyncDataError>

Unsubscribe from live updates

Default implementation is a no-op. Providers should override if they manage subscription state.

Implementors§