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:
@@ -55,6 +55,7 @@ export(BeltHold)
|
||||
export(Beta)
|
||||
export(BetaNeutralSpread)
|
||||
export(BetterVolume)
|
||||
export(BinanceFeed)
|
||||
export(BipowerVariation)
|
||||
export(BodySizePct)
|
||||
export(BollingerBands)
|
||||
@@ -527,6 +528,8 @@ export(ZeroLagMacd)
|
||||
export(ZigZag)
|
||||
export(Zlema)
|
||||
export(batch)
|
||||
export(binance_close)
|
||||
export(binance_next)
|
||||
export(is_ready)
|
||||
export(name)
|
||||
export(push)
|
||||
|
||||
@@ -4143,3 +4143,40 @@ Zlema <- function(period) {
|
||||
.wk_obj("zlema", ptr, "Zlema")
|
||||
}
|
||||
|
||||
#' Connect to a live Binance kline feed
|
||||
#'
|
||||
#' Opens a live Binance kline stream for one or more comma-separated `symbols`
|
||||
#' (case-insensitive) at the given `interval` code (an integer `0:15`, the same
|
||||
#' order as the other bindings: 0 = 1s, 1 = 1m, ... 12 = 1d, 13 = 3d, 14 = 1w,
|
||||
#' 15 = 1M). `base_url` overrides the endpoint (`NULL` = production). Not
|
||||
#' available in the wasm (r-universe/webR) build, which has no raw sockets.
|
||||
#'
|
||||
#' @keywords internal
|
||||
#' @export
|
||||
BinanceFeed <- function(symbols, interval, base_url = NULL) {
|
||||
ptr <- .Call("wk_binance_connect", symbols, as.integer(interval), base_url,
|
||||
PACKAGE = "wickra")
|
||||
structure(list(ptr = ptr), class = "wickra_binance_feed")
|
||||
}
|
||||
|
||||
#' Poll a Binance feed for the next kline event
|
||||
#'
|
||||
#' Waits up to `timeout_ms` milliseconds. Returns a named list (`symbol`, `open`,
|
||||
#' `high`, `low`, `close`, `volume`, `open_time`, `is_closed`) when an event
|
||||
#' arrives, or `NULL` on timeout. Errors once the stream is closed.
|
||||
#'
|
||||
#' @keywords internal
|
||||
#' @export
|
||||
binance_next <- function(feed, timeout_ms = 1000) {
|
||||
.Call("wk_binance_next", feed$ptr, as.numeric(timeout_ms), PACKAGE = "wickra")
|
||||
}
|
||||
|
||||
#' Close a Binance feed
|
||||
#'
|
||||
#' @keywords internal
|
||||
#' @export
|
||||
binance_close <- function(feed) {
|
||||
.Call("wk_binance_close", feed$ptr, PACKAGE = "wickra")
|
||||
invisible(NULL)
|
||||
}
|
||||
|
||||
|
||||
@@ -22660,6 +22660,55 @@ SEXP wk_zlema_reset(SEXP e) {
|
||||
return R_NilValue;
|
||||
}
|
||||
|
||||
/* Live Binance kline feed (feature `live-binance`). Gated out of the
|
||||
* Emscripten/wasm build (r-universe/webR), which has no raw TCP/TLS sockets. */
|
||||
#ifndef __EMSCRIPTEN__
|
||||
static void binance_fin(SEXP e) {
|
||||
struct BinanceStream *h = (struct BinanceStream *)R_ExternalPtrAddr(e);
|
||||
if (h) wickra_binance_free(h);
|
||||
R_ClearExternalPtr(e);
|
||||
}
|
||||
SEXP wk_binance_connect(SEXP symbols, SEXP interval, SEXP base_url) {
|
||||
const char *url = (base_url == R_NilValue || Rf_xlength(base_url) == 0)
|
||||
? NULL : CHAR(STRING_ELT(base_url, 0));
|
||||
struct BinanceStream *h = wickra_binance_connect(
|
||||
CHAR(STRING_ELT(symbols, 0)), (uint8_t)Rf_asInteger(interval), url);
|
||||
if (!h) Rf_error("invalid BinanceFeed parameters");
|
||||
SEXP e = PROTECT(R_MakeExternalPtr(h, R_NilValue, R_NilValue));
|
||||
R_RegisterCFinalizerEx(e, binance_fin, TRUE);
|
||||
UNPROTECT(1);
|
||||
return e;
|
||||
}
|
||||
SEXP wk_binance_next(SEXP e, SEXP timeout_ms) {
|
||||
struct BinanceStream *h = (struct BinanceStream *)R_ExternalPtrAddr(e);
|
||||
struct WickraKlineEvent ev;
|
||||
int code = wickra_binance_next(h, &ev, (int64_t)Rf_asReal(timeout_ms));
|
||||
if (code == 0) return R_NilValue;
|
||||
if (code != 1) Rf_error("binance feed closed");
|
||||
const char *names[] = {"symbol", "open", "high", "low", "close", "volume",
|
||||
"open_time", "is_closed"};
|
||||
SEXP out = PROTECT(Rf_allocVector(VECSXP, 8));
|
||||
SET_VECTOR_ELT(out, 0, Rf_mkString((const char *)ev.symbol));
|
||||
SET_VECTOR_ELT(out, 1, Rf_ScalarReal(ev.open));
|
||||
SET_VECTOR_ELT(out, 2, Rf_ScalarReal(ev.high));
|
||||
SET_VECTOR_ELT(out, 3, Rf_ScalarReal(ev.low));
|
||||
SET_VECTOR_ELT(out, 4, Rf_ScalarReal(ev.close));
|
||||
SET_VECTOR_ELT(out, 5, Rf_ScalarReal(ev.volume));
|
||||
SET_VECTOR_ELT(out, 6, Rf_ScalarReal((double)ev.open_time));
|
||||
SET_VECTOR_ELT(out, 7, Rf_ScalarLogical(ev.is_closed));
|
||||
SEXP nm = PROTECT(Rf_allocVector(STRSXP, 8));
|
||||
for (int i = 0; i < 8; i++) SET_STRING_ELT(nm, i, Rf_mkChar(names[i]));
|
||||
Rf_setAttrib(out, R_NamesSymbol, nm);
|
||||
UNPROTECT(2);
|
||||
return out;
|
||||
}
|
||||
SEXP wk_binance_close(SEXP e) {
|
||||
struct BinanceStream *h = (struct BinanceStream *)R_ExternalPtrAddr(e);
|
||||
wickra_binance_close(h);
|
||||
return R_NilValue;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const R_CallMethodDef CallEntries[] = {
|
||||
{"wk_abandoned_baby_new", (DL_FUNC)&wk_abandoned_baby_new, 0},
|
||||
{"wk_abandoned_baby_update", (DL_FUNC)&wk_abandoned_baby_update, 7},
|
||||
@@ -26088,6 +26137,11 @@ static const R_CallMethodDef CallEntries[] = {
|
||||
{"wk_zlema_is_ready", (DL_FUNC)&wk_zlema_is_ready, 1},
|
||||
{"wk_zlema_name", (DL_FUNC)&wk_zlema_name, 1},
|
||||
{"wk_zlema_reset", (DL_FUNC)&wk_zlema_reset, 1},
|
||||
#ifndef __EMSCRIPTEN__
|
||||
{"wk_binance_connect", (DL_FUNC)&wk_binance_connect, 3},
|
||||
{"wk_binance_next", (DL_FUNC)&wk_binance_next, 2},
|
||||
{"wk_binance_close", (DL_FUNC)&wk_binance_close, 1},
|
||||
#endif
|
||||
{NULL, NULL, 0}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# The live Binance feed's connect -> read -> reconnect pipeline is covered
|
||||
# deterministically by the Rust mock-WS-server tests in wickra-data. Here we only
|
||||
# assert the binding's error paths, which need no network.
|
||||
|
||||
test_that("binance feed rejects bad parameters", {
|
||||
expect_error(BinanceFeed("", 1L), "BinanceFeed")
|
||||
expect_error(BinanceFeed("BTCUSDT", 1L, "ws://127.0.0.1:1"), "BinanceFeed")
|
||||
})
|
||||
Reference in New Issue
Block a user