@reefclaw/connect 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/bridge/bridge.js +46 -3
- package/assets/bridge/utils/skill-version.d.ts +9 -0
- package/assets/bridge/utils/skill-version.js +45 -1
- package/assets/plugin/ccxt/binance-ban-gate.js +3 -1
- package/assets/plugin/ccxt/binance-public.d.ts +17 -1
- package/assets/plugin/ccxt/binance-public.js +34 -1
- package/assets/plugin/ccxt/intel-public.d.ts +25 -0
- package/assets/plugin/ccxt/intel-public.js +80 -0
- package/assets/plugin/ccxt/public-market-data-api.d.ts +12 -0
- package/assets/plugin/ccxt/public-market-data-api.js +9 -0
- package/assets/plugin/config/plugin-config-io.d.ts +15 -0
- package/assets/plugin/index.js +117 -7
- package/assets/plugin/ingest/position-auto-capture.d.ts +5 -0
- package/assets/plugin/ingest/position-auto-capture.js +8 -2
- package/assets/plugin/ingest/position-decisions-client.d.ts +4 -0
- package/assets/plugin/ingest/readiness-reporter.d.ts +28 -5
- package/assets/plugin/ingest/readiness-reporter.js +40 -19
- package/assets/plugin/live/bracket-id.d.ts +9 -0
- package/assets/plugin/live/bracket-id.js +18 -0
- package/assets/plugin/tools/close-position.d.ts +2 -2
- package/assets/plugin/tools/create-order.d.ts +2 -2
- package/assets/plugin/tools/fetch-ohlcv.d.ts +2 -2
- package/assets/plugin/tools/fetch-ticker.d.ts +2 -2
- package/assets/plugin/tools/get-crypto-metrics.d.ts +2 -2
- package/assets/plugin/tools/get-market-structure.d.ts +2 -2
- package/assets/plugin/tools/get-orderbook.d.ts +2 -2
- package/assets/plugin/tools/get-volume-analysis.d.ts +2 -2
- package/assets/plugin/tools/helpers.d.ts +5 -4
- package/assets/plugin/tools/helpers.js +2 -1
- package/assets/plugin/types.d.ts +20 -1
- package/assets/plugin/venues/hyperliquid/hl-public.d.ts +52 -0
- package/assets/plugin/venues/hyperliquid/hl-public.js +285 -0
- package/assets/plugin/venues/registry.d.ts +19 -0
- package/assets/plugin/venues/registry.js +40 -0
- package/assets/shared/fills.d.ts +5 -1
- package/assets/shared/index.d.ts +4 -0
- package/assets/shared/index.js +2 -0
- package/assets/shared/readiness.d.ts +6 -1
- package/assets/shared/readiness.js +9 -0
- package/assets/shared/venues/symbols.d.ts +43 -0
- package/assets/shared/venues/symbols.js +123 -0
- package/assets/skill/SKILL.md +6 -0
- package/dist/cli.js +36 -1
- package/dist/validate.js +33 -1
- package/package.json +1 -1
- package/assets/shared/signals/indicators-extended.d.ts +0 -52
- package/assets/shared/signals/indicators-extended.js +0 -284
- package/assets/shared/signals/indicators.d.ts +0 -15
- package/assets/shared/signals/indicators.js +0 -107
- package/dist/daemon.js +0 -104
package/dist/cli.js
CHANGED
|
@@ -11,12 +11,28 @@ import { installPlugin } from './plugin.js';
|
|
|
11
11
|
import { installBridge } from './bridge.js';
|
|
12
12
|
import { installSkill } from './skill.js';
|
|
13
13
|
import { enableConnectorSupervisor } from './supervisor-config.js';
|
|
14
|
-
import { checkGateway } from './validate.js';
|
|
14
|
+
import { checkGateway, checkBinanceRegion } from './validate.js';
|
|
15
15
|
import { readConfig, writeConfig, mergeReefClawConfig, gatewayAuthDowngradeNeeded, openClawInstalled, openClawConfigPath, readGatewayPort, } from './openclaw.js';
|
|
16
16
|
import { run, which } from './exec.js';
|
|
17
17
|
import { step, ok, info, warn, fail, banner, bold, green, cyan, dim } from './ui.js';
|
|
18
18
|
const DASHBOARD = 'https://www.reefclaw.com/dashboard';
|
|
19
19
|
const ONBOARDING = 'https://www.reefclaw.com/onboarding';
|
|
20
|
+
const OPENCLAW_FLOOR = [2026, 6, 0];
|
|
21
|
+
/** Parse a `2026.6.11`-style version out of `openclaw --version` output. */
|
|
22
|
+
function parseOpenClawVersion(out) {
|
|
23
|
+
const m = out.match(/(\d{4})\.(\d+)\.(\d+)/);
|
|
24
|
+
return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
|
|
25
|
+
}
|
|
26
|
+
/** True when `v` >= `floor` (calendar-semver tuple compare). */
|
|
27
|
+
function meetsFloor(v, floor) {
|
|
28
|
+
for (let i = 0; i < 3; i += 1) {
|
|
29
|
+
if (v[i] > floor[i])
|
|
30
|
+
return true;
|
|
31
|
+
if (v[i] < floor[i])
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
20
36
|
function preflight() {
|
|
21
37
|
const major = Number(process.versions.node.split('.')[0]);
|
|
22
38
|
if (Number.isFinite(major) && major < 20) {
|
|
@@ -29,6 +45,18 @@ function preflight() {
|
|
|
29
45
|
info(`(If your config lives elsewhere, set OPENCLAW_CONFIG_PATH. Looked at: ${openClawConfigPath()})`);
|
|
30
46
|
process.exit(1);
|
|
31
47
|
}
|
|
48
|
+
// OpenClaw must be new enough to load the plugin. 2026.4's install scanner
|
|
49
|
+
// hard-blocks the plugin and its plugin-API floor is below ours (>=2026.6.0),
|
|
50
|
+
// so proceeding would only fail cryptically at install time. Fail fast — but
|
|
51
|
+
// ONLY on a version we could parse and confirm is too old; a missing CLI or
|
|
52
|
+
// unrecognised output stays out of the way (the loader still guards it).
|
|
53
|
+
const verOut = which('openclaw') ? run('openclaw', ['--version'], { timeoutMs: 10_000 }) : null;
|
|
54
|
+
const parsed = verOut?.ok ? parseOpenClawVersion(verOut.stdout) : null;
|
|
55
|
+
if (parsed && !meetsFloor(parsed, OPENCLAW_FLOOR)) {
|
|
56
|
+
fail(`OpenClaw ${parsed.join('.')} is too old — ReefClaw needs ${OPENCLAW_FLOOR.join('.')} or newer.`);
|
|
57
|
+
info('Update OpenClaw, then re-run: npm i -g openclaw@latest');
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
32
60
|
}
|
|
33
61
|
/** Re-write openclaw.json: preserve the user's existing config, overlay the
|
|
34
62
|
* ENTIRE plugins section the link wrote (the link can wipe other sections, so
|
|
@@ -91,6 +119,10 @@ function nextSteps() {
|
|
|
91
119
|
async function main() {
|
|
92
120
|
banner(bold(cyan('ReefClaw connect')) + dim(' — link your OpenClaw agent to ReefClaw (paper trading)'));
|
|
93
121
|
preflight();
|
|
122
|
+
// Prevention: a host in a Binance-restricted region (HTTP 451) can't trade —
|
|
123
|
+
// not even paper, which uses live Binance prices. Advisory (never fatal); the
|
|
124
|
+
// dashboard readiness surface re-checks it live after connect.
|
|
125
|
+
const binanceReachable = await checkBinanceRegion();
|
|
94
126
|
const pre = readConfig();
|
|
95
127
|
const plugin = installPlugin();
|
|
96
128
|
const merged = wireConfig(pre);
|
|
@@ -115,6 +147,9 @@ async function main() {
|
|
|
115
147
|
if (!supervised) {
|
|
116
148
|
warn('Connector supervision was not enabled — see the message above to finish it.');
|
|
117
149
|
}
|
|
150
|
+
if (!binanceReachable) {
|
|
151
|
+
warn('This host looks geo-blocked by Binance (HTTP 451) — trading will not work until you run the agent from a Binance-permitted region (most EU / several Asia VPS regions).');
|
|
152
|
+
}
|
|
118
153
|
nextSteps();
|
|
119
154
|
}
|
|
120
155
|
main().catch((err) => {
|
package/dist/validate.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Light post-install checks. We can only verify the LOCAL gateway here — the
|
|
2
2
|
// relay handshake only succeeds once the user pastes their connect message
|
|
3
3
|
// (that's the onboarding "auto-detect" step on the dashboard).
|
|
4
|
-
import { step, ok, warn, info } from './ui.js';
|
|
4
|
+
import { step, ok, warn, info, fail } from './ui.js';
|
|
5
5
|
/** Any HTTP response from the local gateway means OpenClaw is up and reachable.
|
|
6
6
|
* A connection refused means OpenClaw isn't running. */
|
|
7
7
|
export async function checkGateway(port) {
|
|
@@ -26,3 +26,35 @@ export async function checkGateway(port) {
|
|
|
26
26
|
clearTimeout(t);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
/** Probe whether Binance USD-M Futures is reachable from THIS host. A `451` is
|
|
30
|
+
* Binance's geo-restriction signal — the host's region is blocked, and trading
|
|
31
|
+
* (even paper mode, which uses live Binance prices) cannot work from here. The
|
|
32
|
+
* probe is advisory: only a definitive `451` returns false; a network hiccup /
|
|
33
|
+
* timeout is NOT a geo-block, so we stay out of the way and let the dashboard
|
|
34
|
+
* readiness surface confirm a real, persistent problem later. Never fatal. */
|
|
35
|
+
export async function checkBinanceRegion() {
|
|
36
|
+
step('Checking Binance reachability from this host');
|
|
37
|
+
const url = 'https://fapi.binance.com/fapi/v1/ping';
|
|
38
|
+
const ctrl = new AbortController();
|
|
39
|
+
const t = setTimeout(() => ctrl.abort(), 5000);
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(url, { signal: ctrl.signal });
|
|
42
|
+
if (res.status === 451) {
|
|
43
|
+
fail('Binance returned HTTP 451 — this host is in a Binance-restricted region.');
|
|
44
|
+
info('Trading (and paper mode, which uses live Binance prices) cannot work from here.');
|
|
45
|
+
info('Run the agent from a Binance-permitted region — most EU / several Asia VPS regions work.');
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
ok('Binance is reachable from this host');
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// DNS / timeout / transient network — not a definitive geo-block. Don't
|
|
53
|
+
// block the install on a flaky probe.
|
|
54
|
+
info('Could not probe Binance reachability (network hiccup) — skipping; the dashboard verifies it after connect.');
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
clearTimeout(t);
|
|
59
|
+
}
|
|
60
|
+
}
|
package/package.json
CHANGED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export interface OhlcvInput {
|
|
2
|
-
open: number;
|
|
3
|
-
high: number;
|
|
4
|
-
low: number;
|
|
5
|
-
close: number;
|
|
6
|
-
volume: number;
|
|
7
|
-
}
|
|
8
|
-
export interface MACDResult {
|
|
9
|
-
line: number;
|
|
10
|
-
signal: number;
|
|
11
|
-
histogram: number;
|
|
12
|
-
crossover: 'bullish' | 'bearish' | 'none';
|
|
13
|
-
}
|
|
14
|
-
export interface BollingerResult {
|
|
15
|
-
upper: number;
|
|
16
|
-
middle: number;
|
|
17
|
-
lower: number;
|
|
18
|
-
bandwidth: number;
|
|
19
|
-
percentB: number;
|
|
20
|
-
}
|
|
21
|
-
export interface StochRSIResult {
|
|
22
|
-
k: number;
|
|
23
|
-
d: number;
|
|
24
|
-
}
|
|
25
|
-
export interface IchimokuResult {
|
|
26
|
-
tenkan: number;
|
|
27
|
-
kijun: number;
|
|
28
|
-
senkouA: number;
|
|
29
|
-
senkouB: number;
|
|
30
|
-
chikou: number;
|
|
31
|
-
cloudPosition: 'above' | 'below' | 'inside';
|
|
32
|
-
}
|
|
33
|
-
export interface SupertrendResult {
|
|
34
|
-
value: number;
|
|
35
|
-
direction: 'bullish' | 'bearish';
|
|
36
|
-
}
|
|
37
|
-
export declare function computeMACD(closes: number[], fast?: number, slow?: number, signal?: number): MACDResult;
|
|
38
|
-
export declare function computeBollingerBands(closes: number[], period?: number, stdDev?: number): BollingerResult;
|
|
39
|
-
export declare function computeVWAP(bars: OhlcvInput[]): number;
|
|
40
|
-
export declare function computeStochRSI(closes: number[], rsiPeriod?: number, stochPeriod?: number, kSmooth?: number, dSmooth?: number): StochRSIResult;
|
|
41
|
-
export declare function computeADX(highs: number[], lows: number[], closes: number[], period?: number): {
|
|
42
|
-
adx: number;
|
|
43
|
-
plusDI: number;
|
|
44
|
-
minusDI: number;
|
|
45
|
-
};
|
|
46
|
-
export declare function computeIchimoku(highs: number[], lows: number[], closes: number[], tenkanPeriod?: number, kijunPeriod?: number, senkouBPeriod?: number): IchimokuResult;
|
|
47
|
-
export declare function computeOBV(closes: number[], volumes: number[]): {
|
|
48
|
-
obv: number;
|
|
49
|
-
slope: 'rising' | 'falling' | 'flat';
|
|
50
|
-
};
|
|
51
|
-
export declare function computeSupertrend(highs: number[], lows: number[], closes: number[], period?: number, multiplier?: number): SupertrendResult;
|
|
52
|
-
export declare function computeWilliamsR(highs: number[], lows: number[], closes: number[], period?: number): number;
|
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
// Extended indicator computations for Phase 13 — Expanded Indicators.
|
|
2
|
-
// Wraps technicalindicators library + custom implementations.
|
|
3
|
-
// All functions take OHLCV arrays (oldest first) and return latest values.
|
|
4
|
-
import { computeATRSeries, computeRSI, mean, computeStd } from './indicators.js';
|
|
5
|
-
// ─── MACD (12, 26, 9) ──────────────────────────────────────────────────
|
|
6
|
-
export function computeMACD(closes, fast = 12, slow = 26, signal = 9) {
|
|
7
|
-
if (closes.length < slow + signal) {
|
|
8
|
-
return { line: 0, signal: 0, histogram: 0, crossover: 'none' };
|
|
9
|
-
}
|
|
10
|
-
// Compute full EMA series (both aligned to start at index `slow - 1`)
|
|
11
|
-
const emaFastSeries = emaSeries(closes, fast);
|
|
12
|
-
const emaSlowSeries = emaSeries(closes, slow);
|
|
13
|
-
// Align: fast series starts earlier, so take the tail matching slow series length
|
|
14
|
-
const offset = emaFastSeries.length - emaSlowSeries.length;
|
|
15
|
-
const macdLine = [];
|
|
16
|
-
for (let i = 0; i < emaSlowSeries.length; i++) {
|
|
17
|
-
macdLine.push(emaFastSeries[i + offset] - emaSlowSeries[i]);
|
|
18
|
-
}
|
|
19
|
-
// Signal line = EMA of MACD line
|
|
20
|
-
const signalSeries = emaSeries(macdLine, signal);
|
|
21
|
-
const sigOffset = macdLine.length - signalSeries.length;
|
|
22
|
-
const currentLine = macdLine[macdLine.length - 1];
|
|
23
|
-
const currentSignal = signalSeries[signalSeries.length - 1];
|
|
24
|
-
const prevLine = macdLine.length >= 2 ? macdLine[macdLine.length - 2] : currentLine;
|
|
25
|
-
const prevSignalIdx = signalSeries.length >= 2 ? signalSeries.length - 2 : signalSeries.length - 1;
|
|
26
|
-
const prevSignal = signalSeries[prevSignalIdx];
|
|
27
|
-
let crossover = 'none';
|
|
28
|
-
if (prevLine <= prevSignal && currentLine > currentSignal)
|
|
29
|
-
crossover = 'bullish';
|
|
30
|
-
else if (prevLine >= prevSignal && currentLine < currentSignal)
|
|
31
|
-
crossover = 'bearish';
|
|
32
|
-
return {
|
|
33
|
-
line: currentLine,
|
|
34
|
-
signal: currentSignal,
|
|
35
|
-
histogram: currentLine - currentSignal,
|
|
36
|
-
crossover,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
// ─── Bollinger Bands (20, 2σ) ───────────────────────────────────────────
|
|
40
|
-
export function computeBollingerBands(closes, period = 20, stdDev = 2) {
|
|
41
|
-
if (closes.length < period) {
|
|
42
|
-
const p = closes[closes.length - 1] ?? 0;
|
|
43
|
-
return { upper: p, middle: p, lower: p, bandwidth: 0, percentB: 0.5 };
|
|
44
|
-
}
|
|
45
|
-
const slice = closes.slice(-period);
|
|
46
|
-
const middle = mean(slice);
|
|
47
|
-
const std = computeStd(slice);
|
|
48
|
-
const upper = middle + stdDev * std;
|
|
49
|
-
const lower = middle - stdDev * std;
|
|
50
|
-
const bandwidth = middle > 0 ? ((upper - lower) / middle) * 100 : 0;
|
|
51
|
-
const price = closes[closes.length - 1];
|
|
52
|
-
const percentB = upper !== lower ? (price - lower) / (upper - lower) : 0.5;
|
|
53
|
-
return { upper, middle, lower, bandwidth, percentB };
|
|
54
|
-
}
|
|
55
|
-
// ─── VWAP ───────────────────────────────────────────────────────────────
|
|
56
|
-
export function computeVWAP(bars) {
|
|
57
|
-
if (bars.length === 0)
|
|
58
|
-
return 0;
|
|
59
|
-
let cumVolume = 0;
|
|
60
|
-
let cumTPxVol = 0;
|
|
61
|
-
for (const bar of bars) {
|
|
62
|
-
const tp = (bar.high + bar.low + bar.close) / 3;
|
|
63
|
-
cumVolume += bar.volume;
|
|
64
|
-
cumTPxVol += tp * bar.volume;
|
|
65
|
-
}
|
|
66
|
-
return cumVolume > 0 ? cumTPxVol / cumVolume : bars[bars.length - 1].close;
|
|
67
|
-
}
|
|
68
|
-
// ─── Stochastic RSI (14, 14, 3, 3) ─────────────────────────────────────
|
|
69
|
-
export function computeStochRSI(closes, rsiPeriod = 14, stochPeriod = 14, kSmooth = 3, dSmooth = 3) {
|
|
70
|
-
if (closes.length < rsiPeriod + stochPeriod + dSmooth) {
|
|
71
|
-
return { k: 50, d: 50 };
|
|
72
|
-
}
|
|
73
|
-
// Compute RSI series
|
|
74
|
-
const rsiValues = [];
|
|
75
|
-
for (let i = rsiPeriod + 1; i <= closes.length; i++) {
|
|
76
|
-
rsiValues.push(computeRSI(closes.slice(0, i), rsiPeriod));
|
|
77
|
-
}
|
|
78
|
-
if (rsiValues.length < stochPeriod)
|
|
79
|
-
return { k: 50, d: 50 };
|
|
80
|
-
// Stochastic of RSI
|
|
81
|
-
const rawK = [];
|
|
82
|
-
for (let i = stochPeriod - 1; i < rsiValues.length; i++) {
|
|
83
|
-
const window = rsiValues.slice(i - stochPeriod + 1, i + 1);
|
|
84
|
-
const min = Math.min(...window);
|
|
85
|
-
const max = Math.max(...window);
|
|
86
|
-
rawK.push(max !== min ? ((rsiValues[i] - min) / (max - min)) * 100 : 50);
|
|
87
|
-
}
|
|
88
|
-
// %K = SMA of raw stochastic
|
|
89
|
-
const kValues = sma(rawK, kSmooth);
|
|
90
|
-
// %D = SMA of %K
|
|
91
|
-
const dValues = sma(kValues, dSmooth);
|
|
92
|
-
return {
|
|
93
|
-
k: Math.round(kValues[kValues.length - 1] ?? 50),
|
|
94
|
-
d: Math.round(dValues[dValues.length - 1] ?? 50),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
// ─── ADX (14) ───────────────────────────────────────────────────────────
|
|
98
|
-
export function computeADX(highs, lows, closes, period = 14) {
|
|
99
|
-
if (highs.length < period * 2 + 1) {
|
|
100
|
-
return { adx: 0, plusDI: 0, minusDI: 0 };
|
|
101
|
-
}
|
|
102
|
-
const plusDM = [];
|
|
103
|
-
const minusDM = [];
|
|
104
|
-
const tr = [];
|
|
105
|
-
for (let i = 1; i < highs.length; i++) {
|
|
106
|
-
const upMove = highs[i] - highs[i - 1];
|
|
107
|
-
const downMove = lows[i - 1] - lows[i];
|
|
108
|
-
plusDM.push(upMove > downMove && upMove > 0 ? upMove : 0);
|
|
109
|
-
minusDM.push(downMove > upMove && downMove > 0 ? downMove : 0);
|
|
110
|
-
tr.push(Math.max(highs[i] - lows[i], Math.abs(highs[i] - closes[i - 1]), Math.abs(lows[i] - closes[i - 1])));
|
|
111
|
-
}
|
|
112
|
-
// Smooth with Wilder's smoothing (equivalent to EMA with alpha=1/period)
|
|
113
|
-
const smoothPlusDM = wilderSmooth(plusDM, period);
|
|
114
|
-
const smoothMinusDM = wilderSmooth(minusDM, period);
|
|
115
|
-
const smoothTR = wilderSmooth(tr, period);
|
|
116
|
-
// +DI and -DI series
|
|
117
|
-
const plusDISeries = [];
|
|
118
|
-
const minusDISeries = [];
|
|
119
|
-
for (let i = 0; i < smoothTR.length; i++) {
|
|
120
|
-
plusDISeries.push(smoothTR[i] > 0 ? (smoothPlusDM[i] / smoothTR[i]) * 100 : 0);
|
|
121
|
-
minusDISeries.push(smoothTR[i] > 0 ? (smoothMinusDM[i] / smoothTR[i]) * 100 : 0);
|
|
122
|
-
}
|
|
123
|
-
// DX series
|
|
124
|
-
const dxSeries = [];
|
|
125
|
-
for (let i = 0; i < plusDISeries.length; i++) {
|
|
126
|
-
const sum = plusDISeries[i] + minusDISeries[i];
|
|
127
|
-
dxSeries.push(sum > 0 ? (Math.abs(plusDISeries[i] - minusDISeries[i]) / sum) * 100 : 0);
|
|
128
|
-
}
|
|
129
|
-
// ADX = Wilder smooth of DX
|
|
130
|
-
const adxSeries = wilderSmooth(dxSeries, period);
|
|
131
|
-
return {
|
|
132
|
-
adx: Math.round(adxSeries[adxSeries.length - 1] ?? 0),
|
|
133
|
-
plusDI: Math.round(plusDISeries[plusDISeries.length - 1] ?? 0),
|
|
134
|
-
minusDI: Math.round(minusDISeries[minusDISeries.length - 1] ?? 0),
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
// ─── Ichimoku Cloud ─────────────────────────────────────────────────────
|
|
138
|
-
export function computeIchimoku(highs, lows, closes, tenkanPeriod = 9, kijunPeriod = 26, senkouBPeriod = 52) {
|
|
139
|
-
const n = highs.length;
|
|
140
|
-
if (n < senkouBPeriod) {
|
|
141
|
-
const p = closes[n - 1] ?? 0;
|
|
142
|
-
return { tenkan: p, kijun: p, senkouA: p, senkouB: p, chikou: p, cloudPosition: 'inside' };
|
|
143
|
-
}
|
|
144
|
-
const midpoint = (arr, period, end) => {
|
|
145
|
-
const slice = arr.slice(Math.max(0, end - period + 1), end + 1);
|
|
146
|
-
return (Math.max(...slice) + Math.min(...slice)) / 2;
|
|
147
|
-
};
|
|
148
|
-
const tenkan = midpoint(highs.concat().map((h, i) => Math.max(h, lows[i])), tenkanPeriod, n - 1);
|
|
149
|
-
const kijun = midpoint(highs.concat().map((h, i) => Math.max(h, lows[i])), kijunPeriod, n - 1);
|
|
150
|
-
// Recalculate properly using highs and lows separately
|
|
151
|
-
const tenkanH = Math.max(...highs.slice(-tenkanPeriod));
|
|
152
|
-
const tenkanL = Math.min(...lows.slice(-tenkanPeriod));
|
|
153
|
-
const tenkanVal = (tenkanH + tenkanL) / 2;
|
|
154
|
-
const kijunH = Math.max(...highs.slice(-kijunPeriod));
|
|
155
|
-
const kijunL = Math.min(...lows.slice(-kijunPeriod));
|
|
156
|
-
const kijunVal = (kijunH + kijunL) / 2;
|
|
157
|
-
const senkouA = (tenkanVal + kijunVal) / 2;
|
|
158
|
-
const senkouBH = Math.max(...highs.slice(-senkouBPeriod));
|
|
159
|
-
const senkouBL = Math.min(...lows.slice(-senkouBPeriod));
|
|
160
|
-
const senkouB = (senkouBH + senkouBL) / 2;
|
|
161
|
-
const chikou = closes[n - 1]; // Current close (projected back 26 periods)
|
|
162
|
-
const price = closes[n - 1];
|
|
163
|
-
const cloudTop = Math.max(senkouA, senkouB);
|
|
164
|
-
const cloudBottom = Math.min(senkouA, senkouB);
|
|
165
|
-
const cloudPosition = price > cloudTop ? 'above' : price < cloudBottom ? 'below' : 'inside';
|
|
166
|
-
return { tenkan: tenkanVal, kijun: kijunVal, senkouA, senkouB, chikou, cloudPosition };
|
|
167
|
-
}
|
|
168
|
-
// ─── OBV (On-Balance Volume) ────────────────────────────────────────────
|
|
169
|
-
export function computeOBV(closes, volumes) {
|
|
170
|
-
if (closes.length < 2)
|
|
171
|
-
return { obv: 0, slope: 'flat' };
|
|
172
|
-
let obv = 0;
|
|
173
|
-
const obvSeries = [0];
|
|
174
|
-
for (let i = 1; i < closes.length; i++) {
|
|
175
|
-
if (closes[i] > closes[i - 1])
|
|
176
|
-
obv += volumes[i];
|
|
177
|
-
else if (closes[i] < closes[i - 1])
|
|
178
|
-
obv -= volumes[i];
|
|
179
|
-
obvSeries.push(obv);
|
|
180
|
-
}
|
|
181
|
-
// Slope over last 10 bars
|
|
182
|
-
const lookback = Math.min(10, obvSeries.length);
|
|
183
|
-
const recent = obvSeries.slice(-lookback);
|
|
184
|
-
const first = recent[0];
|
|
185
|
-
const last = recent[recent.length - 1];
|
|
186
|
-
const threshold = Math.abs(first) * 0.01; // 1% threshold
|
|
187
|
-
const slope = last - first > threshold ? 'rising' : last - first < -threshold ? 'falling' : 'flat';
|
|
188
|
-
return { obv, slope };
|
|
189
|
-
}
|
|
190
|
-
// ─── Supertrend (10, 3) ─────────────────────────────────────────────────
|
|
191
|
-
export function computeSupertrend(highs, lows, closes, period = 10, multiplier = 3) {
|
|
192
|
-
const atrSeries = computeATRSeries(highs, lows, closes, period);
|
|
193
|
-
if (atrSeries.length === 0) {
|
|
194
|
-
return { value: closes[closes.length - 1] ?? 0, direction: 'bullish' };
|
|
195
|
-
}
|
|
196
|
-
// ATR series starts at index 1 (needs previous close for TR)
|
|
197
|
-
// Align: atrSeries[i] corresponds to bar index i+1
|
|
198
|
-
let upperBand = 0;
|
|
199
|
-
let lowerBand = 0;
|
|
200
|
-
let supertrend = 0;
|
|
201
|
-
let direction = 'bullish';
|
|
202
|
-
for (let i = 0; i < atrSeries.length; i++) {
|
|
203
|
-
const barIdx = i + 1; // offset for TR calculation
|
|
204
|
-
const hl2 = (highs[barIdx] + lows[barIdx]) / 2;
|
|
205
|
-
const atr = atrSeries[i];
|
|
206
|
-
const basicUpper = hl2 + multiplier * atr;
|
|
207
|
-
const basicLower = hl2 - multiplier * atr;
|
|
208
|
-
upperBand = i > 0 && basicUpper < upperBand && closes[barIdx - 1] > upperBand ? upperBand : basicUpper;
|
|
209
|
-
lowerBand = i > 0 && basicLower > lowerBand && closes[barIdx - 1] < lowerBand ? lowerBand : basicLower;
|
|
210
|
-
if (i === 0) {
|
|
211
|
-
supertrend = closes[barIdx] > upperBand ? lowerBand : upperBand;
|
|
212
|
-
direction = closes[barIdx] > upperBand ? 'bullish' : 'bearish';
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
if (direction === 'bullish') {
|
|
216
|
-
if (closes[barIdx] < lowerBand) {
|
|
217
|
-
direction = 'bearish';
|
|
218
|
-
supertrend = upperBand;
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
supertrend = lowerBand;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
if (closes[barIdx] > upperBand) {
|
|
226
|
-
direction = 'bullish';
|
|
227
|
-
supertrend = lowerBand;
|
|
228
|
-
}
|
|
229
|
-
else {
|
|
230
|
-
supertrend = upperBand;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return { value: supertrend, direction };
|
|
236
|
-
}
|
|
237
|
-
// ─── Williams %R (14) ───────────────────────────────────────────────────
|
|
238
|
-
export function computeWilliamsR(highs, lows, closes, period = 14) {
|
|
239
|
-
if (highs.length < period)
|
|
240
|
-
return -50;
|
|
241
|
-
const recentHighs = highs.slice(-period);
|
|
242
|
-
const recentLows = lows.slice(-period);
|
|
243
|
-
const hh = Math.max(...recentHighs);
|
|
244
|
-
const ll = Math.min(...recentLows);
|
|
245
|
-
const close = closes[closes.length - 1];
|
|
246
|
-
return hh !== ll ? ((hh - close) / (hh - ll)) * -100 : -50;
|
|
247
|
-
}
|
|
248
|
-
// ─── Helper: EMA series ─────────────────────────────────────────────────
|
|
249
|
-
function emaSeries(data, period) {
|
|
250
|
-
if (data.length === 0)
|
|
251
|
-
return [];
|
|
252
|
-
if (data.length < period)
|
|
253
|
-
return [data[data.length - 1]];
|
|
254
|
-
const k = 2 / (period + 1);
|
|
255
|
-
const result = [];
|
|
256
|
-
let ema = mean(data.slice(0, period));
|
|
257
|
-
result.push(ema);
|
|
258
|
-
for (let i = period; i < data.length; i++) {
|
|
259
|
-
ema = data[i] * k + ema * (1 - k);
|
|
260
|
-
result.push(ema);
|
|
261
|
-
}
|
|
262
|
-
return result;
|
|
263
|
-
}
|
|
264
|
-
// ─── Helper: SMA series ─────────────────────────────────────────────────
|
|
265
|
-
function sma(data, period) {
|
|
266
|
-
if (data.length < period)
|
|
267
|
-
return data.length > 0 ? [mean(data)] : [];
|
|
268
|
-
const result = [];
|
|
269
|
-
for (let i = period - 1; i < data.length; i++) {
|
|
270
|
-
result.push(mean(data.slice(i - period + 1, i + 1)));
|
|
271
|
-
}
|
|
272
|
-
return result;
|
|
273
|
-
}
|
|
274
|
-
// ─── Helper: Wilder's smoothing ─────────────────────────────────────────
|
|
275
|
-
function wilderSmooth(data, period) {
|
|
276
|
-
if (data.length < period)
|
|
277
|
-
return [];
|
|
278
|
-
const result = [];
|
|
279
|
-
result.push(mean(data.slice(0, period)));
|
|
280
|
-
for (let i = period; i < data.length; i++) {
|
|
281
|
-
result.push((result[result.length - 1] * (period - 1) + data[i]) / period);
|
|
282
|
-
}
|
|
283
|
-
return result;
|
|
284
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export declare function computeEMA(data: number[], period: number): number;
|
|
2
|
-
export declare function computeATR(highs: number[], lows: number[], closes: number[], period: number): number;
|
|
3
|
-
export declare function computeATRSeries(highs: number[], lows: number[], closes: number[], period: number): number[];
|
|
4
|
-
export declare function computeRSI(closes: number[], period: number): number;
|
|
5
|
-
export declare function linearSlope(y: number[], x?: number[]): number;
|
|
6
|
-
export declare function computeStd(data: number[]): number;
|
|
7
|
-
export declare function mean(data: number[]): number;
|
|
8
|
-
/** Find swing highs and lows from OHLCV bars (simple pivot-point method). */
|
|
9
|
-
export declare function findSwingPoints(bars: {
|
|
10
|
-
high: number;
|
|
11
|
-
low: number;
|
|
12
|
-
}[], lookback?: number): {
|
|
13
|
-
highs: number[];
|
|
14
|
-
lows: number[];
|
|
15
|
-
};
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// Shared indicator computation helpers.
|
|
2
|
-
// Used by both regime feature engineering and signal condition evaluation.
|
|
3
|
-
export function computeEMA(data, period) {
|
|
4
|
-
if (data.length < period)
|
|
5
|
-
return data[data.length - 1] ?? 0;
|
|
6
|
-
const k = 2 / (period + 1);
|
|
7
|
-
let ema = mean(data.slice(0, period));
|
|
8
|
-
for (let i = period; i < data.length; i++) {
|
|
9
|
-
ema = data[i] * k + ema * (1 - k);
|
|
10
|
-
}
|
|
11
|
-
return ema;
|
|
12
|
-
}
|
|
13
|
-
export function computeATR(highs, lows, closes, period) {
|
|
14
|
-
const series = computeATRSeries(highs, lows, closes, period);
|
|
15
|
-
return series[series.length - 1] ?? 0;
|
|
16
|
-
}
|
|
17
|
-
export function computeATRSeries(highs, lows, closes, period) {
|
|
18
|
-
if (highs.length < 2)
|
|
19
|
-
return [];
|
|
20
|
-
const tr = [];
|
|
21
|
-
for (let i = 1; i < highs.length; i++) {
|
|
22
|
-
tr.push(Math.max(highs[i] - lows[i], Math.abs(highs[i] - closes[i - 1]), Math.abs(lows[i] - closes[i - 1])));
|
|
23
|
-
}
|
|
24
|
-
const atr = [];
|
|
25
|
-
atr[0] = mean(tr.slice(0, period));
|
|
26
|
-
for (let i = 1; i < tr.length; i++) {
|
|
27
|
-
atr[i] = (atr[i - 1] * (period - 1) + tr[i]) / period;
|
|
28
|
-
}
|
|
29
|
-
return atr;
|
|
30
|
-
}
|
|
31
|
-
export function computeRSI(closes, period) {
|
|
32
|
-
if (closes.length < period + 1)
|
|
33
|
-
return 50;
|
|
34
|
-
const changes = [];
|
|
35
|
-
for (let i = 1; i < closes.length; i++) {
|
|
36
|
-
changes.push(closes[i] - closes[i - 1]);
|
|
37
|
-
}
|
|
38
|
-
let avgGain = 0;
|
|
39
|
-
let avgLoss = 0;
|
|
40
|
-
for (let i = 0; i < period; i++) {
|
|
41
|
-
if (changes[i] > 0)
|
|
42
|
-
avgGain += changes[i];
|
|
43
|
-
else
|
|
44
|
-
avgLoss -= changes[i];
|
|
45
|
-
}
|
|
46
|
-
avgGain /= period;
|
|
47
|
-
avgLoss /= period;
|
|
48
|
-
for (let i = period; i < changes.length; i++) {
|
|
49
|
-
const gain = changes[i] > 0 ? changes[i] : 0;
|
|
50
|
-
const loss = changes[i] < 0 ? -changes[i] : 0;
|
|
51
|
-
avgGain = (avgGain * (period - 1) + gain) / period;
|
|
52
|
-
avgLoss = (avgLoss * (period - 1) + loss) / period;
|
|
53
|
-
}
|
|
54
|
-
if (avgLoss === 0)
|
|
55
|
-
return 100;
|
|
56
|
-
const rs = avgGain / avgLoss;
|
|
57
|
-
return 100 - 100 / (1 + rs);
|
|
58
|
-
}
|
|
59
|
-
export function linearSlope(y, x) {
|
|
60
|
-
const n = y.length;
|
|
61
|
-
if (n < 2)
|
|
62
|
-
return 0;
|
|
63
|
-
const xs = x ?? Array.from({ length: n }, (_, i) => i);
|
|
64
|
-
const mx = mean(xs);
|
|
65
|
-
const my = mean(y);
|
|
66
|
-
let num = 0;
|
|
67
|
-
let den = 0;
|
|
68
|
-
for (let i = 0; i < n; i++) {
|
|
69
|
-
num += (xs[i] - mx) * (y[i] - my);
|
|
70
|
-
den += (xs[i] - mx) ** 2;
|
|
71
|
-
}
|
|
72
|
-
return den > 0 ? num / den : 0;
|
|
73
|
-
}
|
|
74
|
-
export function computeStd(data) {
|
|
75
|
-
if (data.length < 2)
|
|
76
|
-
return 0;
|
|
77
|
-
const avg = mean(data);
|
|
78
|
-
const variance = data.reduce((s, v) => s + (v - avg) ** 2, 0) / (data.length - 1);
|
|
79
|
-
return Math.sqrt(variance);
|
|
80
|
-
}
|
|
81
|
-
export function mean(data) {
|
|
82
|
-
if (data.length === 0)
|
|
83
|
-
return 0;
|
|
84
|
-
return data.reduce((s, v) => s + v, 0) / data.length;
|
|
85
|
-
}
|
|
86
|
-
/** Find swing highs and lows from OHLCV bars (simple pivot-point method). */
|
|
87
|
-
export function findSwingPoints(bars, lookback = 5) {
|
|
88
|
-
const highs = [];
|
|
89
|
-
const lows = [];
|
|
90
|
-
for (let i = lookback; i < bars.length - lookback; i++) {
|
|
91
|
-
let isHigh = true;
|
|
92
|
-
let isLow = true;
|
|
93
|
-
for (let j = i - lookback; j <= i + lookback; j++) {
|
|
94
|
-
if (j === i)
|
|
95
|
-
continue;
|
|
96
|
-
if (bars[j].high >= bars[i].high)
|
|
97
|
-
isHigh = false;
|
|
98
|
-
if (bars[j].low <= bars[i].low)
|
|
99
|
-
isLow = false;
|
|
100
|
-
}
|
|
101
|
-
if (isHigh)
|
|
102
|
-
highs.push(bars[i].high);
|
|
103
|
-
if (isLow)
|
|
104
|
-
lows.push(bars[i].low);
|
|
105
|
-
}
|
|
106
|
-
return { highs, lows };
|
|
107
|
-
}
|