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:
@@ -1,54 +1,42 @@
|
||||
// Stream live BTCUSDT 1-minute klines from Binance and feed each close through EMA(20).
|
||||
// Requires network access (build-only in CI). Runs for up to 60 seconds.
|
||||
// Uses Wickra's native BinanceFeed — no third-party WebSocket client. Requires
|
||||
// network access (build-only in CI). Runs for up to 60 seconds.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
wickra "github.com/wickra-lib/wickra/bindings/go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const url = "wss://stream.binance.com:9443/ws/btcusdt@kline_1m"
|
||||
fmt.Printf("Connecting to %s (up to 60s)...\n", url)
|
||||
fmt.Println("Streaming live BTCUSDT 1-minute klines from Binance (up to 60s)...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
conn, _, err := websocket.Dial(ctx, url, nil)
|
||||
// Native feed: a blocking poll over the same tested stream as the Rust core.
|
||||
feed, err := wickra.NewBinanceFeed("BTCUSDT", wickra.OneMinute, "")
|
||||
if err != nil {
|
||||
log.Fatalf("dial: %v", err)
|
||||
log.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
defer feed.Close()
|
||||
|
||||
ema, _ := wickra.NewEma(20)
|
||||
defer ema.Close()
|
||||
|
||||
for {
|
||||
_, data, err := conn.Read(ctx)
|
||||
deadline := time.Now().Add(60 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
// next() returns the event and ok=true, ok=false on timeout (poll again),
|
||||
// or an error once the stream is closed.
|
||||
event, ok, err := feed.Next(time.Second)
|
||||
if err != nil {
|
||||
fmt.Println("Done (time limit reached).")
|
||||
fmt.Println("Done (feed closed).")
|
||||
return
|
||||
}
|
||||
|
||||
var msg struct {
|
||||
K struct {
|
||||
Close string `json:"c"`
|
||||
} `json:"k"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &msg); err != nil || msg.K.Close == "" {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
closePx, err := strconv.ParseFloat(msg.K.Close, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("close=%.2f EMA(20)=%.2f\n", closePx, ema.Update(closePx))
|
||||
fmt.Printf("close=%.2f EMA(20)=%.2f\n", event.Close, ema.Update(event.Close))
|
||||
}
|
||||
fmt.Println("Done (time limit reached).")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user