Ind* indicators: CSV/series support on all 250 kinds

Transpile the C component ind_* function for every kind into
lightgui/xframe/indicators.py (tools/transpile_indicators.py), verified
250/250 against the C++ reference dump (<=1e-9, NaN patterns included).
IndicatorWidget gains the StockWidget series API (set_series, populate,
get_series, add_point, clear, load_csv, item_count, get_lines) and draws
the computed lines (14-colour palette, NaN gaps, dots for isolated
marker points) with the demo series as fallback. Docs in
docs/indicators-data.md; tests/test_indicators_csv.py (17 tests, golden
spot values vs the C++ reference); Xvfb shots via
tools/ind_csv_shots.py.
This commit is contained in:
cnmcdee committed 2026-12-02 11:04:35 +00:00
1 parent d0f430f88c
commit 3b67cbf9de
6 files changed
+7447 -1

No files matched your search

+215 -1
View File
@@ -2020,10 +2020,223 @@ class ChartWidget(Widget):
class IndicatorWidget(Widget):
"""One of the 250 Ind* indicators (archetype Indicator).
Draws the indicator's computed lines from OHLCV bars. The bars
come from the widget's own series when set (``set_series`` /
``load_csv`` / ``populate``), else from the built-in deterministic
demo series -- the same fallback the StockWidget chart widgets
use. The series is runtime state only, never saved in .fdj.
The lines themselves are computed by
``lightgui.xframe.indicators.compute_lines`` (transpiled from the
C component ``ind_*`` functions; see tools/transpile_indicators.py
and lightgui/xframe/indicators.py), so every kind recomputes from
the loaded data instead of the built-in demo lines.
"""
ARCHETYPE = "Indicator"
# Plot palette: the 14 line colours from the Rust
# xframe-indicators plot.rs PLOT_PALETTE, in order. Line j of a
# kind uses _PLOT_PALETTE[j % 14].
_PLOT_PALETTE = (
(0.13, 0.35, 0.85), # blue
(0.95, 0.55, 0.10), # orange
(0.55, 0.25, 0.80), # purple
(0.00, 0.60, 0.60), # teal
(0.85, 0.20, 0.55), # magenta
(0.55, 0.35, 0.15), # brown
(0.20, 0.65, 0.25), # green
(0.80, 0.15, 0.15), # red
(0.10, 0.55, 0.80), # steel blue
(0.65, 0.65, 0.10), # olive
(0.35, 0.35, 0.35), # grey
(0.90, 0.70, 0.10), # gold
(0.25, 0.25, 0.70), # indigo
(0.60, 0.20, 0.20), # brick
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Own series: None means "use the built-in demo series";
# a list (possibly empty) is data the user fed in.
self._series = None
# --- data ingestion (mirrors StockWidget, single slot) --------------
def set_series(self, bars):
"""Replace the series this indicator computes from.
*bars* is a list of (open, high, low, close, volume) tuples
(or dicts with those keys), or None to revert to the built-in
demo series. The indicator recomputes its lines from the new
data on the next draw. Marks the widget dirty so the host
redraws.
"""
self._series = _stock_mod._validate_bars(bars)
self._mark_dirty()
def populate(self, bars):
"""Alias for ``set_series``: fill the indicator with *bars*.
The clear/populate pair: ``widget.clear()`` empties the
series, ``widget.populate(bars)`` (or ``set_series``) refills
it.
"""
self.set_series(bars)
def get_series(self):
"""The bars this indicator currently plots: the widget's own
series when set, else the built-in demo series. Returns a
copy, so callers cannot mutate the plotted data.
"""
return list(_ind_mod._bars(self))
def add_point(self, bar):
"""Append one (open, high, low, close, volume) bar to the
series and return the new bar count.
If no own series was set yet, this starts a fresh series
(it does NOT extend the demo series).
"""
bar = _stock_mod._validate_bars([bar], what="add_point")[0]
if self._series is None:
self._series = []
self._series.append(bar)
self._mark_dirty()
return len(self._series)
def clear(self):
"""Empty the series. A cleared widget draws the no-data
placeholder until ``populate`` / ``set_series`` refills it
(or ``set_series(None)`` restores the demo series).
"""
self._series = []
self._mark_dirty()
def load_csv(self, source):
"""Load OHLCV bars from a CSV file into this indicator.
*source* is a path or a file-like object; see
``lightgui.xframe.stock.load_csv`` for the format (the same
``date,open,high,low,close,volume`` spec the chart widgets
use). Returns the number of bars loaded. Raises ValueError
naming the offending row on bad data, and leaves the previous
series untouched in that case.
"""
bars = _stock_mod.load_csv(source)
self.set_series(bars)
return len(bars)
def item_count(self):
"""Number of bars this indicator currently plots."""
return len(_ind_mod._bars(self))
def get_lines(self, params=None):
"""The plotted lines for the current series, as a list of
float lists (one per bar, NaN where the indicator has no
value yet), in plot order. *params* overrides the kind's
default parameters positionally. An empty list means the
series was cleared; a kind this widget does not know returns
the panel fallback (no lines).
"""
kind = self._ind_kind()
if kind is None:
return []
bars = _ind_mod._bars(self)
if not bars:
return []
return _ind_mod.compute_lines(kind, bars, params)
def _ind_kind(self):
"""Registry name (``IndRSI``) for this widget's kind, or None
when the kind is not one of the 250 indicators."""
kind = self.kind
if kind in _ind_mod.INDICATOR_INFO:
return kind
if ("Ind" + kind) in _ind_mod.INDICATOR_INFO:
return "Ind" + kind
return None
def _draw(self, cr):
_draw_panel(cr, self)
# A cleared series draws the no-data placeholder instead of
# lines: compute_lines would index into an empty series.
if self._series is not None and len(self._series) == 0:
_stock_mod._no_data(cr, self)
return
kind = self._ind_kind()
if kind is None:
_draw_panel(cr, self)
return
bars = _ind_mod._bars(self)
lines = _ind_mod.compute_lines(kind, bars)
_bg(cr, self, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
_wtext(cr, self, self.pkind, self.width / 2, 16, size=12,
align="center", rgb=(0.4, 0.4, 0.4),
max_width=self.width - 12)
# Plot area below the header.
px, py = 6, 24
pw, ph = max(1, self.width - 12), max(1, self.height - 30)
n = len(bars)
if n == 0 or ph <= 0:
return
lo, hi = None, None
for values in lines:
for v in values:
if v is None or v != v:
continue
lo = v if lo is None else min(lo, v)
hi = v if hi is None else max(hi, v)
if lo is None:
return
if hi <= lo:
hi = lo + 1.0
pad = (hi - lo) * 0.05 or 0.5
lo -= pad
hi += pad
def _x(i):
return px + (i + 0.5) * pw / n
def _y(v):
return py + ph - (v - lo) / (hi - lo) * ph
for j, values in enumerate(lines):
colour = self._PLOT_PALETTE[j % len(self._PLOT_PALETTE)]
cr.save()
_rgb(cr, colour)
cr.set_line_width(2.0)
started = False
for i in range(min(n, len(values))):
v = values[i]
if v is None or v != v:
started = False
continue
if not started:
cr.move_to(_x(i), _y(v))
started = True
else:
cr.line_to(_x(i), _y(v))
cr.stroke()
cr.restore()
# Isolated finite points (pattern/marker kinds such as
# FractalUp/Down and ZigZag, whose values are single-bar
# pivots) would otherwise draw nothing: a move_to with no
# line_to leaves no mark. Draw them as filled dots.
cr.save()
_rgb(cr, colour)
for i in range(min(n, len(values))):
v = values[i]
if v is None or v != v:
continue
prev_nan = i == 0 or values[i - 1] is None \
or values[i - 1] != values[i - 1]
next_nan = i + 1 >= len(values) or values[i + 1] is None \
or values[i + 1] != values[i + 1]
if prev_nan and next_nan:
cr.arc(_x(i), _y(v), 3, 0, 2 * math.pi)
cr.fill()
cr.restore()
class InstrumentWidget(Widget):
@@ -2428,6 +2641,7 @@ def is_known_kind(kind):
_KIND_CLASSES = {}
from lightgui.xframe import stock as _stock_mod
from lightgui.xframe import indicators as _ind_mod
from lightgui.xframe import instruments as _inst_mod
# Keyed by registry NAME (see lookup()): the Stock* names draw in
# StockWidget, the Inst* names in InstWidget. Everything else keeps
File diff suppressed because it is too large. Load diff