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:
+17
-14
@@ -1,17 +1,20 @@
|
||||
# Stream live BTCUSDT 1-minute klines from Binance and feed each close through EMA(20).
|
||||
# Requires network access and the 'websocket' + 'jsonlite' packages. Runs ~60s.
|
||||
# Uses Wickra's native BinanceFeed — no third-party packages. Requires network
|
||||
# access. Runs for up to ~60 seconds.
|
||||
library(wickra)
|
||||
for (p in c("websocket", "jsonlite", "later")) {
|
||||
if (!requireNamespace(p, quietly = TRUE)) stop(sprintf("install '%s' to run this example", p))
|
||||
}
|
||||
|
||||
ema <- Ema(20)
|
||||
ws <- websocket::WebSocket$new("wss://stream.binance.com:9443/ws/btcusdt@kline_1m")
|
||||
ws$onOpen(function(event) cat("Connected; streaming for up to 60s...\n"))
|
||||
ws$onMessage(function(event) {
|
||||
msg <- jsonlite::fromJSON(event$data)
|
||||
close <- as.numeric(msg$k$c)
|
||||
cat(sprintf("close=%.2f EMA(20)=%.2f\n", close, update(ema, close)))
|
||||
})
|
||||
ws$connect()
|
||||
later::later(function() ws$close(), 60)
|
||||
while (ws$readyState() <= 1L) later::run_now(timeout = 1)
|
||||
# Interval code 1 == 1m (the Interval declaration order shared by every binding).
|
||||
feed <- BinanceFeed("BTCUSDT", 1L)
|
||||
cat("Streaming for up to 60s...\n")
|
||||
|
||||
deadline <- Sys.time() + 60
|
||||
repeat {
|
||||
if (Sys.time() > deadline) break
|
||||
# binance_next() returns a named list on an event, or NULL on timeout.
|
||||
event <- binance_next(feed, 1000)
|
||||
if (is.null(event)) next
|
||||
cat(sprintf("close=%.2f EMA(20)=%.2f\n", event$close, update(ema, event$close)))
|
||||
}
|
||||
binance_close(feed)
|
||||
cat("Done (time limit reached).\n")
|
||||
|
||||
Reference in New Issue
Block a user