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:
@@ -0,0 +1,20 @@
|
||||
package wickra
|
||||
|
||||
import "testing"
|
||||
|
||||
// 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: a bad interval,
|
||||
// an empty symbol list, and an unreachable endpoint must all surface as an
|
||||
// error rather than a usable handle.
|
||||
func TestBinanceFeedRejectsBadParams(t *testing.T) {
|
||||
if _, err := NewBinanceFeed("BTCUSDT", BinanceInterval(99), ""); err == nil {
|
||||
t.Fatal("expected an error for an unknown interval code")
|
||||
}
|
||||
if _, err := NewBinanceFeed("", OneMinute, ""); err == nil {
|
||||
t.Fatal("expected an error for an empty symbol list")
|
||||
}
|
||||
if _, err := NewBinanceFeed("BTCUSDT", OneMinute, "ws://127.0.0.1:1"); err == nil {
|
||||
t.Fatal("expected an error connecting to an unreachable endpoint")
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,8 @@ typedef struct BetaNeutralSpread BetaNeutralSpread;
|
||||
|
||||
typedef struct BetterVolume BetterVolume;
|
||||
|
||||
typedef struct BinanceStream BinanceStream;
|
||||
|
||||
typedef struct BipowerVariation BipowerVariation;
|
||||
|
||||
typedef struct BodySizePct BodySizePct;
|
||||
@@ -1690,6 +1692,17 @@ typedef struct WickraCandle {
|
||||
int64_t timestamp;
|
||||
} WickraCandle;
|
||||
|
||||
typedef struct WickraKlineEvent {
|
||||
uint8_t symbol[16];
|
||||
double open;
|
||||
double high;
|
||||
double low;
|
||||
double close;
|
||||
double volume;
|
||||
int64_t open_time;
|
||||
bool is_closed;
|
||||
} WickraKlineEvent;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
@@ -13748,6 +13761,18 @@ uintptr_t wickra_candle_reader_read(struct CandleReader *handle,
|
||||
|
||||
void wickra_candle_reader_free(struct CandleReader *handle);
|
||||
|
||||
struct BinanceStream *wickra_binance_connect(const char *symbols,
|
||||
uint8_t interval,
|
||||
const char *base_url);
|
||||
|
||||
int32_t wickra_binance_next(struct BinanceStream *handle,
|
||||
struct WickraKlineEvent *out,
|
||||
int64_t timeout_ms);
|
||||
|
||||
void wickra_binance_close(struct BinanceStream *handle);
|
||||
|
||||
void wickra_binance_free(struct BinanceStream *handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
@@ -3,12 +3,14 @@
|
||||
package wickra
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include "wickra.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -43319,3 +43321,113 @@ func (ind *Zlema) Close() {
|
||||
runtime.SetFinalizer(ind, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Live Binance kline feed (feature `live-binance`) =====
|
||||
|
||||
// BinanceInterval selects a kline interval (the Interval declaration order).
|
||||
type BinanceInterval uint8
|
||||
|
||||
// Kline intervals supported by the live Binance feed.
|
||||
const (
|
||||
OneSecond BinanceInterval = iota
|
||||
OneMinute
|
||||
ThreeMinutes
|
||||
FiveMinutes
|
||||
FifteenMinutes
|
||||
ThirtyMinutes
|
||||
OneHour
|
||||
TwoHours
|
||||
FourHours
|
||||
SixHours
|
||||
EightHours
|
||||
TwelveHours
|
||||
OneDay
|
||||
ThreeDays
|
||||
OneWeek
|
||||
OneMonth
|
||||
)
|
||||
|
||||
// KlineEvent is one event from the live Binance feed.
|
||||
type KlineEvent struct {
|
||||
Symbol string
|
||||
Open float64
|
||||
High float64
|
||||
Low float64
|
||||
Close float64
|
||||
Volume float64
|
||||
OpenTime int64
|
||||
IsClosed bool
|
||||
}
|
||||
|
||||
// BinanceFeed is a live Binance kline stream over the Wickra C ABI.
|
||||
type BinanceFeed struct {
|
||||
handle *C.struct_BinanceStream
|
||||
}
|
||||
|
||||
// NewBinanceFeed connects to Binance's live kline stream for the given
|
||||
// comma-separated symbols (case-insensitive) at interval. baseURL overrides
|
||||
// the endpoint ("" = production wss://stream.binance.com:9443; pass a ws://
|
||||
// URL to target a test server). It returns ErrInvalidParams on a bad symbol
|
||||
// list, an unknown interval, a bad URL, or a failed initial connect.
|
||||
func NewBinanceFeed(symbols string, interval BinanceInterval, baseURL string) (*BinanceFeed, error) {
|
||||
csym := C.CString(symbols)
|
||||
defer C.free(unsafe.Pointer(csym))
|
||||
var curl *C.char
|
||||
if baseURL != "" {
|
||||
curl = C.CString(baseURL)
|
||||
defer C.free(unsafe.Pointer(curl))
|
||||
}
|
||||
ptr := C.wickra_binance_connect(csym, C.uint8_t(interval), curl)
|
||||
if ptr == nil {
|
||||
return nil, ErrInvalidParams
|
||||
}
|
||||
obj := &BinanceFeed{handle: ptr}
|
||||
runtime.SetFinalizer(obj, (*BinanceFeed).Close)
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// Next polls for the next kline event, waiting up to timeout. It returns the
|
||||
// event and true when one arrives, the zero value and false on timeout, or
|
||||
// ErrFeedClosed once the stream is closed or has errored out.
|
||||
func (f *BinanceFeed) Next(timeout time.Duration) (KlineEvent, bool, error) {
|
||||
var ev C.struct_WickraKlineEvent
|
||||
code := int(C.wickra_binance_next(f.handle, &ev, C.int64_t(timeout.Milliseconds())))
|
||||
runtime.KeepAlive(f)
|
||||
switch code {
|
||||
case 1:
|
||||
return klineFromC(&ev), true, nil
|
||||
case 0:
|
||||
return KlineEvent{}, false, nil
|
||||
default:
|
||||
return KlineEvent{}, false, ErrFeedClosed
|
||||
}
|
||||
}
|
||||
|
||||
func klineFromC(ev *C.struct_WickraKlineEvent) KlineEvent {
|
||||
n := 0
|
||||
for n < len(ev.symbol) && ev.symbol[n] != 0 {
|
||||
n++
|
||||
}
|
||||
sym := C.GoBytes(unsafe.Pointer(&ev.symbol[0]), C.int(n))
|
||||
return KlineEvent{
|
||||
Symbol: string(sym),
|
||||
Open: float64(ev.open),
|
||||
High: float64(ev.high),
|
||||
Low: float64(ev.low),
|
||||
Close: float64(ev.close),
|
||||
Volume: float64(ev.volume),
|
||||
OpenTime: int64(ev.open_time),
|
||||
IsClosed: bool(ev.is_closed),
|
||||
}
|
||||
}
|
||||
|
||||
// Close ends the stream and frees the native handle. Idempotent and safe to
|
||||
// call alongside the finalizer.
|
||||
func (f *BinanceFeed) Close() {
|
||||
if f.handle != nil {
|
||||
C.wickra_binance_close(f.handle)
|
||||
C.wickra_binance_free(f.handle)
|
||||
f.handle = nil
|
||||
runtime.SetFinalizer(f, nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,3 +28,7 @@ import "errors"
|
||||
// ErrInvalidParams is returned by a New<Indicator> constructor when the native
|
||||
// constructor rejects the supplied parameters (for example a zero period).
|
||||
var ErrInvalidParams = errors.New("wickra: invalid indicator parameters")
|
||||
|
||||
// ErrFeedClosed is returned by BinanceFeed.Next once the stream has been closed
|
||||
// or has errored out and exhausted its reconnect attempts.
|
||||
var ErrFeedClosed = errors.New("wickra: binance feed closed")
|
||||
|
||||
Reference in New Issue
Block a user