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:
@@ -47,33 +47,24 @@
|
||||
</p>
|
||||
|
||||
<script type="module">
|
||||
import init, { version, installPanicHook, MACD, ADX } from "../../bindings/wasm/pkg/wickra_wasm.js";
|
||||
import init, {
|
||||
CandleReader, version, installPanicHook, MACD, ADX } from "../../bindings/wasm/pkg/wickra_wasm.js";
|
||||
|
||||
const FEE = 0.001;
|
||||
const ADX_FLOOR = 20.0;
|
||||
const REQUIRED = ["timestamp", "open", "high", "low", "close", "volume"];
|
||||
|
||||
function parseCsv(text) {
|
||||
const lines = text.split(/\r?\n/).filter((line) => line.length > 0);
|
||||
if (lines.length === 0) throw new Error("file is empty");
|
||||
const header = lines[0].split(",").map((s) => s.trim());
|
||||
const missing = REQUIRED.filter((c) => !header.includes(c));
|
||||
if (missing.length > 0) {
|
||||
throw new Error(`missing required column(s): ${missing.join(", ")}; found: ${header.join(", ")}`);
|
||||
}
|
||||
if (lines.length === 1) throw new Error("CSV has a header but no data rows");
|
||||
const idx = {};
|
||||
for (const c of REQUIRED) idx[c] = header.indexOf(c);
|
||||
const cols = { open: [], high: [], low: [], close: [], volume: [] };
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
const cells = lines[i].split(",");
|
||||
for (const c of ["open", "high", "low", "close", "volume"]) {
|
||||
const v = Number(cells[idx[c]]);
|
||||
if (!Number.isFinite(v)) {
|
||||
throw new Error(`row ${i + 1} column '${c}' is not numeric: ${JSON.stringify(cells[idx[c]])}`);
|
||||
}
|
||||
cols[c].push(v);
|
||||
}
|
||||
// Native CandleReader: header validation, BOM/whitespace tolerance. No
|
||||
// manual CSV parsing.
|
||||
const candles = new CandleReader(text).read();
|
||||
const cols = { timestamp: [], open: [], high: [], low: [], close: [], volume: [] };
|
||||
for (const c of candles) {
|
||||
cols.timestamp.push(c.timestamp);
|
||||
cols.open.push(c.open);
|
||||
cols.high.push(c.high);
|
||||
cols.low.push(c.low);
|
||||
cols.close.push(c.close);
|
||||
cols.volume.push(c.volume);
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user