2026-06-09 19:18:40 +02:00
|
|
|
# Stream live BTCUSDT 1-minute klines from Binance and feed each close through EMA(20).
|
2026-06-17 01:49:11 +02:00
|
|
|
# Uses Wickra's native BinanceFeed — no third-party packages. Requires network
|
|
|
|
|
# access. Runs for up to ~60 seconds.
|
2026-06-09 19:18:40 +02:00
|
|
|
library(wickra)
|
2026-06-17 01:49:11 +02:00
|
|
|
|
2026-06-09 19:18:40 +02:00
|
|
|
ema <- Ema(20)
|
2026-06-17 01:49:11 +02:00
|
|
|
# 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")
|