"""Ind* indicator data ingestion: CSV loading + set_series/clear/populate. - ``IndicatorWidget.load_csv`` / ``set_series`` / ``get_series`` / ``add_point`` / ``clear`` / ``populate`` / ``item_count``: the same series API (and the same ``date,open,high,low,close,volume`` CSV spec) the StockWidget chart widgets got, but for the 250 Ind* indicators. - ``compute_lines``: every one of the 250 kinds recomputes its lines from loaded OHLCV data without crashing (demo series and the 30-bar CSV fixture), each line is one value per bar, and the loaded-data lines differ from the demo lines. Spot values are golden-checked against the C++ component reference (``ind_*`` functions on the seeded demo series, seed 42); the full 250-kind cross-check lives in tools/transpile_indicators.py's verification pass. - Rendering: every kind draws CSV data headless without raising, the CSV render differs from the demo render, a one-bar series draws, and a cleared series draws the no-data placeholder. """ import io import math import os import unittest import cairo from lightgui.xframe import core from lightgui.xframe import indicators as ind_mod from lightgui.xframe import stock as stock_mod REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CSV_PATH = os.path.join(REPO, "tests", "data", "ohlcv_30.csv") KINDS = ind_mod.indicator_names() # Golden spot values from the C++ component reference (ind_* on the # seeded demo series, seed 42): (kind, line, bar, value). GOLDEN = ( ("IndRSI", 0, 25, 46.880370427479114), ("IndMACD", 0, 40, -0.7883630259220524), ("IndMACD", 1, 40, -0.7673869019079353), ("IndMACD", 2, 40, -0.02097612401411708), ("IndBollingerBands", 0, 40, 100.71938861058676), ("IndBollingerBands", 1, 40, 97.25597274343893), ("IndBollingerBands", 2, 40, 93.7925568762911), ("IndSMA", 0, 30, 98.70914778880885), ("IndEMA", 0, 30, 98.15064907635568), ("IndATR", 0, 30, 1.4632890946762565), ) def _make(name, width=400, height=300): widget = core.create(name, name + "_001", 0, 0, width, height) assert widget is not None, name return widget def _draw(widget): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, widget.width + 4, widget.height + 4) cr = cairo.Context(surface) cr.set_source_rgb(1, 1, 1) cr.paint() widget.draw(cr) return surface def _png_bytes(surface): buf = io.BytesIO() surface.write_to_png(buf) return buf.getvalue() def _finite(values): return [v for v in values if v is not None and v == v] class TestIndicatorSeriesApi(unittest.TestCase): def test_kinds(self): self.assertEqual(len(KINDS), 250) self.assertEqual(len(set(KINDS)), 250) def test_demo_fallback(self): w = _make("IndRSI") self.assertEqual(w.item_count(), 250) self.assertEqual(len(w.get_series()), 250) lines = w.get_lines() self.assertEqual(len(lines), 1) self.assertEqual(len(lines[0]), 250) self.assertTrue(_finite(lines[0])) def test_set_series_round_trip(self): w = _make("IndRSI") bars = stock_mod.load_csv(CSV_PATH) w.set_series(bars) self.assertEqual(w.item_count(), 30) self.assertEqual(w.get_series(), bars) # get_series returns a copy: mutating it must not move the plot got = w.get_series() got.append(got[-1]) self.assertEqual(w.item_count(), 30) def test_set_series_none_restores_demo(self): w = _make("IndRSI") w.set_series(stock_mod.load_csv(CSV_PATH)) self.assertEqual(w.item_count(), 30) w.set_series(None) self.assertEqual(w.item_count(), 250) def test_add_point(self): w = _make("IndSMA") w.set_series(stock_mod.load_csv(CSV_PATH)) self.assertEqual(w.add_point((100.0, 101.0, 99.0, 100.5, 1000.0)), 31) self.assertEqual(w.item_count(), 31) # No own series yet: starts fresh, does not extend the demo. w2 = _make("IndSMA") self.assertEqual(w2.add_point((100.0, 101.0, 99.0, 100.5, 1000.0)), 1) self.assertEqual(w2.item_count(), 1) def test_clear_populate(self): w = _make("IndEMA") w.set_series(stock_mod.load_csv(CSV_PATH)) w.clear() self.assertEqual(w.item_count(), 0) self.assertEqual(w.get_lines(), []) w.populate(stock_mod.load_csv(CSV_PATH)) self.assertEqual(w.item_count(), 30) self.assertTrue(_finite(w.get_lines()[0])) def test_load_csv(self): w = _make("IndMACD") self.assertEqual(w.load_csv(CSV_PATH), 30) self.assertEqual(w.item_count(), 30) self.assertEqual(len(w.get_lines()), 3) def test_load_csv_bad_data_leaves_series(self): w = _make("IndRSI") before = w.get_series() with self.assertRaises(ValueError): w.load_csv(io.StringIO("date,open,high,low,close,volume\n" "2026-01-01,1,2,3,xx,5\n")) self.assertEqual(w.get_series(), before) def test_set_series_rejects_bad_bars(self): w = _make("IndRSI") with self.assertRaises(ValueError): w.set_series([(1.0, 2.0, 3.0)]) # not a 5-tuple bar def test_golden_values(self): bars = stock_mod.demo_series() for kind, line, bar, value in GOLDEN: got = ind_mod.compute_lines(kind, bars)[line][bar] self.assertAlmostEqual(got, value, places=9, msg="%s line %d bar %d" % (kind, line, bar)) def test_params_override(self): bars = stock_mod.demo_series() default = ind_mod.compute_lines("IndRSI", bars)[0] other = ind_mod.compute_lines("IndRSI", bars, [7])[0] self.assertNotEqual(default, other) class TestAllKindsRecompute(unittest.TestCase): def test_all_kinds_load_and_recompute(self): demo = stock_mod.demo_series() csv_bars = stock_mod.load_csv(CSV_PATH) no_finite_csv = [] for kind in KINDS: demo_lines = ind_mod.compute_lines(kind, demo) self.assertTrue(demo_lines, kind) for values in demo_lines: self.assertEqual(len(values), 250, kind) csv_lines = ind_mod.compute_lines(kind, csv_bars) self.assertEqual(len(csv_lines), len(demo_lines), kind) for values in csv_lines: self.assertEqual(len(values), 30, kind) # Loaded data must change the output (where the kind has # any finite value on the short CSV at all). if any(_finite(v) for v in csv_lines): self.assertNotEqual( [v for line in csv_lines for v in line], [v for line in demo_lines[:len(csv_lines)] for v in line[:30]], kind) else: no_finite_csv.append(kind) # The kinds with no finite value on the 30-bar CSV are the # long-warm-up family (multi-stage smoothing, 52-bar Senkou # Span B, Klinger slow EMA 55, Guppy long leg 60, ...). Each # of them must still produce finite values on the 250-bar # demo series -- otherwise it would be a broken kind, not a # warm-up. self.assertLessEqual(len(no_finite_csv), 40, sorted(no_finite_csv)) for kind in no_finite_csv: demo_lines = ind_mod.compute_lines(kind, demo) self.assertTrue(any(v == v for line in demo_lines for v in line), kind) def test_all_kinds_widget_api(self): csv_bars = stock_mod.load_csv(CSV_PATH) for kind in KINDS: w = _make(kind) self.assertEqual(w.item_count(), 250, kind) w.load_csv(CSV_PATH) self.assertEqual(w.item_count(), 30, kind) lines = w.get_lines() self.assertTrue(lines, kind) for values in lines: self.assertEqual(len(values), 30, kind) w.clear() self.assertEqual(w.item_count(), 0, kind) self.assertEqual(w.get_lines(), [], kind) w.set_series(csv_bars) self.assertEqual(w.item_count(), 30, kind) class TestIndicatorRendering(unittest.TestCase): def test_all_kinds_draw_demo_and_csv(self): for kind in KINDS: w = _make(kind) _draw(w) # demo series, must not raise w.load_csv(CSV_PATH) _draw(w) # loaded CSV data, must not raise def test_csv_render_differs_from_demo(self): same = [] for kind in KINDS: w = _make(kind) demo_png = _png_bytes(_draw(w)) w.load_csv(CSV_PATH) csv_png = _png_bytes(_draw(w)) if demo_png == csv_png: same.append(kind) # A kind whose CSV lines are all NaN (long warm-up on 30 bars) # draws the same empty plot as... no: the demo plot has lines, # so every kind must differ. Report the stragglers if any. self.assertEqual(same, [], same) def test_one_bar_draws(self): w = _make("IndRSI") w.set_series([(100.0, 101.0, 99.0, 100.5, 1000.0)]) _draw(w) def test_cleared_draws_placeholder(self): w = _make("IndRSI") demo_png = _png_bytes(_draw(w)) w.clear() cleared_png = _png_bytes(_draw(w)) self.assertNotEqual(demo_png, cleared_png) w.populate(stock_mod.load_csv(CSV_PATH)) self.assertNotEqual(cleared_png, _png_bytes(_draw(w))) if __name__ == "__main__": unittest.main()