Economic Indicators#
abstract · Cargo Docs
info · Feature flag required
The economic domain requires at least one provider feature. FRED is the primary recommended provider:
finance-query = { version = "...", features = ["fred"] }The EconomicIndicator domain handle fetches macro-economic time series data. It is backed by FRED (Federal Reserve Economic Data), with optional Alpha Vantage and Polygon fallbacks, and is keyed by a series ID string.
Getting a Handle#
Create an EconomicIndicator handle from a Providers instance by routing the ECONOMIC capability to FRED, then call .series() to fetch the data:
use finance_query::{Capability, Provider, Providers};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let providers = Providers::builder()
.route(Capability::ECONOMIC, [Provider::Fred])
.build()
.await?;
let gdp = providers.economic("GDP");
let series = gdp.series().await?;
println!("{}: {} observations", series.series_id, series.observations.len());
Ok(())
}
The series ID is a FRED series identifier such as "GDP", "FEDFUNDS", or "CPIAUCSL". See the FRED series catalog for the full list of 800k+ available series.
note · FRED API Key
FRED requires an API key. Initialise it once before building your Providers instance:
use finance_query::fred;
fn main() -> Result<(), Box<dyn std::error::Error>> {
fred::init(std::env::var("FRED_API_KEY")?)?;
Ok(())
}
See FRED Provider Reference for setup details.
- Deserializing a full FRED series response (
fred::series→MacroSeries) completes in well under half a millisecond.
EconomicSeries Fields#
The returned EconomicSeries value contains:
| Field | Type | Description |
|---|---|---|
series_id | String | Series identifier (e.g., "GDP", "FEDFUNDS") |
title | Option<String> | Human-readable series title |
units | Option<String> | Unit of measurement (e.g., "Billions of Dollars", "Percent") |
frequency | Option<String> | Reporting frequency (e.g., "Annual", "Monthly") |
observations | Vec<MacroObservation> | Chronologically ordered observations |
Each MacroObservation has:
| Field | Type | Description |
|---|---|---|
date | String | Observation date as YYYY-MM-DD |
value | Option<f64> | Observation value; None when FRED reports a missing value |
Both tables are backed by a compiled, real value — EconomicSeries and
MacroObservation are #[non_exhaustive] and have no public constructor, but
both derive Deserialize, so a fixture value can be built from outside the
crate with serde_json::from_value:
use finance_query::EconomicSeries;
use finance_query::fred::MacroObservation;
fn main() {
let series: EconomicSeries = serde_json::from_value(serde_json::json!({
"series_id": "GDP",
"title": "Gross Domestic Product",
"units": "Billions of Dollars",
"frequency": "Quarterly",
"observations": [
{"date": "2024-01-01", "value": 28624.1},
{"date": "2024-04-01", "value": 29016.7}
]
}))
.unwrap();
let first: &MacroObservation = &series.observations[0];
println!("series_id = {}", series.series_id);
println!("title = {:?}", series.title);
println!("first observation = {} -> {:?}", first.date, first.value);
}
recorded outputcargo soothfast docs capture
series_id = GDP
title = Some("Gross Domestic Product")
first observation = 2024-01-01 -> Some(28624.1)Example: Inspecting GDP Data#
use finance_query::{Capability, Provider, Providers};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let providers = Providers::builder()
.route(Capability::ECONOMIC, [Provider::Fred])
.build()
.await?;
let gdp = providers.economic("GDP");
let series = gdp.series().await?;
println!("Series: {}", series.series_id);
if let Some(title) = &series.title {
println!("Title: {}", title);
}
if let Some(units) = &series.units {
println!("Units: {}", units);
}
if let Some(freq) = &series.frequency {
println!("Frequency: {}", freq);
}
println!("Observations: {}", series.observations.len());
// Print the last 5 observations
for obs in series.observations.iter().rev().take(5) {
match obs.value {
Some(v) => println!("{}: {:.1}", obs.date, v),
None => println!("{}: N/A", obs.date),
}
}
Ok(())
}
See Also#
- FRED Provider Reference — low-level FRED API (
fred::series,fred::treasury_yields, initialisation) - Getting Started — building a
Providersinstance and routing capabilities
