Skip to main content

finance_query/domains/
indices.rs

1//! Stock market index quote handle.
2//!
3//! Created via [`Providers::index`](crate::Providers::index).
4
5use crate::constants::{Interval, TimeRange};
6use crate::error::Result;
7use crate::models::chart::Chart;
8
9domain_handle! {
10    /// A stock market index backed by configured data providers.
11    ///
12    /// Created via [`Providers::index`](crate::Providers::index).
13    pub struct Index { symbol, symbol }
14    cache: crate::models::indices::IndexQuote, chart
15}
16
17impl Index {
18    /// Fetch the current quote for this index.
19    pub async fn quote(&self) -> Result<crate::models::indices::IndexQuote> {
20        fetch_via!(
21            self,
22            symbol,
23            INDICES,
24            fetch_indices_quote,
25            crate::models::indices::IndexQuote
26        )
27    }
28
29    /// Fetch historical OHLCV candles for this index.
30    ///
31    /// The symbol is passed to the `CHART` route as-is, so it should be in the
32    /// form the route expects (e.g. Yahoo index symbols like `^GSPC`).
33    pub async fn chart(&self, interval: Interval, range: TimeRange) -> Result<Chart> {
34        fetch_chart_via!(self, self.symbol.to_string(), interval, range)
35    }
36
37    /// Fetch historical candles over `range` at a sensible default interval
38    /// ([`TimeRange::default_interval`]).
39    pub async fn history(&self, range: TimeRange) -> Result<Chart> {
40        self.chart(range.default_interval(), range).await
41    }
42}
43
44impl_chartable_analytics!(Index, crate::risk::TradingCalendar::Exchange);