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:
@@ -1,42 +1,66 @@
|
||||
using Wickra;
|
||||
using Wickra.Examples;
|
||||
|
||||
// Trend follower: enter long on a MACD histogram cross up, but only when ADX(14) > 20
|
||||
// confirms a trend; exit when the histogram crosses back below zero.
|
||||
var bars = args.Length > 0 ? MarketData.LoadOhlcvCsv(args[0]) : MarketData.SyntheticCandles(2000);
|
||||
// Strategy example: MACD crossover with ADX trend-strength filter.
|
||||
//
|
||||
// Enters long on a MACD histogram cross up (the histogram turns positive) while
|
||||
// ADX(14) > 20 (a directional market); exits on the opposite MACD crossover
|
||||
// regardless of ADX. 0.1% fees per trade. The C# counterpart of
|
||||
// examples/python/strategy_macd_adx.py, printing the same summary. Uses the
|
||||
// checked-in examples/data/btcusdt-1h.csv dataset (pass a CSV path to override).
|
||||
const double Fee = 0.001;
|
||||
const double AdxFloor = 20.0;
|
||||
|
||||
var bars = args.Length > 0 ? MarketData.LoadOhlcvCsv(args[0]) : MarketData.BundledCandles("btcusdt-1h.csv");
|
||||
|
||||
using var macd = new MacdIndicator(12, 26, 9);
|
||||
using var adx = new Adx(14);
|
||||
|
||||
var returns = new List<double>();
|
||||
var trades = 0;
|
||||
var inPosition = false;
|
||||
var entry = 0.0;
|
||||
var prevHistogram = double.NaN;
|
||||
var entryPrice = 0.0;
|
||||
var closedTrades = new List<double>();
|
||||
var equity = 1.0;
|
||||
var equityCurve = new List<double>();
|
||||
bool? prevSign = null;
|
||||
|
||||
foreach (var b in bars)
|
||||
{
|
||||
var m = macd.Update(b.Close);
|
||||
var a = adx.Update(b.Open, b.High, b.Low, b.Close, b.Volume, b.Timestamp);
|
||||
var price = b.Close;
|
||||
equityCurve.Add(inPosition ? equity * (price / entryPrice) : equity);
|
||||
|
||||
if (m is not { } macdValue || a is not { } adxValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var trending = adxValue.Adx > 20.0;
|
||||
if (!inPosition && trending && double.IsFinite(prevHistogram) && prevHistogram <= 0.0 && macdValue.Histogram > 0.0)
|
||||
var histSign = macdValue.Histogram > 0.0;
|
||||
var crossUp = prevSign == false && histSign;
|
||||
var crossDown = prevSign == true && !histSign;
|
||||
prevSign = histSign;
|
||||
|
||||
if (!inPosition && crossUp && adxValue.Adx > AdxFloor)
|
||||
{
|
||||
entryPrice = price;
|
||||
equity *= 1.0 - Fee;
|
||||
inPosition = true;
|
||||
entry = b.Close;
|
||||
trades++;
|
||||
}
|
||||
else if (inPosition && macdValue.Histogram < 0.0)
|
||||
else if (inPosition && crossDown)
|
||||
{
|
||||
returns.Add((b.Close - entry) / entry);
|
||||
var tradeRet = price / entryPrice - 1.0;
|
||||
closedTrades.Add(tradeRet);
|
||||
equity *= (1.0 + tradeRet) * (1.0 - Fee);
|
||||
inPosition = false;
|
||||
}
|
||||
|
||||
prevHistogram = macdValue.Histogram;
|
||||
}
|
||||
|
||||
Backtest.Print("MACD + ADX trend", Backtest.Summarize(returns, trades));
|
||||
if (inPosition)
|
||||
{
|
||||
var tradeRet = bars[^1].Close / entryPrice - 1.0;
|
||||
closedTrades.Add(tradeRet);
|
||||
equity *= (1.0 + tradeRet) * (1.0 - Fee);
|
||||
}
|
||||
|
||||
Backtest.PrintSummary("MACD + ADX Trend Filter (1h, BTCUSDT)",
|
||||
bars[0].Close, bars[^1].Close, bars.Length, closedTrades, equity, equityCurve);
|
||||
|
||||
Reference in New Issue
Block a user