examples: move Node examples into a top-level examples/node/
Continue the per-language `examples/<lang>/` restructure: move the three
Node example files (streaming.js, backtest.js, live_trading.js) out of
bindings/node/examples/ and into a top-level examples/node/ directory.
* `examples/node/package.json` is a `private` package that pulls the
native binding via `file:../../bindings/node` and lists `ws` as a
dev-dependency for the live-trading example. `require('..')` in each
file becomes `require('wickra')` — exactly what a downstream user would
write — and the file-header run instructions are updated to the new
two-step workflow (`npm install` in bindings/node, then in
examples/node).
* `backtest.js`'s default-CSV path becomes the much shorter
`__dirname/../data/btcusdt-1d.csv` from the new location.
* `bindings/node/package.json` drops the now-unused `ws` devDependency.
* `.gitignore` is broadened from `bindings/node/node_modules/` to
`**/node_modules/` so the new `examples/node/node_modules/` directory is
not tracked.
* The README "Languages" table, project-layout block and
`examples/README.md` Node section are updated for the new paths and run
commands.
Verified by running `node backtest.js` (3200 BTCUSDT daily bars, matching
output), `node streaming.js`, and `node --check live_trading.js` from the
new location.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// Streaming indicators with the Wickra Node binding.
|
||||
//
|
||||
// Feeds a synthetic price series through several indicators tick by tick —
|
||||
// the same O(1)-per-update model a live trading bot would use — and prints a
|
||||
// status line whenever every indicator has warmed up.
|
||||
//
|
||||
// Run it from the repository after building the native binding:
|
||||
//
|
||||
// cd bindings/node && npm install && npx napi build --platform --release
|
||||
// cd ../../examples/node && npm install
|
||||
// node streaming.js
|
||||
|
||||
const wickra = require('wickra');
|
||||
|
||||
// A deterministic synthetic series: a slow trend with two oscillations and a
|
||||
// little noise from a seeded generator (so every run prints the same thing).
|
||||
function makeSeries(n) {
|
||||
let seed = 1234567;
|
||||
const rand = () => {
|
||||
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
|
||||
return seed / 0x7fffffff;
|
||||
};
|
||||
const prices = [];
|
||||
for (let t = 0; t < n; t++) {
|
||||
const price =
|
||||
100 +
|
||||
t * 0.05 +
|
||||
Math.sin(t * 0.07) * 8 +
|
||||
Math.cos(t * 0.21) * 3 +
|
||||
(rand() - 0.5);
|
||||
prices.push(price);
|
||||
}
|
||||
return prices;
|
||||
}
|
||||
|
||||
const fmt = (v) =>
|
||||
v === null || v === undefined || Number.isNaN(v) ? ' -- ' : v.toFixed(2);
|
||||
|
||||
function main() {
|
||||
console.log(`Wickra ${wickra.version()} — streaming indicator demo\n`);
|
||||
|
||||
const sma = new wickra.SMA(20);
|
||||
const ema = new wickra.EMA(20);
|
||||
const rsi = new wickra.RSI(14);
|
||||
const macd = new wickra.MACD(12, 26, 9);
|
||||
|
||||
const prices = makeSeries(120);
|
||||
let signals = 0;
|
||||
|
||||
for (let t = 0; t < prices.length; t++) {
|
||||
const price = prices[t];
|
||||
const smaV = sma.update(price);
|
||||
const emaV = ema.update(price);
|
||||
const rsiV = rsi.update(price);
|
||||
const macdV = macd.update(price); // { macd, signal, histogram } or null
|
||||
|
||||
// Only act once every indicator has produced a value.
|
||||
if (smaV == null || emaV == null || rsiV == null || macdV == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const overbought = rsiV > 70 && macdV.histogram < 0;
|
||||
const oversold = rsiV < 30 && macdV.histogram > 0;
|
||||
const tag = overbought ? 'SELL?' : oversold ? 'BUY? ' : ' ';
|
||||
if (overbought || oversold) signals++;
|
||||
|
||||
console.log(
|
||||
`t=${String(t).padStart(3)} price=${fmt(price)} ` +
|
||||
`sma=${fmt(smaV)} ema=${fmt(emaV)} rsi=${fmt(rsiV)} ` +
|
||||
`macd_hist=${fmt(macdV.histogram)} ${tag}`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`\nDone — ${signals} candidate signal(s) over ${prices.length} ticks.`);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user