nautechsystems/nautilus_trader · error · anyhow::Error

Invalid bar specification format: {}

Error message

Invalid bar specification format: {}

What it means

Each bar specification string must be in the three-part form "<step>-<aggregation>-<price_type>", e.g. "1-HOUR-LAST". request_bars splits on '-' and requires exactly 3 parts; anything else is rejected before parsing.

Source

Thrown at crates/adapters/interactive_brokers/src/historical/client.rs:330

        if all_contracts.is_empty() {
            anyhow::bail!("No valid contracts found after conversion");
        }

        let trading_hours = if use_rth {
            TradingHours::Regular
        } else {
            TradingHours::Extended
        };

        let mut all_bars = Vec::new();

        for contract in all_contracts {
            for bar_spec_str in &bar_specifications {
                // Parse bar spec (e.g., "1-HOUR-LAST")
                let parts: Vec<&str> = bar_spec_str.split('-').collect();
                if parts.len() != 3 {
                    anyhow::bail!("Invalid bar specification format: {}", bar_spec_str);
                }

                let step = parts[0].parse::<usize>()?;
                let aggregation = parts[1].to_lowercase();
                let price_type = parts[2].to_uppercase();

                let bar_spec = match aggregation.as_str() {
                    "second" => BarSpecification::new(
                        step,
                        BarAggregation::Second,
                        PriceType::from_str(&price_type).unwrap_or(PriceType::Last),
                    ),
                    "minute" => BarSpecification::new(
                        step,
                        BarAggregation::Minute,
                        PriceType::from_str(&price_type).unwrap_or(PriceType::Last),
                    ),
                    "hour" => BarSpecification::new(

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Write specs as exactly three hyphen-separated parts, e.g. "1-MINUTE-LAST", "5-HOUR-BID".
  2. Verify each entry has exactly two '-' characters and no empty segments.
  3. Fix the source config list producing malformed spec strings.

Example fix

// before
let specs = vec!["1-HOUR"];
// after
let specs = vec!["1-HOUR-LAST"];
Defensive patterns

Strategy: validation

Validate before calling

fn valid_bar_spec(s: &str) -> bool {
    s.split('-').filter(|p| !p.is_empty()).count() == 3
}
let specs: Vec<&str> = raw.into_iter().filter(|s| valid_bar_spec(s)).collect();

Prevention

When it happens

Trigger: Passing strings like "1-HOUR" (missing price type), "1HOUR-LAST" (no separators), "1-MINUTE-LAST-EXTRA", or an empty string in bar_specifications.

Common situations: Config files with specs written in shorthand ("1h", "5min"); copying IB's native bar size strings ("1 hour") instead of Nautilus bar spec format; trailing whitespace or stray hyphens.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/d232edf14d0ff3ea. Report an issue: GitHub.