Skip to main content

finance_query/domains/
commodities.rs

1//! Commodity price quote handle.
2//!
3//! Created via [`Providers::commodity`](crate::Providers::commodity).
4
5use crate::constants::{Interval, TimeRange};
6use crate::error::Result;
7use crate::models::chart::Chart;
8
9domain_handle! {
10    /// A commodity backed by configured data providers.
11    ///
12    /// Created via [`Providers::commodity`](crate::Providers::commodity).
13    pub struct Commodity { symbol, symbol }
14    cache: crate::models::commodities::CommodityQuote, chart
15}
16
17impl Commodity {
18    /// Fetch the current quote for this commodity.
19    pub async fn quote(&self) -> Result<crate::models::commodities::CommodityQuote> {
20        fetch_via!(
21            self,
22            symbol,
23            COMMODITIES,
24            as_commodities,
25            CommoditiesQuote,
26            fetch_commodities_quote,
27            crate::models::commodities::CommodityQuote
28        )
29    }
30
31    /// Fetch historical OHLCV candles for this commodity.
32    ///
33    /// The symbol is passed to the `CHART` route as-is. Note this expects the
34    /// route's chart-symbol form (e.g. the Yahoo futures symbol `GC=F` for
35    /// gold), which differs from the commodity *names* (`WHEAT`) some providers
36    /// use for [`quote`](Self::quote).
37    pub async fn chart(&self, interval: Interval, range: TimeRange) -> Result<Chart> {
38        fetch_chart_via!(self, self.symbol.to_string(), interval, range)
39    }
40
41    /// Fetch historical candles over `range` at a sensible default interval
42    /// ([`TimeRange::default_interval`]).
43    pub async fn history(&self, range: TimeRange) -> Result<Chart> {
44        self.chart(range.default_interval(), range).await
45    }
46}
47
48impl_chartable_analytics!(Commodity, crate::risk::TradingCalendar::Exchange);