examples: fix and harmonize the strategy backtests across all languages (#324)
The strategy_* examples were only syntax-smoked in CI, never run, which hid two classes of problem: 1. Python strategy_macd_adx / strategy_bollinger_squeeze passed three separate arguments to the candle indicators ADX/ATR, whose .update() takes a single candle — a TypeError at runtime — and read the ADX tuple at index 0 (plus_di) instead of 2 (adx). Both fixed. 2. The Go / C# / R / Java strategies defaulted to synthetic data and used a different (annualised) one-line summary, so they printed wildly different numbers from the Rust/Python/Node/C/WASM suite. Rewrite them to the shared per-trade backtest (load the bundled BTCUSDT CSV by default, same entry/exit logic, same print_summary output). All nine runnable bindings now print byte-identical backtest summaries on the same data (MACD+ADX 246 trades / -47.19%, RSI 37 / -17.84%, Bollinger 1 / -7.82%), verified by diffing each language's output against the Python reference. WASM shares the same logic and bundled dataset (browser-rendered).
This commit is contained in:
@@ -115,7 +115,7 @@ def main() -> None:
|
||||
|
||||
for c in candles:
|
||||
bb_out = bb.update(c["close"])
|
||||
atr_val = atr.update(c["high"], c["low"], c["close"])
|
||||
atr_val = atr.update(c)
|
||||
price = c["close"]
|
||||
mtm = equity * (price / entry_price) if in_position else equity
|
||||
equity_curve.append(mtm)
|
||||
|
||||
@@ -101,7 +101,7 @@ def main() -> None:
|
||||
|
||||
for c in candles:
|
||||
macd_out = macd.update(c["close"])
|
||||
adx_out = adx.update(c["high"], c["low"], c["close"])
|
||||
adx_out = adx.update(c)
|
||||
price = c["close"]
|
||||
mtm = equity * (price / entry_price) if in_position else equity
|
||||
equity_curve.append(mtm)
|
||||
@@ -112,7 +112,7 @@ def main() -> None:
|
||||
# MACD output is a (macd, signal, histogram) tuple/object across
|
||||
# bindings. The Python binding returns a namedtuple.
|
||||
histogram = macd_out[2] if isinstance(macd_out, tuple) else macd_out.histogram
|
||||
adx_value = adx_out[0] if isinstance(adx_out, tuple) else adx_out.adx
|
||||
adx_value = adx_out[2] if isinstance(adx_out, tuple) else adx_out.adx
|
||||
|
||||
hist_sign = histogram > 0.0
|
||||
cross_up = prev_hist_sign is False and hist_sign
|
||||
|
||||
Reference in New Issue
Block a user