677ea37402
Stacked on #315 (the native Binance REST fetcher). Retarget to `main` once #315 merges. Migrates the runnable examples off third-party data-I/O packages onto Wickra's native data layer (`CandleReader`, `Resampler`, `BinanceFeed`, `fetch_*klines`). ## Third-party packages removed (the zero-dep selling point) - **Node**: `ws` (live feed → BinanceFeed) — dropped from package.json + lockfile - **Go**: `github.com/coder/websocket` — dropped from go.mod / go.sum (`go mod tidy`) - **Java**: `jackson-databind` (live feed + REST fetch) — dropped from pom.xml - **R**: `jsonlite` + `websocket` + `later` — dropped from the README notes Each language's CSV loading now goes through `CandleReader`, manual resampling through `Resampler`, the live feed through `BinanceFeed`, and (Java/R) the REST download through the native fetcher. ## Verification Ran the offline examples per language against the bundled data — backtest and multi_timeframe produce identical output across Python / Node / Go / Java / R (e.g. ATR(14) last 345.1010; 1h→5m resamples to 240 bars, →15m to 80 bars). C# / C / WASM (stdlib-only, no third-party deps to remove) follow in this branch. Note: the streaming `strategy_*` examples have pre-existing candle-indicator runtime bugs (CI only syntax-smokes them); the CSV migration preserves their shape and leaves those bugs for a separate fix.
62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
namespace Wickra.Examples;
|
|
|
|
/// <summary>One OHLCV bar with a millisecond timestamp.</summary>
|
|
public readonly record struct Bar(double Open, double High, double Low, double Close, double Volume, long Timestamp);
|
|
|
|
/// <summary>
|
|
/// Deterministic synthetic market data plus a small OHLCV CSV loader, shared by
|
|
/// the offline examples so they run without network access.
|
|
/// </summary>
|
|
public static class MarketData
|
|
{
|
|
/// <summary>A reproducible price path (trend + two cycles), no randomness.</summary>
|
|
public static double[] SyntheticPrices(int count, double start = 100.0)
|
|
{
|
|
var prices = new double[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
prices[i] = start + 12.0 * Math.Sin(i * 0.05) + 5.0 * Math.Sin(i * 0.013) + i * 0.01;
|
|
}
|
|
|
|
return prices;
|
|
}
|
|
|
|
/// <summary>A reproducible OHLCV series derived from <see cref="SyntheticPrices"/>.</summary>
|
|
public static Bar[] SyntheticCandles(int count, long startTimestamp = 0, long stepMs = 3_600_000)
|
|
{
|
|
var prices = SyntheticPrices(count + 1);
|
|
var bars = new Bar[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
var open = prices[i];
|
|
var close = prices[i + 1];
|
|
var high = Math.Max(open, close) + 0.5 + Math.Abs(Math.Sin(i * 0.7));
|
|
var low = Math.Min(open, close) - 0.5 - Math.Abs(Math.Cos(i * 0.7));
|
|
var volume = 1_000.0 + 500.0 * (1.0 + Math.Sin(i * 0.1));
|
|
bars[i] = new Bar(open, high, low, close, volume, startTimestamp + i * stepMs);
|
|
}
|
|
|
|
return bars;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads an OHLCV CSV. Accepts rows of <c>timestamp,open,high,low,close,volume</c>
|
|
/// or <c>open,high,low,close,volume</c>; a non-numeric first row is treated as a header.
|
|
/// </summary>
|
|
public static Bar[] LoadOhlcvCsv(string path)
|
|
{
|
|
// Native CandleReader: header validation, BOM and field-whitespace tolerance.
|
|
// No manual CSV parsing.
|
|
using var reader = new Wickra.CandleReader(File.ReadAllText(path));
|
|
var candles = reader.Read();
|
|
var bars = new Bar[candles.Length];
|
|
for (var i = 0; i < candles.Length; i++)
|
|
{
|
|
var c = candles[i];
|
|
bars[i] = new Bar(c.Open, c.High, c.Low, c.Close, c.Volume, (long)c.Timestamp);
|
|
}
|
|
|
|
return bars;
|
|
}
|
|
}
|