Extract indicator catalog (250) from src/indicators.h
data/indicators.json: the catalog only - name, formula family (the defs_* group), plot kind, style, kPalette colour index, default parameters, and the C++ compute-function name. Formulas are NOT ported here; the function name lets the later pure-function port find each formula in the header. Groups: averages 32, bands 28, momentum 52, volume 20, volatility 43, patterns 66, final 9 = 250. Every referenced function verified defined in the header (246 distinct fns; 4 shared), all names unique, all defaults numeric-or-empty, all colour indices inside the 14-entry kPalette. The 3 extra v.push_back hits a naive grep finds are pivot-point code inside a formula body, not registry entries.
This commit is contained in:
1 parent
f5b24ee484
commit
8178513e4e
2 files changed
+3167
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Extract the indicator catalog from src/indicators.h into data/indicators.json.
|
||||
|
||||
Sources (read-only, /opt/lightgui):
|
||||
- src/indicators.h — namespace fdi: Def registry built by defs_averages,
|
||||
defs_bands, defs_momentum, defs_volume, defs_volatility, defs_patterns,
|
||||
defs_final; PlotKind / Style enums; kPalette line colours.
|
||||
|
||||
This is the CATALOG only: name, formula family (the defs_* group),
|
||||
plot kind, style, palette colour index, default parameters, and the
|
||||
C++ compute-function name. The formulas themselves are NOT ported
|
||||
here — that is a later phase (xframe-indicators, pure-function port
|
||||
with numeric golden tests). The function name is recorded so the port
|
||||
can find each formula in the header.
|
||||
|
||||
Def fields: name, PlotKind (Overlay/Pane/Markers), Style (Lines/Bars),
|
||||
color (kPalette index; line j uses color + j), defaults (comma-separated
|
||||
default parameters), fn (compute function).
|
||||
|
||||
Usage: python3 tools/extract_indicators.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from common import (DATA_DIR, ExtractError, braced_groups, cpp_string,
|
||||
fail, provenance, read_source, split_top_level,
|
||||
write_json)
|
||||
|
||||
INDICATORS_H = "src/indicators.h"
|
||||
GENERATOR = "python3 tools/extract_indicators.py"
|
||||
GROUPS = ["averages", "bands", "momentum", "volume", "volatility",
|
||||
"patterns", "final"]
|
||||
|
||||
|
||||
def parse_enums(text):
|
||||
m = re.search(r"enum class PlotKind \{([^}]*)\};", text)
|
||||
if not m:
|
||||
fail("PlotKind enum not found in %s" % INDICATORS_H)
|
||||
plot_kinds = [k.strip() for k in m.group(1).split(",") if k.strip()]
|
||||
m = re.search(r"enum class Style \{([^}]*)\};", text)
|
||||
if not m:
|
||||
fail("Style enum not found in %s" % INDICATORS_H)
|
||||
styles = [k.strip() for k in m.group(1).split(",") if k.strip()]
|
||||
return plot_kinds, styles
|
||||
|
||||
|
||||
def parse_line_palette(text):
|
||||
m = re.search(r"kPalette\[\]\[3\] = \{(.*?)\n\};", text, re.S)
|
||||
if not m:
|
||||
fail("kPalette not found in %s" % INDICATORS_H)
|
||||
entries = []
|
||||
for line in m.group(1).splitlines():
|
||||
tm = re.search(r"\{\s*([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\s*\},?\s*(//.*)?",
|
||||
line)
|
||||
if tm:
|
||||
entries.append({
|
||||
"rgb": [float(tm.group(1)), float(tm.group(2)),
|
||||
float(tm.group(3))],
|
||||
"name": (tm.group(4) or "").lstrip("/").strip(),
|
||||
})
|
||||
if not entries:
|
||||
fail("no kPalette entries parsed from %s" % INDICATORS_H)
|
||||
return entries
|
||||
|
||||
|
||||
def parse_defs_function(text, group):
|
||||
"""All v.push_back({...}) entries of one defs_<group>() function."""
|
||||
m = re.search(r"static void defs_%s\(std::vector<Def>& v\) \{(.*?)\n\}"
|
||||
% re.escape(group), text, re.S)
|
||||
if not m:
|
||||
fail("defs_%s not found in %s" % (group, INDICATORS_H))
|
||||
body = m.group(1)
|
||||
# Alias lines: auto O = PlotKind::Overlay, P = PlotKind::Pane, ...;
|
||||
# auto L = Style::Lines, B = Style::Bars;
|
||||
aliases = {}
|
||||
for am in re.finditer(
|
||||
r"(?:auto|,)\s*(\w+)\s*=\s*(PlotKind|Style)::(\w+)", body):
|
||||
aliases[am.group(1)] = am.group(3)
|
||||
entries = []
|
||||
for em in re.finditer(r"v\.push_back\(\{([^}]*)\}\);", body):
|
||||
fields = split_top_level(em.group(1))
|
||||
if len(fields) != 6:
|
||||
fail("defs_%s: malformed Def entry: {%s}" % (group, em.group(1)))
|
||||
name = cpp_string(fields[0])
|
||||
kind_tok = fields[1].strip()
|
||||
style_tok = fields[2].strip()
|
||||
kind = aliases.get(kind_tok)
|
||||
if kind is None:
|
||||
fail("defs_%s: unresolvable PlotKind token %r in %r"
|
||||
% (group, kind_tok, name))
|
||||
style = aliases.get(style_tok)
|
||||
if style is None:
|
||||
fail("defs_%s: unresolvable Style token %r in %r"
|
||||
% (group, style_tok, name))
|
||||
color = int(fields[3].strip())
|
||||
defaults = cpp_string(fields[4])
|
||||
fn = fields[5].strip()
|
||||
params = []
|
||||
if defaults.strip():
|
||||
for tok in defaults.split(","):
|
||||
tok = tok.strip()
|
||||
try:
|
||||
params.append(float(tok))
|
||||
except ValueError:
|
||||
fail("defs_%s: non-numeric default %r in %r"
|
||||
% (group, tok, name))
|
||||
entries.append({
|
||||
"name": name,
|
||||
"group": group,
|
||||
"plot_kind": kind,
|
||||
"style": style,
|
||||
"color": color,
|
||||
"defaults": defaults,
|
||||
"default_params": params,
|
||||
"function": fn,
|
||||
})
|
||||
return entries
|
||||
|
||||
|
||||
def build():
|
||||
text = read_source(INDICATORS_H)
|
||||
plot_kinds, styles = parse_enums(text)
|
||||
palette = parse_line_palette(text)
|
||||
|
||||
# Every compute function referenced must be defined in the header
|
||||
# as a Lines-returning function (f_* for indicators, pat_* for
|
||||
# candlestick patterns). Shared functions are fine (e.g. the three
|
||||
# Bollinger variants share f_bollinger* helpers).
|
||||
defined_fns = set(re.findall(r"(?:static\s+)?Lines\s+(\w+)\s*\(", text))
|
||||
|
||||
indicators = []
|
||||
groups = {}
|
||||
for group in GROUPS:
|
||||
entries = parse_defs_function(text, group)
|
||||
groups[group] = len(entries)
|
||||
indicators.extend(entries)
|
||||
|
||||
names = [e["name"] for e in indicators]
|
||||
if len(set(names)) != len(names):
|
||||
dupes = sorted(n for n in set(names) if names.count(n) > 1)
|
||||
fail("duplicate indicator names: %s" % dupes)
|
||||
for e in indicators:
|
||||
if e["plot_kind"] not in plot_kinds:
|
||||
fail("indicator %r: unknown PlotKind %r"
|
||||
% (e["name"], e["plot_kind"]))
|
||||
if e["style"] not in styles:
|
||||
fail("indicator %r: unknown Style %r" % (e["name"], e["style"]))
|
||||
if not (0 <= e["color"] < len(palette)):
|
||||
fail("indicator %r: color index %d outside kPalette (size %d)"
|
||||
% (e["name"], e["color"], len(palette)))
|
||||
if e["function"] not in defined_fns:
|
||||
fail("indicator %r: function %s not defined in %s"
|
||||
% (e["name"], e["function"], INDICATORS_H))
|
||||
|
||||
# The header's own count claim (first comment line) must agree.
|
||||
m = re.search(r"indicators\.h — (\d+) technical indicator", text)
|
||||
if m and int(m.group(1)) != len(indicators):
|
||||
fail("header claims %s indicators but %d Defs parsed"
|
||||
% (m.group(1), len(indicators)))
|
||||
|
||||
return {
|
||||
"_provenance": provenance([INDICATORS_H], GENERATOR),
|
||||
"indicator_count": len(indicators),
|
||||
"plot_kinds": plot_kinds,
|
||||
"styles": styles,
|
||||
"line_palette": palette,
|
||||
"line_palette_size": len(palette),
|
||||
"groups": groups,
|
||||
"indicators": indicators,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
data = build()
|
||||
except ExtractError as e:
|
||||
print("extract_indicators: ERROR: %s" % e, file=sys.stderr)
|
||||
return 1
|
||||
write_json(os.path.join(DATA_DIR, "indicators.json"), data)
|
||||
print("indicators: %d (%s)"
|
||||
% (data["indicator_count"],
|
||||
", ".join("%s %d" % kv
|
||||
for kv in sorted(data["groups"].items()))))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user