"""End-to-end parameter sweep through the TA-Lib Python wrapper (1M bars, periods 2..100)."""
import hashlib, sys
import numpy as np
import talib
ind = sys.argv[1]
n = int(sys.argv[2]) if len(sys.argv) > 2 else 1_000_000
rng = np.random.default_rng(20261007)
c = 100.0 * np.cumprod(1.0 + 0.02 * (rng.random(n) - 0.5))
o = np.concatenate(([c[0]], c[:-1]))
h = np.maximum(o, c) * (1.0 + 0.005 * rng.random(n))
l = np.minimum(o, c) * (1.0 - 0.005 * rng.random(n))
m = hashlib.sha256()
for k in range(99):
    p = 2 + k
    if ind == 'EMA':   r = talib.EMA(c, timeperiod=p)
    elif ind == 'RSI': r = talib.RSI(c, timeperiod=p)
    elif ind == 'ATR': r = talib.ATR(h, l, c, timeperiod=p)
    elif ind == 'ADX': r = talib.ADX(h, l, c, timeperiod=p)
    elif ind == 'DX':  r = talib.DX(h, l, c, timeperiod=p)
    elif ind == 'CCI': r = talib.CCI(h, l, c, timeperiod=p)
    elif ind == 'MACD': f = 2 + k % 33; r = talib.MACD(c, fastperiod=f, slowperiod=f + 4 + 8 * (k // 33), signalperiod=9)
    elif ind == 'SAR': r = talib.SAR(h, l, acceleration=0.005 * (1 + k % 33), maximum=0.1 * (1 + k // 33))
    elif ind == 'BBANDS': r = talib.BBANDS(c, timeperiod=p, nbdevup=2.0, nbdevdn=2.0, matype=0)
    elif ind == 'STOCH': r = talib.STOCH(h, l, c, fastk_period=p, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    else: raise SystemExit('unknown indicator')
    # The printed digest folds in a fixed sample of every result (every 997th value and the last),
    # as a client's code reads its results; hashing all 99 x 8 MB would cost more than most
    # indicators. Bit-exactness through the wrapper is checked in full by tools/py_exact.py.
    for a in (r if isinstance(r, tuple) else (r,)):
        m.update(a[::997].tobytes())
        m.update(a[-1:].tobytes())
print(ind, m.hexdigest())
