75eefbbd08
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).
58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using Wickra;
|
|
using Wickra.Examples;
|
|
|
|
// Strategy example: RSI(14) mean-reversion.
|
|
//
|
|
// Go long when RSI(14) drops below 30 (oversold), exit when it recovers above
|
|
// 70 (overbought). 0.1% fees per trade. The C# counterpart of
|
|
// examples/python/strategy_rsi_mean_reversion.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 Oversold = 30.0;
|
|
const double Overbought = 70.0;
|
|
|
|
var bars = args.Length > 0 ? MarketData.LoadOhlcvCsv(args[0]) : MarketData.BundledCandles("btcusdt-1h.csv");
|
|
|
|
using var rsi = new Rsi(14);
|
|
|
|
var inPosition = false;
|
|
var entryPrice = 0.0;
|
|
var closedTrades = new List<double>();
|
|
var equity = 1.0;
|
|
var equityCurve = new List<double>();
|
|
|
|
foreach (var b in bars)
|
|
{
|
|
var value = rsi.Update(b.Close);
|
|
var price = b.Close;
|
|
equityCurve.Add(inPosition ? equity * (price / entryPrice) : equity);
|
|
if (!double.IsFinite(value))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!inPosition && value < Oversold)
|
|
{
|
|
entryPrice = price;
|
|
equity *= 1.0 - Fee;
|
|
inPosition = true;
|
|
}
|
|
else if (inPosition && value > Overbought)
|
|
{
|
|
var tradeRet = price / entryPrice - 1.0;
|
|
closedTrades.Add(tradeRet);
|
|
equity *= (1.0 + tradeRet) * (1.0 - Fee);
|
|
inPosition = false;
|
|
}
|
|
}
|
|
|
|
if (inPosition)
|
|
{
|
|
var tradeRet = bars[^1].Close / entryPrice - 1.0;
|
|
closedTrades.Add(tradeRet);
|
|
equity *= (1.0 + tradeRet) * (1.0 - Fee);
|
|
}
|
|
|
|
Backtest.PrintSummary("RSI Mean-Reversion (1h, BTCUSDT)",
|
|
bars[0].Close, bars[^1].Close, bars.Length, closedTrades, equity, equityCurve);
|