Skip to main content

Module history

Module history 

Source
Expand description

Reading a long candle history in windows that the broker accepts, within the rate limit.

One candles request covers a limited span (BCS: 1440 bars, T-Bank: from a day for minute bars to ten years for monthly ones). A year of hourly bars is therefore several requests, and each one spends quota. CandlePages cuts the range into windows, reads them one after another, waits out [Error::RateLimited], repeats a read that failed on the network, joins the edges of neighbouring windows without duplicates and always knows where to start again (resume_from).

Only reads are repeated here. A candle read changes nothing at the broker, so a repeat is safe. Commands are never repeated anywhere in the library.

The helper is written against [MarketDataSource], so it works with every connector. The span of a window comes from [MarketDataSource::candles_max_span] unless HistoryOpts::window says otherwise.

§Examples

let year = TimeRange::new(
    UnixNanos::from_nanos(1_767_225_600_000_000_000), // 2026-01-01
    UnixNanos::from_nanos(1_798_761_600_000_000_000), // 2027-01-01
);
let mut pages = CandlePages::new(md, sber, Timeframe::H1, year, HistoryOpts::default());
while let Some(page) = pages.next_page().await? {
    // Write the page to disk here. After a crash start again from `pages.resume_from()`.
    println!("{} bars, next window starts at {:?}", page.len(), pages.resume_from());
}

Structs§

CandlePages
A reader of candle history, one window per next_page call.
HistoryError
A history read stopped. Carries the error and the place to start from again.
HistoryOpts
How CandlePages cuts the range and how patient it is.

Functions§

load_candles
Reads the whole range into memory: CandlePages until the end.