feat(data): native live Binance kline feed in 9 languages (+ 3d/1M intervals) (#313)

* feat(data): C-ABI Binance feed + Go binding (F4 wip)

C ABI exposes the existing async BinanceKlineStream (tokio + TLS, auto-reconnect,
mock-server-tested in wickra-data) through a blocking poll: wickra_binance_connect
/ _next(out, timeout_ms) -> {1 event, 0 timeout, -1 closed} / _close / _free over
an opaque BinanceStream that owns a current-thread runtime. WickraKlineEvent
carries OHLCV + open_time + is_closed + a 16-byte symbol buffer. `live-binance`
is now a default feature of wickra-c (the published DLL ships the feed; the wasm
build drops it via --no-default-features).

Go: NewBinanceFeed(symbols, interval, baseURL) + Next(timeout) + Close, with a
deterministic error-path smoke (the connect->event pipeline is covered by the
Rust mock-WS-server tests).

* feat(data): Binance feed C# + Java bindings (F4 wip)

C#: BinanceFeed(symbols, interval, baseUrl?) + Next(timeout) -> KlineEvent? +
Dispose; bespoke WickraKlineEvent native struct (fixed symbol buffer + byte
is_closed) since the scalar struct parser can't model it. `char` maps to `byte`
for the const char* params.

Java: BinanceFeed + KlineEvent record + BinanceInterval enum over Panama FFM;
the event is read at hand-computed offsets (symbol@0, doubles@16..48,
open_time@56, is_closed@64; 72-byte struct). Both with deterministic error-path
smokes (pipeline covered by the Rust mock-WS-server tests).

* feat(data): Binance feed R binding (F4 wip)

R: BinanceFeed(symbols, interval, base_url) + binance_next(feed, timeout_ms) ->
named list | NULL + binance_close, via bespoke .Call glue (wk_binance_*). The
glue + its registration entries are gated out of the Emscripten/wasm build
(#ifndef __EMSCRIPTEN__) since r-universe/webR has no raw sockets. NAMESPACE
exports added by hand (roxygen2 not installed locally). Deterministic error-path
smoke; pipeline covered by the Rust mock-WS-server tests.

* feat(data): native Binance feed for Node + Python; CHANGELOG (F4 complete)

Node (napi) BinanceFeed: new(symbols, interval, baseUrl?) + next(timeoutMs) ->
KlineEvent | null + close. Python (pyo3) BinanceFeed: same, with next releasing
the GIL (py.detach) while it waits. Both drive the mock-server-tested async
BinanceKlineStream on a single-thread tokio runtime (blocking poll); wickra-data
gains the live-binance feature + tokio in each binding.

Completes F4: the live Binance kline feed is now native in all 9 languages
(WASM excluded), with no third-party WebSocket client in any of them.
This commit is contained in:
kingchenc
2026-06-16 02:01:37 +02:00
committed by GitHub
parent baf4d0ff47
commit 3a709d9a66
30 changed files with 1127 additions and 5 deletions
@@ -41508,3 +41508,95 @@ public sealed class Zlema : IDisposable
public void Dispose() => _handle.Dispose();
}
/// <summary>Kline interval for the live Binance feed.</summary>
public enum BinanceInterval : byte
{
OneSecond,
OneMinute,
ThreeMinutes,
FiveMinutes,
FifteenMinutes,
ThirtyMinutes,
OneHour,
TwoHours,
FourHours,
SixHours,
EightHours,
TwelveHours,
OneDay,
ThreeDays,
OneWeek,
OneMonth,
}
/// <summary>One event from the live Binance feed.</summary>
public readonly record struct KlineEvent(string Symbol, double Open, double High, double Low, double Close, double Volume, long OpenTime, bool IsClosed);
/// <summary>A live Binance kline stream over the Wickra C ABI.</summary>
public sealed class BinanceFeed : IDisposable
{
private readonly WickraHandle _handle;
/// <summary>Connect to Binance's live kline stream for the given comma-separated symbols
/// (case-insensitive) at <paramref name="interval"/>. <paramref name="baseUrl"/> overrides the
/// endpoint (null = production; pass a ws:// URL to target a test server).</summary>
public BinanceFeed(string symbols, BinanceInterval interval, string? baseUrl = null)
{
ArgumentNullException.ThrowIfNull(symbols);
var symBytes = System.Text.Encoding.UTF8.GetBytes(symbols + '\0');
var urlBytes = baseUrl is null ? null : System.Text.Encoding.UTF8.GetBytes(baseUrl + '\0');
nint ptr;
unsafe
{
fixed (byte* sp = symBytes)
fixed (byte* up = urlBytes)
{
ptr = NativeMethods.wickra_binance_connect(sp, (byte)interval, up);
}
}
if (ptr == nint.Zero)
{
throw new ArgumentException("invalid BinanceFeed parameters");
}
_handle = new WickraHandle(ptr, NativeMethods.wickra_binance_free);
}
/// <summary>Poll for the next kline event, waiting up to <paramref name="timeout"/>.
/// Returns the event, or null on timeout (call again). Throws once the stream is
/// closed or has errored out.</summary>
public KlineEvent? Next(TimeSpan timeout)
{
WickraKlineEvent ev;
int code;
unsafe
{
code = NativeMethods.wickra_binance_next(_handle.DangerousGetHandle(), &ev, (long)timeout.TotalMilliseconds);
}
GC.KeepAlive(_handle);
if (code == 0)
{
return null;
}
if (code != 1)
{
throw new InvalidOperationException("binance feed closed");
}
string symbol;
unsafe
{
symbol = Marshal.PtrToStringUTF8((nint)ev.symbol) ?? string.Empty;
}
return new KlineEvent(symbol, ev.open, ev.high, ev.low, ev.close, ev.volume, ev.open_time, ev.is_closed != 0);
}
public void Dispose()
{
unsafe
{
NativeMethods.wickra_binance_close(_handle.DangerousGetHandle());
}
GC.KeepAlive(_handle);
_handle.Dispose();
}
}
@@ -12442,6 +12442,18 @@ internal static partial class NativeMethods
[LibraryImport(WickraNative.LibraryName)]
internal static partial void wickra_candle_reader_free(nint handle);
[LibraryImport(WickraNative.LibraryName)]
internal static unsafe partial nint wickra_binance_connect(byte* symbols, byte interval, byte* baseUrl);
[LibraryImport(WickraNative.LibraryName)]
internal static unsafe partial int wickra_binance_next(nint handle, WickraKlineEvent* @out, long timeoutMs);
[LibraryImport(WickraNative.LibraryName)]
internal static partial void wickra_binance_close(nint handle);
[LibraryImport(WickraNative.LibraryName)]
internal static partial void wickra_binance_free(nint handle);
}
[StructLayout(LayoutKind.Sequential)]
@@ -13298,3 +13310,16 @@ internal static partial class NativeMethods
public double direction;
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct WickraKlineEvent
{
public fixed byte symbol[16];
public double open;
public double high;
public double low;
public double close;
public double volume;
public long open_time;
public byte is_closed;
}