"""Consistency check of the IcustomGroups results, written a second time in Python.

Usage: python crosscheck.py [folder]    (default: the folder of this script, MQL5\\Files\\IcustomGroups)

This is not an independent reference. It applies the rules stated in the article
to the files the MQL5 programs wrote, so it catches arithmetic and logging slips,
not a wrong rule.
1. type_matrix.csv (TypeMatrix.mq5): every main cell is predicted from 3 rules,
   for IndicatorCreate() and for iCustom(), and the 2 APIs are compared. Every
   edge cell is predicted from C-style conversions (truncation toward zero,
   wraparound, INT_MIN for a double out of int range, float rounding) and the
   245-character string limit measured in the article.
2. BandsLite.layout.txt (SaveLayout.mq5): the plain call iCustom(50, 2.5, 3, 80.0)
   is replayed slot by slot to predict what BandsLite receives.
3. shift_cost.csv (ShiftCost.mq5): both sets of bands are recomputed from the
   closes, and the signals are counted again with the BandsLite rule.
Only the standard library is used.
"""
import csv, math, os, struct, sys

folder = sys.argv[1] if len(sys.argv) > 1 else os.path.dirname(os.path.abspath(__file__))
path = lambda name: os.path.join(folder, name)

# --- 1. the type matrix -------------------------------------------------
BITS = {"int": (32, True), "enum": (32, True), "color": (32, False), "uint": (32, False),
        "uchar": (8, False), "short": (16, True), "datetime": (64, True)}
MAX_STRING = 245                             # measured on build 6230, see the article


def parse(value):
    """'double 2.5' -> ('double', 2.5), 'string 245 chars' -> ('string', 'x' * 245)."""
    kind, _, text = value.partition(" ")
    if kind == "string":
        if text == "empty":
            return kind, ""
        if text.endswith(" chars"):
            return kind, "x" * int(text.split()[0])
        return kind, text
    if kind == "datetime":
        return kind, 1767225600              # D'2026.01.01'
    words = {"true": 1, "red": 255, "nan": float("nan"), "inf": float("inf")}
    number = words.get(text)
    if number is None:
        number = float(text) if kind == "double" else int(text)
    return kind, number


def to_integer(number, bits, signed):
    """A number stored in an integer slot of the given width."""
    if isinstance(number, float):
        if math.isnan(number) or math.isinf(number) or abs(number) >= 2 ** 63:
            return -2 ** 31 if bits == 32 and signed else 0
        number = int(number)                 # toward zero
        if bits == 32 and signed and not -2 ** 31 <= number < 2 ** 31:
            return -2 ** 31                  # a double out of int range
    number %= 2 ** bits
    if signed and number >= 2 ** (bits - 1):
        number -= 2 ** bits
    return number


def show_real(x, precise):
    if math.isnan(x):
        return "nan"
    if math.isinf(x):
        return "inf"
    return "%.10g" % x if precise else "%.2f" % x


def predict(slot, value, precise):
    """(loads, stored) under the 3 rules plus the conversions."""
    kind, v = parse(value)
    is_string = kind == "string"
    if slot == "group":                      # rule 1: a group slot takes anything and keeps its own text
        return True, "kept"
    if slot == "string":                     # rule 2: a string slot takes only strings
        if not is_string:
            return False, ""
        if precise:
            n = min(len(v), MAX_STRING)
            return True, "%d chars, indicator got %d" % (n, n)
        return True, v
    if is_string:                            # rule 3: a numeric slot takes any number, never a string
        return False, ""
    if slot == "double":
        return True, show_real(float(v), precise)
    if slot == "float":
        x = float(v)
        if not math.isinf(x) and not math.isnan(x):
            x = float("inf") if abs(x) > 3.4028234663852886e38 else struct.unpack("f", struct.pack("f", x))[0]
        return True, show_real(x, precise)
    if slot == "bool":
        return True, "1" if v != 0 else "0"
    bits, signed = BITS[slot]
    return True, str(to_integer(v, bits, signed))


rows = list(csv.DictReader(open(path("type_matrix.csv"), encoding="ascii"), delimiter=";"))
for part in ("main", "edge"):
    cells = {}
    for api in ("IndicatorCreate", "iCustom"):
        mine = [r for r in rows if r["part"] == part and r["api"] == api]
        agree = 0
        for r in mine:
            loads, stored = predict(r["slot"], r["value"], part == "edge")
            ok = (loads == (r["loaded"] == "1")) and (not loads or stored == r["stored"])
            agree += ok
            if not ok:
                print("  differs:", dict(r), "predicted", loads, stored)
            cells.setdefault((r["slot"], r["value"]), []).append((r["loaded"], r["stored"]))
        print("%s %-15s %d of %d cells match the rules" % (part, api, agree, len(mine)))
    same = sum(1 for v in cells.values() if len(v) == 2 and v[0] == v[1])
    print("%s: the 2 APIs agree in %d of %d cells" % (part, same, len(cells)))

# --- 2. the plain call replayed on the saved layout ---------------------
lines = open(path("BandsLite.layout.txt"), encoding="ascii").read().splitlines()
slots = [l.split("\t") for l in lines[1:]]
TYPE_DOUBLE = "13"
values = [50, 2.5, 3, 80.0]                  # what the plain iCustom() call passes, in order
received = []
for i, s in enumerate(slots):
    given = values[i] if i < len(values) else None
    if int(s[3]) & 1:
        continue                             # the group slot swallows the value it is given
    if given is None:
        given = float(s[2]) if s[0] == TYPE_DOUBLE else int(s[1])
    received.append(float(given) if s[0] == TYPE_DOUBLE else int(given))
print("plain call on the saved layout: period=%d deviation=%.2f confirm=%d min_width=%.1f" % tuple(received))

# --- 3. the bands and signals recomputed from the closes ----------------
bars = list(csv.DictReader(open(path("shift_cost.csv"), encoding="ascii"), delimiter=";"))
close = [float(b["close"]) for b in bars]
POINT = 0.00001


def bands(period, dev):
    up, lo = [None] * len(close), [None] * len(close)
    for i in range(period - 1, len(close)):
        w = close[i - period + 1:i + 1]
        mean = sum(w) / period
        sd = math.sqrt(max(sum(x * x for x in w) / period - mean * mean, 0.0))
        up[i], lo[i] = mean + dev * sd, mean - dev * sd
    return up, lo


def signals(up, lo, confirm, min_width, first):
    buys = sells = 0
    for i in range(first, len(close)):
        if up[i] - lo[i] < min_width * POINT:
            continue
        below = all(close[i - k] < lo[i - k] for k in range(1, confirm + 1))
        above = all(close[i - k] > up[i - k] for k in range(1, confirm + 1))
        buys += below and close[i] > lo[i]
        sells += above and close[i] < up[i]
    return buys, sells


for name, (period, dev, confirm, width) in (("ok", (50, 2.5, 3, 80.0)), ("raw", received)):
    up, lo = bands(period, dev)
    first = period - 1 + confirm             # bars before this need closes older than the file
    worst = max(abs(up[i] - float(b["upper_" + name])) for i, b in enumerate(bars) if i >= first)
    outside = sum(1 for i in range(first, len(close)) if close[i] > up[i] + 1e-12 or close[i] < lo[i] - 1e-12)
    py = signals(up, lo, confirm, width, first + confirm)
    mq = (sum(1 for b in bars[first + confirm:] if b["signal_" + name] == "1"),
          sum(1 for b in bars[first + confirm:] if b["signal_" + name] == "-1"))
    print("%-3s period %d dev %.1f: upper band max diff %.6f, closes outside the bands %d, "
          "signals Python %d/%d, MQL5 %d/%d" % (name, period, dev, worst, outside, py[0], py[1], mq[0], mq[1]))
