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:
@@ -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).");
|
||||
|
||||
Reference in New Issue
Block a user