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:
+12
-35
@@ -18,9 +18,6 @@ const path = require('node:path');
|
||||
|
||||
const wickra = require('wickra');
|
||||
|
||||
// The OHLCV columns the default layout requires; the CSV header must name
|
||||
// every one of them (any extra columns are ignored).
|
||||
const REQUIRED_COLUMNS = ['timestamp', 'open', 'high', 'low', 'close', 'volume'];
|
||||
|
||||
// Default dataset: the checked-in BTCUSDT daily candles under the workspace
|
||||
// `examples/data/` directory, resolved relative to this file.
|
||||
@@ -31,42 +28,22 @@ const DEFAULT_CSV = path.join(__dirname, '..', 'data', 'btcusdt-1d.csv');
|
||||
// The Wickra CSV layout is plain numeric — no quoted fields, no embedded
|
||||
// commas — so splitting on `,` is a complete and correct parse for it.
|
||||
function readHistory(csvPath) {
|
||||
// Native CandleReader: validates the header (timestamp,open,high,low,close,
|
||||
// volume), tolerates a UTF-8 BOM and field whitespace, and throws on a
|
||||
// malformed row. No manual CSV parsing.
|
||||
const text = fs.readFileSync(csvPath, 'utf8');
|
||||
const lines = text.split(/\r?\n/).filter((line) => line.length > 0);
|
||||
if (lines.length === 0) {
|
||||
throw new Error(`${csvPath}: file is empty`);
|
||||
}
|
||||
|
||||
const header = lines[0].split(',').map((cell) => cell.trim());
|
||||
const missing = REQUIRED_COLUMNS.filter((col) => !header.includes(col));
|
||||
if (missing.length > 0) {
|
||||
throw new Error(
|
||||
`${csvPath}: CSV header is missing required column(s): ${missing.join(', ')}; ` +
|
||||
`found: ${header.join(', ')}`,
|
||||
);
|
||||
}
|
||||
if (lines.length === 1) {
|
||||
const candles = new wickra.CandleReader(text).read();
|
||||
if (candles.length === 0) {
|
||||
throw new Error(`${csvPath}: CSV has a header but no data rows`);
|
||||
}
|
||||
|
||||
const columnIndex = {};
|
||||
for (const col of REQUIRED_COLUMNS) {
|
||||
columnIndex[col] = header.indexOf(col);
|
||||
}
|
||||
|
||||
const cols = { timestamp: [], open: [], high: [], low: [], close: [], volume: [] };
|
||||
for (let row = 1; row < lines.length; row++) {
|
||||
const cells = lines[row].split(',');
|
||||
for (const col of REQUIRED_COLUMNS) {
|
||||
const raw = cells[columnIndex[col]];
|
||||
const value = raw === undefined ? NaN : Number(raw.trim());
|
||||
if (raw === undefined || raw.trim() === '' || !Number.isFinite(value)) {
|
||||
throw new Error(
|
||||
`${csvPath}: row ${row + 1} column '${col}' is not numeric: ${JSON.stringify(raw)}`,
|
||||
);
|
||||
}
|
||||
cols[col].push(value);
|
||||
}
|
||||
for (const k of candles) {
|
||||
cols.timestamp.push(k.timestamp);
|
||||
cols.open.push(k.open);
|
||||
cols.high.push(k.high);
|
||||
cols.low.push(k.low);
|
||||
cols.close.push(k.close);
|
||||
cols.volume.push(k.volume);
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user