Files
Muse 2819a316e4 Ind* indicators: OHLCV CSV loading for all 250 kinds
Reuse the stock-chart CSV parser (same format, same sample file):
IndicatorWidget::populate_from_csv / Widget::load_csv_data replace
the demo bars and the lines recompute from the loaded series at draw
time, so no per-kind wiring was needed. Adds csv::bars_from_rows +
date_to_epoch_nanos (Bar::time is nanoseconds; this also makes the
four pivot widgets compute real lines instead of all-NaN). Demo
series stays as fallback (reset_to_demo, loaded_from_csv flag).

Tests: 9 new (all-250 load + recompute + headless draw, pivot fix,
bad-CSV keeps demo bars); full workspace suite clean except the
pre-existing lightgui-gen fixture failure. Screenshots: 250/250
CSV-loaded renders under Xvfb differ from demo goldens (RMSE
0.0060-0.0661). Docs: docs/indicators-csv.md; rustdoc Ind* pages
updated.
2026-12-02 10:44:12 +00:00

8.4 KiB
Raw Permalink Blame History

Ind* indicator widgets: how series data gets in (Rust)

How the 250 Ind* widgets in lightgui_rust ingest data, what sample data ships with them (a built-in demo series — see below), and how to replace it with your own CSV. The reference implementation is crates/xframe-widgets/src/indicators.rs (IndicatorWidget) plus the shared CSV parser in crates/xframe-widgets/src/csv.rs (parse_ohlcv_csv, bars_from_rows, date_to_epoch_nanos). The CSV format is the same one the stock chart widgets use — see docs/stock-charts-csv.md for the full format reference.

The short version

use xframe_widgets::{create, Widget};

let mut ind = create("IndSMA").unwrap();
ind.set_bounds(40, 40, 720, 520);
let csv = std::fs::read_to_string(
    "crates/xframe-demo/examples/data/sample-ohlcv.csv",
).unwrap();
let rows = ind.load_csv_data(&csv).expect("sample CSV loads");
assert_eq!(rows, 30); // 30 bars; the SMA line recomputes from them

Or from the shell, with a live window under Xvfb:

cargo run -p xframe-demo -- IndSMA \
    --csv crates/xframe-demo/examples/data/sample-ohlcv.csv \
    --geometry 20,20,760,560 --hold 5

Audit: how data gets in

All 250 Ind* kinds are one archetype, IndicatorWidget (crates/xframe-widgets/src/archetypes.rs), wrapping the inner indicators::IndicatorWidget. There are not 250 structs: the kind string (e.g. "SMA") specialises the archetype, and the fdi definition for that kind (xframe_indicators::find_def) supplies the computation.

Why no per-kind wiring was needed

Each indicator recomputes its lines from its current bars on every draw (IndicatorWidget::lines() calls ind.compute(&self.bars)), so loading a series is one generic operation: replace self.bars and the next draw plots the new lines. The CSV job therefore adds a single loader on the archetype instead of 250 setters. The 50 kinds with no fdi def are unaffected in the same way: they draw from the same bars (grid + view, or candles), so a loaded series changes what they draw too — just with no indicator lines on top.

The 250 kinds, by what they plot

Group Count What a loaded series changes
Pane defs 128 The indicator's own lines/bars in its pane (own y-scale, candles hidden)
Overlay defs 69 Indicator lines drawn over the loaded price candles
Markers defs 3 Marker triangles at the computed points, over the candles
No fdi def 50 No lines — 38 draw grid + view only, 12 draw the loaded candles (NO_DEF_CANDLES)

Line style is Lines for 193 def kinds and Bars for 7. Thirty defs are shared by more than one widget (e.g. RSI backs six widgets: IndRSI, IndAdaptiveRSI, IndCutlerRSI, IndEhlersSmoothedRSI, IndMACDofRSI, IndRSX), so the same computation with different defaults/line selections appears under several names.

Data model

IndicatorWidget holds bars: Vec<xframe_indicators::Bar> — one {open, high, low, close, volume, time} per row — plus a PlotView (first/count window, auto-y, wheel zoom, drag pan, crosshair) and the per-widget candle palette surveyed from the C++ headers. Bar::time is nanoseconds since the epoch (the C++ long long).

The API

Method What it does
populate_from_csv(text) / load_csv_data(text) Parse OHLCV CSV (csv::parse_ohlcv_csv format) and recompute from the loaded bars. Returns the number of rows loaded. A failed load leaves the previous bars untouched. The view resets to the first bar.
set_bars(bars) Replace the series directly with Vec<Bar> (clears the loaded-from-CSV flag).
reset_to_demo() Drop loaded bars and restore the built-in demo series.
bars() The bars currently computed from.
loaded_from_csv() Whether the current bars came from a CSV load.
computed_lines() The indicator lines for the current bars — the same values draw plots.
set_param(i, v) / get_param(i) Indicator parameters by index (def kinds only), e.g. the SMA period. Changing a parameter recomputes the lines on the next draw, from whichever bars are loaded.

The generic entry point is Widget::load_csv_data (overridden on the IndicatorWidget archetype to call populate_from_csv), so the demo binary's --csv flag and any embedder that talks to widgets through the trait get CSV loading for indicators with no extra code.

Template data: the demo series

A fresh indicator renders without any loading: it starts on the built-in deterministic demo series, demo_bars(n) — a port of C++ xframe::make_seeded_series(n, 42) (widgets/components/StockData.h, lightgui eb0b5fc), dated 2026-01-01 + i days. 85 widgets use 120 bars, the rest 250 (BARS_120, surveyed from the C++ mains). The demo series is the fallback, not the data: loaded_from_csv() is false until a CSV load succeeds, and reset_to_demo() restores it.

CSV format

Exactly the stock-chart format (csv::parse_ohlcv_csv — same parser, same rules, no second format):

date,open,high,low,close,volume
2026-01-05,100.00,101.20,99.10,100.80,850000

One bar per line, comma-separated, in that column order. A header row is optional (the first line is skipped when its second field is not a number). volume is optional and defaults to 0.0. Blank lines and lines starting with # are skipped. date must look like a date: YYYY-MM-DD, YYYY/MM/DD, YYYYMMDD, epoch seconds (all digits, 9+), or a date followed by a time (YYYY-MM-DD HH:MM:SS). A bad row is an error naming its line number, and the whole load fails — the previous bars stay in place.

Dates become Bar::time in nanoseconds via csv::date_to_epoch_nanos (a bare date means midnight UTC; a trailing time is added). The repo's sample file is crates/xframe-demo/examples/data/sample-ohlcv.csv (30 weekday bars, 2026-01-05..2026-02-13).

The pivot quirk (why loaded data fixes pivot points)

Bar::time is documented as nanoseconds, but the demo series stores seconds in it (demo_bars passes 1767225600 + i * 86400). The pivot-point defs group bars into days with time / 86_400_000_000_000, so every demo bar lands on day 0 and the four pivot widgets (IndStandardPivotPoints, IndCamarillaPivotPoints, IndFibonacciPivotPoints, IndWoodiePivotPoints) draw all-NaN lines from demo data. CSV-loaded bars carry real nanosecond dates, so the pivots compute — loading data is the fix, not a workaround. demo_bars itself is untouched: changing it would move the demo screenshots for all 250 widgets.

How much data is enough

Indicator warmups vary: a 20-period SMA needs 20 bars, a 52-bar span needs 52. Measured with the sample file and a 250-row CSV built from the demo series:

  • 30 rows (sample file): 189 of the 200 def kinds produce at least one finite line value; the other 11 need longer warmups.
  • 250 rows: all 200 def kinds produce finite values.
  • The 50 no-def kinds never produce lines (by design — they have no def to compute), at any length.

A short file is not an error: the widget loads, draws what it can, and the lines fill in as more bars arrive. If you need a specific indicator's full warmup, load at least its longest period (e.g. 52 bars for the Ichimoku spans).

Verification

  • Unit/parser tests in crates/xframe-widgets/src/csv.rs (date_to_epoch_nanos known dates incl. leap years, bars_from_rows mapping) and src/indicators.rs (sample load recomputes lines, bad CSV keeps demo bars, all-250 load + recompute + headless draw, pivots go from all-NaN to finite, reset-to-demo, Widget trait path): 16 tests in xframe-widgets --lib, all passing.
  • Screenshots under Xvfb (xframe-demo --csv, 800x600, settle-capture as in tools/visreg/run.sh): 20 kinds sampled across Overlay / Pane / Markers / no-def / pivots — every CSV shot differs from its demo-data shot (normalised RMSE 0.021–0.065, vs the 0.005 visreg threshold), proving loaded data changes the plotted output. Full 250-kind CSV sweep under Xvfb: all 250 kinds render with the sample loaded, all 800x600, and every CSV shot differs from its demo-data golden (normalised RMSE min 0.0060, median 0.0345, max 0.0661 — none below the 0.005 visreg threshold; the smallest movers are the no-def pane kinds, whose grid scale alone shifts, and the largest are the pivot points, which go from blank to drawn).
  • Full workspace suite: no regressions (the only failures are pre-existing: lightgui-gen fixtures on pristine HEAD, and lightgui-support font/theme suites, which need a display and pass under Xvfb).