nautechsystems/nautilus_trader · error

invalid candle close: {e}

Error message

invalid candle close: {e}

What it means

parse_ws_bar converts the candle's close decimal into a Price at the instrument's price_precision via Price::from_decimal_dp. Failure is wrapped as 'invalid candle close', indicating the close value cannot be represented at the configured precision.

Source

Thrown at crates/adapters/lighter/src/websocket/parse.rs:406

/// Returns an error if the OHLCV decimals overflow the instrument's precision, or if the
/// timestamp cannot be converted.
pub fn parse_ws_bar(
    instrument: &InstrumentAny,
    candle: &LighterWsCandle,
    resolution: LighterCandleResolution,
    ts_init: UnixNanos,
) -> anyhow::Result<Bar> {
    let price_precision = instrument.price_precision();
    let size_precision = instrument.size_precision();

    let open = Price::from_decimal_dp(candle.o, price_precision)
        .map_err(|e| anyhow::anyhow!("invalid candle open: {e}"))?;
    let high = Price::from_decimal_dp(candle.h, price_precision)
        .map_err(|e| anyhow::anyhow!("invalid candle high: {e}"))?;
    let low = Price::from_decimal_dp(candle.l, price_precision)
        .map_err(|e| anyhow::anyhow!("invalid candle low: {e}"))?;
    let close = Price::from_decimal_dp(candle.c, price_precision)
        .map_err(|e| anyhow::anyhow!("invalid candle close: {e}"))?;
    let volume = Quantity::from_decimal_dp(candle.v, size_precision)
        .map_err(|e| anyhow::anyhow!("invalid candle volume: {e}"))?;

    let t_ms = u64::try_from(candle.t)
        .map_err(|_| anyhow::anyhow!("negative candle timestamp: {}", candle.t))?;
    let ts_event = parse_millis_to_nanos(t_ms)?;

    let bar_type = BarType::new(
        instrument.id(),
        resolution.to_bar_spec(),
        AggregationSource::External,
    );

    Bar::new_checked(bar_type, open, high, low, close, volume, ts_event, ts_init)
        .map_err(|e| anyhow::anyhow!("invalid candle bar: {e}"))
}

fn parse_book_level_delta(

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Set price_precision to match the venue's actual precision
  2. Quantize candle.c to price_precision before conversion
  3. Log candle.c with the underlying error to diagnose
  4. Confirm correct instrument/market mapping

Example fix

// before
let close = Price::from_decimal_dp(candle.c, price_precision)?;
// after
let close = Price::from_decimal_dp(candle.c.round_dp(price_precision as u32), price_precision)?;
Defensive patterns

Strategy: validation

Validate before calling

let close = candle.c.round_dp(instrument.price_precision() as u32);

Try / catch

match parse_ws_bar(candle, instrument, ...) {
    Err(e) if e.to_string().contains("invalid candle close") => {
        log::warn!("bad candle close {}, skipping bar: {e}", candle.c);
    }
    other => other?,
}

Prevention

When it happens

Trigger: Candle close decimal with more fractional digits than price_precision or otherwise failing Price::from_decimal_dp in the WebSocket candle message.

Common situations: Instrument price_precision misconfigured; venue emitting extra decimals; wrong instrument for the candle subscription.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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