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:
kingchenc
2026-06-17 01:49:11 +02:00
committed by GitHub
parent 2ae76bb90e
commit 677ea37402
40 changed files with 576 additions and 1102 deletions
+11 -25
View File
@@ -44,40 +44,26 @@
<script type="module">
import init, {
CandleReader,
version, installPanicHook,
SMA, EMA, RSI, MACD, BollingerBands, ATR, ADX, OBV,
} from "../../bindings/wasm/pkg/wickra_wasm.js";
const REQUIRED = ["timestamp", "open", "high", "low", "close", "volume"];
// Parse a plain OHLCV CSV — the Wickra layout never quotes values nor
// embeds commas in fields, so split-on-comma is a complete parse.
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);
// 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 (let i = 1; i < lines.length; i++) {
const cells = lines[i].split(",");
for (const c of REQUIRED) {
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);
}
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;
}
+26 -45
View File
@@ -46,38 +46,25 @@
<script type="module">
import init, {
CandleReader, Resampler,
version, installPanicHook,
RSI, MACD, ADX,
} from "../../bindings/wasm/pkg/wickra_wasm.js";
const REQUIRED = ["timestamp", "open", "high", "low", "close", "volume"];
const ONE_MINUTE_MS = 60_000;
function parseCsv(text) {
const lines = text.split(/\r?\n/).filter((l) => l.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);
// 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 (let i = 1; i < lines.length; i++) {
const cells = lines[i].split(",");
for (const c of REQUIRED) {
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);
}
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;
}
@@ -88,29 +75,23 @@
// construction).
function resample(cols, bucketMs) {
if (cols.timestamp.length === 0) throw new Error("resample: empty series");
// Native Resampler — no hand-written bucketing. update() returns a closed
// candle on a bucket boundary; flush() yields the final partial bucket.
const r = new Resampler(bucketMs);
const out = { timestamp: [], open: [], high: [], low: [], close: [], volume: [] };
let bucketStart = Math.floor(cols.timestamp[0] / bucketMs) * bucketMs;
let o = cols.open[0], h = cols.high[0], l = cols.low[0],
c = cols.close[0], v = cols.volume[0];
for (let i = 1; i < cols.timestamp.length; i++) {
const start = Math.floor(cols.timestamp[i] / bucketMs) * bucketMs;
if (start === bucketStart) {
if (cols.high[i] > h) h = cols.high[i];
if (cols.low[i] < l) l = cols.low[i];
c = cols.close[i];
v += cols.volume[i];
} else {
out.timestamp.push(bucketStart);
out.open.push(o); out.high.push(h); out.low.push(l);
out.close.push(c); out.volume.push(v);
bucketStart = start;
o = cols.open[i]; h = cols.high[i]; l = cols.low[i];
c = cols.close[i]; v = cols.volume[i];
}
const push = (k) => {
out.timestamp.push(k.timestamp);
out.open.push(k.open); out.high.push(k.high); out.low.push(k.low);
out.close.push(k.close); out.volume.push(k.volume);
};
for (let i = 0; i < cols.timestamp.length; i++) {
const k = r.update(cols.open[i], cols.high[i], cols.low[i],
cols.close[i], cols.volume[i], cols.timestamp[i]);
if (k !== null) push(k);
}
out.timestamp.push(bucketStart);
out.open.push(o); out.high.push(h); out.low.push(l);
out.close.push(c); out.volume.push(v);
const last = r.flush();
if (last !== null) push(last);
r.free();
return out;
}
+13 -22
View File
@@ -48,7 +48,8 @@
</p>
<script type="module">
import init, { version, installPanicHook, BollingerBands, ATR } from "../../bindings/wasm/pkg/wickra_wasm.js";
import init, {
CandleReader, version, installPanicHook, BollingerBands, ATR } from "../../bindings/wasm/pkg/wickra_wasm.js";
const FEE = 0.001;
const BB_PERIOD = 20;
@@ -56,29 +57,19 @@
const ATR_PERIOD = 14;
const ATR_STOP_MULT = 2.0;
const SQUEEZE_LOOKBACK = 180;
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;
}
+13 -22
View File
@@ -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;
}
+13 -22
View File
@@ -48,37 +48,28 @@
</p>
<script type="module">
import init, { version, installPanicHook, RSI } from "../../bindings/wasm/pkg/wickra_wasm.js";
import init, {
CandleReader, version, installPanicHook, RSI } from "../../bindings/wasm/pkg/wickra_wasm.js";
const FEE = 0.001;
const RSI_PERIOD = 14;
const OVERSOLD = 30.0;
const OVERBOUGHT = 70.0;
const REQUIRED = ["timestamp", "open", "high", "low", "close", "volume"];
// Plain OHLCV CSV — the Wickra layout never quotes values nor embeds commas,
// so split-on-comma is a complete parse.
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;
}