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 fetch_commodities_quote,
25 crate::models::commodities::CommodityQuote
26 )
27 }
28
29 /// Fetch historical OHLCV candles for this commodity.
30 ///
31 /// The symbol is passed to the `CHART` route as-is. Note this expects the
32 /// route's chart-symbol form (e.g. the Yahoo futures symbol `GC=F` for
33 /// gold), which differs from the commodity *names* (`WHEAT`) some providers
34 /// use for [`quote`](Self::quote).
35 pub async fn chart(&self, interval: Interval, range: TimeRange) -> Result<Chart> {
36 fetch_chart_via!(self, self.symbol.to_string(), interval, range)
37 }
38
39 /// Fetch historical candles over `range` at a sensible default interval
40 /// ([`TimeRange::default_interval`]).
41 pub async fn history(&self, range: TimeRange) -> Result<Chart> {
42 self.chart(range.default_interval(), range).await
43 }
44}
45
46impl_chartable_analytics!(Commodity, crate::risk::TradingCalendar::Exchange);