examples: migrate to the native data layer (drop ws/coder-websocket/jackson/jsonlite) (#316)

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.
This commit is contained in:
kingchenc
2026-06-17 01:49:11 +02:00
committed by GitHub
parent 2ae76bb90e
commit 677ea37402
40 changed files with 576 additions and 1102 deletions
+9 -26
View File
@@ -45,34 +45,17 @@ public static class MarketData
/// </summary>
public static Bar[] LoadOhlcvCsv(string path)
{
var bars = new List<Bar>();
foreach (var rawLine in File.ReadLines(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 line = rawLine.Trim();
if (line.Length == 0)
{
continue;
}
var cols = line.Split(',');
if (!double.TryParse(cols[0], System.Globalization.CultureInfo.InvariantCulture, out _) &&
!long.TryParse(cols[0], out _))
{
continue; // header row
}
double F(int i) => double.Parse(cols[i], System.Globalization.CultureInfo.InvariantCulture);
if (cols.Length >= 6)
{
bars.Add(new Bar(F(1), F(2), F(3), F(4), F(5), long.Parse(cols[0])));
}
else
{
bars.Add(new Bar(F(0), F(1), F(2), F(3), F(4), bars.Count));
}
var c = candles[i];
bars[i] = new Bar(c.Open, c.High, c.Low, c.Close, c.Volume, (long)c.Timestamp);
}
return bars.ToArray();
return bars;
}
}
+9 -19
View File
@@ -1,33 +1,23 @@
using System.Globalization;
using System.Text.Json;
using Wickra;
// Download real BTCUSDT hourly klines from the Binance REST API into a CSV that the
// other examples can consume. Requires network access (build-only in CI).
const string url = "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=500";
// other examples can consume, using Wickra's native fetcher — no third-party HTTP or
// JSON library. Requires network access (build-only in CI).
Console.WriteLine("Fetching 500 BTCUSDT 1h klines from Binance...");
using var http = new HttpClient();
Console.WriteLine($"Fetching {url}");
var json = await http.GetStringAsync(url);
var klines = BinanceFeed.FetchKlines("BTCUSDT", BinanceInterval.OneHour, 500);
using var doc = JsonDocument.Parse(json);
var dir = Path.Combine(AppContext.BaseDirectory, "data");
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, "btcusdt_1h.csv");
using var writer = new StreamWriter(path);
writer.WriteLine("timestamp,open,high,low,close,volume");
var count = 0;
foreach (var kline in doc.RootElement.EnumerateArray())
foreach (var k in klines)
{
// Binance kline array: [openTime, open, high, low, close, volume, ...]
var ts = kline[0].GetInt64();
var o = kline[1].GetString();
var h = kline[2].GetString();
var l = kline[3].GetString();
var c = kline[4].GetString();
var v = kline[5].GetString();
writer.WriteLine(string.Create(CultureInfo.InvariantCulture, $"{ts},{o},{h},{l},{c},{v}"));
count++;
writer.WriteLine(string.Create(CultureInfo.InvariantCulture,
$"{(long)k.Timestamp},{k.Open},{k.High},{k.Low},{k.Close},{k.Volume}"));
}
Console.WriteLine($"Wrote {count} klines to {path}");
Console.WriteLine($"Wrote {klines.Length} klines to {path}");
+14 -30
View File
@@ -1,40 +1,24 @@
using System.Globalization;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using Wickra;
// Stream live BTCUSDT 1-minute klines from Binance and feed each close through EMA(20).
// Stream live BTCUSDT 1-minute klines from Binance and feed each close through EMA(20),
// using Wickra's native BinanceFeed — no third-party WebSocket or JSON library.
// Requires network access (build-only in CI). Runs for up to 60 seconds.
var uri = new Uri("wss://stream.binance.com:9443/ws/btcusdt@kline_1m");
Console.WriteLine($"Connecting to {uri} (up to 60s)...");
Console.WriteLine("Streaming live BTCUSDT 1-minute klines from Binance (up to 60s)...");
using var ws = new ClientWebSocket();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
// Native feed: a blocking poll over the same tested stream as the Rust core.
// Next(timeout) returns the event, or null on timeout (poll again).
using var feed = new BinanceFeed("BTCUSDT", BinanceInterval.OneMinute);
using var ema = new Ema(20);
var buffer = new byte[8192];
try
var deadline = DateTime.UtcNow.AddSeconds(60);
while (DateTime.UtcNow < deadline)
{
await ws.ConnectAsync(uri, cts.Token);
while (ws.State == WebSocketState.Open && !cts.IsCancellationRequested)
var ev = feed.Next(TimeSpan.FromSeconds(1));
if (ev is null)
{
var result = await ws.ReceiveAsync(buffer, cts.Token);
if (result.MessageType == WebSocketMessageType.Close)
{
break;
}
using var doc = JsonDocument.Parse(Encoding.UTF8.GetString(buffer, 0, result.Count));
if (doc.RootElement.TryGetProperty("k", out var k))
{
var close = double.Parse(k.GetProperty("c").GetString()!, CultureInfo.InvariantCulture);
var value = ema.Update(close);
Console.WriteLine($"close={close:F2} EMA(20)={value:F2}");
}
continue;
}
Console.WriteLine($"close={ev.Value.Close:F2} EMA(20)={ema.Update(ev.Value.Close):F2}");
}
catch (OperationCanceledException)
{
Console.WriteLine("Done (time limit reached).");
}
Console.WriteLine("Done (time limit reached).");
+14 -8
View File
@@ -25,20 +25,26 @@ static Bar[] Resample(Bar[] source, int factor)
return source;
}
// Native Resampler: bucket by an absolute timeframe (the synthetic bars step
// 60_000 ms, so factor minutes == factor*60_000 ms). No hand-written bucketing.
using var r = new Resampler((long)factor * 60_000);
var output = new List<Bar>();
for (var i = 0; i < source.Length; i += factor)
foreach (var b in source)
{
var end = Math.Min(i + factor, source.Length);
double high = double.MinValue, low = double.MaxValue, volume = 0;
for (var j = i; j < end; j++)
var c = r.Update(b.Open, b.High, b.Low, b.Close, b.Volume, b.Timestamp);
if (c is not null)
{
high = Math.Max(high, source[j].High);
low = Math.Min(low, source[j].Low);
volume += source[j].Volume;
output.Add(ToBar(c.Value));
}
}
output.Add(new Bar(source[i].Open, high, low, source[end - 1].Close, volume, source[i].Timestamp));
var last = r.Flush();
if (last is not null)
{
output.Add(ToBar(last.Value));
}
return output.ToArray();
}
static Bar ToBar(Candle c) => new(c.Open, c.High, c.Low, c.Close, c.Volume, (long)c.Timestamp);