@reefclaw/openclaw-plugin 0.1.0
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/.gitignore +8 -0
- package/audit/mode-transition-audit.d.ts +11 -0
- package/audit/mode-transition-audit.js +29 -0
- package/balance-utils.d.ts +36 -0
- package/balance-utils.js +98 -0
- package/bridge/bridge.d.ts +109 -0
- package/bridge/bridge.js +1036 -0
- package/bridge/config.d.ts +43 -0
- package/bridge/config.js +176 -0
- package/bridge/connector.d.ts +52 -0
- package/bridge/connector.js +293 -0
- package/bridge/event-replay-buffer.d.ts +43 -0
- package/bridge/event-replay-buffer.js +109 -0
- package/bridge/gateway/event-parser.d.ts +209 -0
- package/bridge/gateway/event-parser.js +793 -0
- package/bridge/gateway/gateway-config.d.ts +39 -0
- package/bridge/gateway/gateway-config.js +86 -0
- package/bridge/gateway/gateway-http-client.d.ts +50 -0
- package/bridge/gateway/gateway-http-client.js +165 -0
- package/bridge/gateway/gateway-ws-client.d.ts +116 -0
- package/bridge/gateway/gateway-ws-client.js +417 -0
- package/bridge/gateway/poller.d.ts +146 -0
- package/bridge/gateway/poller.js +505 -0
- package/bridge/gateway/tool-discovery.d.ts +25 -0
- package/bridge/gateway/tool-discovery.js +198 -0
- package/bridge/index.d.ts +2 -0
- package/bridge/index.js +253 -0
- package/bridge/logger.d.ts +2 -0
- package/bridge/logger.js +2 -0
- package/bridge/provider.d.ts +156 -0
- package/bridge/provider.js +2 -0
- package/bridge/providers/emergency-commands.d.ts +54 -0
- package/bridge/providers/emergency-commands.js +235 -0
- package/bridge/providers/gateway.d.ts +322 -0
- package/bridge/providers/gateway.js +2302 -0
- package/bridge/providers/mock.d.ts +37 -0
- package/bridge/providers/mock.js +385 -0
- package/bridge/providers/onboarding-commands.d.ts +83 -0
- package/bridge/providers/onboarding-commands.js +213 -0
- package/bridge/providers/risk-calculator.d.ts +96 -0
- package/bridge/providers/risk-calculator.js +369 -0
- package/bridge/setup.d.ts +32 -0
- package/bridge/setup.js +226 -0
- package/bridge/types.d.ts +584 -0
- package/bridge/types.js +50 -0
- package/bridge/utils/reconnect.d.ts +6 -0
- package/bridge/utils/reconnect.js +6 -0
- package/bridge/utils/skill-signing.d.ts +51 -0
- package/bridge/utils/skill-signing.js +138 -0
- package/bridge/utils/skill-version.d.ts +17 -0
- package/bridge/utils/skill-version.js +74 -0
- package/ccxt/binance-ban-gate.d.ts +47 -0
- package/ccxt/binance-ban-gate.js +409 -0
- package/ccxt/binance-private.d.ts +325 -0
- package/ccxt/binance-private.js +1415 -0
- package/ccxt/binance-public.d.ts +18 -0
- package/ccxt/binance-public.js +147 -0
- package/config/agent-config-client.d.ts +55 -0
- package/config/agent-config-client.js +145 -0
- package/config/agent-config-poller.d.ts +25 -0
- package/config/agent-config-poller.js +100 -0
- package/config/brackets-config.d.ts +22 -0
- package/config/brackets-config.js +58 -0
- package/config/gate-store.d.ts +18 -0
- package/config/gate-store.js +61 -0
- package/config/plugin-config-io.d.ts +181 -0
- package/config/plugin-config-io.js +84 -0
- package/config/position-review-config.d.ts +35 -0
- package/config/position-review-config.js +105 -0
- package/config/tool-gate.d.ts +53 -0
- package/config/tool-gate.js +125 -0
- package/config/user-data-stream-config.d.ts +85 -0
- package/config/user-data-stream-config.js +224 -0
- package/connector-supervisor.d.ts +36 -0
- package/connector-supervisor.js +149 -0
- package/exchange-adapter.d.ts +49 -0
- package/exchange-adapter.js +4 -0
- package/index.d.ts +30 -0
- package/index.js +2030 -0
- package/ingest/pending-entry-metadata.d.ts +52 -0
- package/ingest/pending-entry-metadata.js +182 -0
- package/ingest/position-auto-capture.d.ts +98 -0
- package/ingest/position-auto-capture.js +394 -0
- package/ingest/position-decisions-client.d.ts +318 -0
- package/ingest/position-decisions-client.js +296 -0
- package/ingest/reconcile-db-vs-exchange.d.ts +13 -0
- package/ingest/reconcile-db-vs-exchange.js +114 -0
- package/ingest/reconciler-cleanup.d.ts +37 -0
- package/ingest/reconciler-cleanup.js +147 -0
- package/ingest/rest-gap-filler.d.ts +191 -0
- package/ingest/rest-gap-filler.js +565 -0
- package/ingest/touched-symbols-store.d.ts +25 -0
- package/ingest/touched-symbols-store.js +96 -0
- package/ingest/trade-store-client.d.ts +40 -0
- package/ingest/trade-store-client.js +125 -0
- package/ingest/ws-ingest.d.ts +43 -0
- package/ingest/ws-ingest.js +126 -0
- package/learning/setup-family.d.ts +21 -0
- package/learning/setup-family.js +103 -0
- package/lifecycle/install-signal-handlers.d.ts +33 -0
- package/lifecycle/install-signal-handlers.js +112 -0
- package/lifecycle/shutdown-coordinator.d.ts +43 -0
- package/lifecycle/shutdown-coordinator.js +131 -0
- package/live/bracket-id.d.ts +18 -0
- package/live/bracket-id.js +81 -0
- package/live/bracket-ledger.d.ts +54 -0
- package/live/bracket-ledger.js +267 -0
- package/live/bracket-manager.d.ts +82 -0
- package/live/bracket-manager.js +478 -0
- package/live/bracket-params.d.ts +22 -0
- package/live/bracket-params.js +124 -0
- package/live/bracket-reconciler.d.ts +95 -0
- package/live/bracket-reconciler.js +573 -0
- package/live/bracket-types.d.ts +102 -0
- package/live/bracket-types.js +8 -0
- package/live/deposit-tracker.d.ts +62 -0
- package/live/deposit-tracker.js +97 -0
- package/live/emergency-controls.d.ts +32 -0
- package/live/emergency-controls.js +226 -0
- package/live/exchange-errors.d.ts +12 -0
- package/live/exchange-errors.js +130 -0
- package/live/exchange-info-cache.d.ts +35 -0
- package/live/exchange-info-cache.js +119 -0
- package/live/fact-subscriber.d.ts +78 -0
- package/live/fact-subscriber.js +182 -0
- package/live/intent-journal.d.ts +42 -0
- package/live/intent-journal.js +122 -0
- package/live/listen-key-manager.d.ts +70 -0
- package/live/listen-key-manager.js +169 -0
- package/live/live-adapter.d.ts +264 -0
- package/live/live-adapter.js +1665 -0
- package/live/live-balance-enricher.d.ts +32 -0
- package/live/live-balance-enricher.js +104 -0
- package/live/live-bracket-api.d.ts +13 -0
- package/live/live-bracket-api.js +20 -0
- package/live/live-state-store.d.ts +194 -0
- package/live/live-state-store.js +450 -0
- package/live/local-signal-service.d.ts +57 -0
- package/live/local-signal-service.js +146 -0
- package/live/local-strategy-evaluator.d.ts +62 -0
- package/live/local-strategy-evaluator.js +127 -0
- package/live/microstructure-assembler.d.ts +54 -0
- package/live/microstructure-assembler.js +148 -0
- package/live/order-poller.d.ts +29 -0
- package/live/order-poller.js +125 -0
- package/live/position-state-store.d.ts +83 -0
- package/live/position-state-store.js +237 -0
- package/live/proposal-decision-listener.d.ts +64 -0
- package/live/proposal-decision-listener.js +288 -0
- package/live/proposal-manager.d.ts +76 -0
- package/live/proposal-manager.js +140 -0
- package/live/rate-limiter.d.ts +47 -0
- package/live/rate-limiter.js +159 -0
- package/live/reconciler.d.ts +39 -0
- package/live/reconciler.js +175 -0
- package/live/setup-buckets.d.ts +7 -0
- package/live/setup-buckets.js +33 -0
- package/live/slippage-tracker.d.ts +45 -0
- package/live/slippage-tracker.js +78 -0
- package/live/stop-watcher.d.ts +34 -0
- package/live/stop-watcher.js +158 -0
- package/live/user-data-active-probe.d.ts +54 -0
- package/live/user-data-active-probe.js +180 -0
- package/live/user-data-stream-controller.d.ts +200 -0
- package/live/user-data-stream-controller.js +579 -0
- package/live/user-data-stream-ws.d.ts +22 -0
- package/live/user-data-stream-ws.js +63 -0
- package/live/user-data-stream.d.ts +243 -0
- package/live/user-data-stream.js +704 -0
- package/logger.d.ts +2 -0
- package/logger.js +2 -0
- package/mfe.d.ts +21 -0
- package/mfe.js +68 -0
- package/onboarding/mode-ladder.d.ts +1 -0
- package/onboarding/mode-ladder.js +3 -0
- package/onboarding/runtime.d.ts +71 -0
- package/onboarding/runtime.js +153 -0
- package/openclaw.plugin.json +94 -0
- package/package.json +27 -0
- package/paper-adapter.d.ts +24 -0
- package/paper-adapter.js +91 -0
- package/persistence/state-manager.d.ts +42 -0
- package/persistence/state-manager.js +164 -0
- package/pinned-plan.d.ts +9 -0
- package/pinned-plan.js +23 -0
- package/risk/pre-trade-check.d.ts +38 -0
- package/risk/pre-trade-check.js +345 -0
- package/risk/pre-trade-types.d.ts +60 -0
- package/risk/pre-trade-types.js +3 -0
- package/scripts/assemble.mjs +114 -0
- package/shadow/shadow-tracker.d.ts +36 -0
- package/shadow/shadow-tracker.js +151 -0
- package/shadow/types.d.ts +42 -0
- package/shadow/types.js +20 -0
- package/shared/indicators-extended.d.ts +52 -0
- package/shared/indicators-extended.js +291 -0
- package/shared/indicators.d.ts +15 -0
- package/shared/indicators.js +114 -0
- package/signals/conditions/registry.d.ts +16 -0
- package/signals/conditions/registry.js +1274 -0
- package/signals/conditions/types.d.ts +1 -0
- package/signals/conditions/types.js +4 -0
- package/signals/direction-rules.d.ts +3 -0
- package/signals/direction-rules.js +24 -0
- package/signals/entry-rules.d.ts +6 -0
- package/signals/entry-rules.js +33 -0
- package/signals/serialize-context.d.ts +4 -0
- package/signals/serialize-context.js +39 -0
- package/signals/stop-rules.d.ts +3 -0
- package/signals/stop-rules.js +48 -0
- package/signals/strategy-adapter.d.ts +14 -0
- package/signals/strategy-adapter.js +122 -0
- package/signals/types.d.ts +1 -0
- package/signals/types.js +8 -0
- package/simulator/exchange-simulator.d.ts +93 -0
- package/simulator/exchange-simulator.js +684 -0
- package/simulator/fill-engine.d.ts +53 -0
- package/simulator/fill-engine.js +276 -0
- package/simulator/paper-market-feed.d.ts +26 -0
- package/simulator/paper-market-feed.js +104 -0
- package/simulator/realistic-fills.d.ts +59 -0
- package/simulator/realistic-fills.js +175 -0
- package/simulator/types.d.ts +219 -0
- package/simulator/types.js +43 -0
- package/skills/reefclaw/SKILL.md +97 -0
- package/strategy/builtin-strategies.d.ts +2 -0
- package/strategy/builtin-strategies.js +109 -0
- package/strategy/condition-registry.d.ts +3 -0
- package/strategy/condition-registry.js +153 -0
- package/strategy/evaluator.d.ts +67 -0
- package/strategy/evaluator.js +93 -0
- package/tools/assessment-validation.d.ts +118 -0
- package/tools/assessment-validation.js +415 -0
- package/tools/attach-brackets.d.ts +34 -0
- package/tools/attach-brackets.js +363 -0
- package/tools/audit-bracket-protection.d.ts +49 -0
- package/tools/audit-bracket-protection.js +527 -0
- package/tools/cancel-all-orders.d.ts +7 -0
- package/tools/cancel-all-orders.js +5 -0
- package/tools/cancel-order.d.ts +10 -0
- package/tools/cancel-order.js +14 -0
- package/tools/check-position-health.d.ts +46 -0
- package/tools/check-position-health.js +194 -0
- package/tools/clear-exchange-credentials.d.ts +24 -0
- package/tools/clear-exchange-credentials.js +70 -0
- package/tools/close-position.d.ts +22 -0
- package/tools/close-position.js +449 -0
- package/tools/create-order.d.ts +54 -0
- package/tools/create-order.js +338 -0
- package/tools/exit-gate.d.ts +58 -0
- package/tools/exit-gate.js +162 -0
- package/tools/fetch-balance.d.ts +5 -0
- package/tools/fetch-balance.js +4 -0
- package/tools/fetch-ohlcv.d.ts +11 -0
- package/tools/fetch-ohlcv.js +8 -0
- package/tools/fetch-open-orders.d.ts +7 -0
- package/tools/fetch-open-orders.js +4 -0
- package/tools/fetch-positions.d.ts +7 -0
- package/tools/fetch-positions.js +4 -0
- package/tools/fetch-ticker.d.ts +11 -0
- package/tools/fetch-ticker.js +5 -0
- package/tools/get-agent-profile.d.ts +4 -0
- package/tools/get-agent-profile.js +6 -0
- package/tools/get-analytics.d.ts +6 -0
- package/tools/get-analytics.js +7 -0
- package/tools/get-backtest.d.ts +12 -0
- package/tools/get-backtest.js +91 -0
- package/tools/get-basis.d.ts +7 -0
- package/tools/get-basis.js +7 -0
- package/tools/get-bracket-config.d.ts +11 -0
- package/tools/get-bracket-config.js +24 -0
- package/tools/get-cascade-risk.d.ts +7 -0
- package/tools/get-cascade-risk.js +8 -0
- package/tools/get-crypto-metrics.d.ts +18 -0
- package/tools/get-crypto-metrics.js +45 -0
- package/tools/get-cvd.d.ts +6 -0
- package/tools/get-cvd.js +6 -0
- package/tools/get-divergences.d.ts +6 -0
- package/tools/get-divergences.js +6 -0
- package/tools/get-funding-context.d.ts +6 -0
- package/tools/get-funding-context.js +16 -0
- package/tools/get-liquidation-levels.d.ts +7 -0
- package/tools/get-liquidation-levels.js +7 -0
- package/tools/get-liquidation-pulse.d.ts +9 -0
- package/tools/get-liquidation-pulse.js +22 -0
- package/tools/get-market-breadth.d.ts +6 -0
- package/tools/get-market-breadth.js +8 -0
- package/tools/get-market-intel.d.ts +19 -0
- package/tools/get-market-intel.js +116 -0
- package/tools/get-market-structure.d.ts +47 -0
- package/tools/get-market-structure.js +198 -0
- package/tools/get-my-mined-patterns.d.ts +20 -0
- package/tools/get-my-mined-patterns.js +61 -0
- package/tools/get-my-proposed-learnings.d.ts +20 -0
- package/tools/get-my-proposed-learnings.js +55 -0
- package/tools/get-my-recent-reviews.d.ts +22 -0
- package/tools/get-my-recent-reviews.js +66 -0
- package/tools/get-orderbook.d.ts +21 -0
- package/tools/get-orderbook.js +32 -0
- package/tools/get-pattern-scan.d.ts +7 -0
- package/tools/get-pattern-scan.js +8 -0
- package/tools/get-regime.d.ts +6 -0
- package/tools/get-regime.js +7 -0
- package/tools/get-relevant-learnings.d.ts +21 -0
- package/tools/get-relevant-learnings.js +65 -0
- package/tools/get-resting-liquidity.d.ts +6 -0
- package/tools/get-resting-liquidity.js +11 -0
- package/tools/get-risk-scenario.d.ts +29 -0
- package/tools/get-risk-scenario.js +47 -0
- package/tools/get-risk-summary.d.ts +51 -0
- package/tools/get-risk-summary.js +118 -0
- package/tools/get-sentiment.d.ts +4 -0
- package/tools/get-sentiment.js +6 -0
- package/tools/get-session-review.d.ts +7 -0
- package/tools/get-session-review.js +8 -0
- package/tools/get-setup-detail.d.ts +7 -0
- package/tools/get-setup-detail.js +303 -0
- package/tools/get-signals.d.ts +15 -0
- package/tools/get-signals.js +54 -0
- package/tools/get-sizing.d.ts +6 -0
- package/tools/get-sizing.js +6 -0
- package/tools/get-trade-feedback.d.ts +7 -0
- package/tools/get-trade-feedback.js +8 -0
- package/tools/get-trade-flow.d.ts +7 -0
- package/tools/get-trade-flow.js +7 -0
- package/tools/get-volume-analysis.d.ts +21 -0
- package/tools/get-volume-analysis.js +74 -0
- package/tools/get-volume-profile.d.ts +7 -0
- package/tools/get-volume-profile.js +7 -0
- package/tools/helpers.d.ts +25 -0
- package/tools/helpers.js +38 -0
- package/tools/intel-api.d.ts +14 -0
- package/tools/intel-api.js +52 -0
- package/tools/intel-cache.d.ts +25 -0
- package/tools/intel-cache.js +133 -0
- package/tools/list-strategies.d.ts +7 -0
- package/tools/list-strategies.js +6 -0
- package/tools/modify-stop.d.ts +17 -0
- package/tools/modify-stop.js +81 -0
- package/tools/modify-target.d.ts +17 -0
- package/tools/modify-target.js +71 -0
- package/tools/propose-learning.d.ts +22 -0
- package/tools/propose-learning.js +65 -0
- package/tools/query-review-outcomes.d.ts +30 -0
- package/tools/query-review-outcomes.js +64 -0
- package/tools/query-trades.d.ts +21 -0
- package/tools/query-trades.js +37 -0
- package/tools/record-position-reviews.d.ts +38 -0
- package/tools/record-position-reviews.js +147 -0
- package/tools/save-strategy.d.ts +16 -0
- package/tools/save-strategy.js +46 -0
- package/tools/scan-pairs.d.ts +18 -0
- package/tools/scan-pairs.js +220 -0
- package/tools/score-setup.d.ts +31 -0
- package/tools/score-setup.js +268 -0
- package/tools/set-bracket-requirement.d.ts +18 -0
- package/tools/set-bracket-requirement.js +81 -0
- package/tools/set-exchange-credentials.d.ts +25 -0
- package/tools/set-exchange-credentials.js +80 -0
- package/tools/set-trading-mode.d.ts +26 -0
- package/tools/set-trading-mode.js +135 -0
- package/tools/test-exchange-credentials.d.ts +16 -0
- package/tools/test-exchange-credentials.js +100 -0
- package/tools/toggle-strategy.d.ts +8 -0
- package/tools/toggle-strategy.js +8 -0
- package/trading-params-cache.d.ts +26 -0
- package/trading-params-cache.js +52 -0
- package/types.d.ts +110 -0
- package/types.js +7 -0
- package/util/plugin-paths.d.ts +3 -0
- package/util/plugin-paths.js +15 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// ⚠️ GENERATED FILE — DO NOT EDIT.
|
|
2
|
+
// Canonical source of truth: shared/src/shared/indicators-extended.ts
|
|
3
|
+
// Regenerate: node scripts/sync-shared-code.mjs (enforced by shared-code-sync.test.ts)
|
|
4
|
+
//
|
|
5
|
+
// This copy exists because this package builds with tsc and deploys as a
|
|
6
|
+
// self-contained tree that strips workspace deps, so it cannot import
|
|
7
|
+
// @reefclaw/shared runtime code across the deploy boundary.
|
|
8
|
+
// Extended indicator computations for Phase 13 — Expanded Indicators.
|
|
9
|
+
// Wraps technicalindicators library + custom implementations.
|
|
10
|
+
// All functions take OHLCV arrays (oldest first) and return latest values.
|
|
11
|
+
import { computeATRSeries, computeRSI, mean, computeStd } from './indicators.js';
|
|
12
|
+
// ─── MACD (12, 26, 9) ──────────────────────────────────────────────────
|
|
13
|
+
export function computeMACD(closes, fast = 12, slow = 26, signal = 9) {
|
|
14
|
+
if (closes.length < slow + signal) {
|
|
15
|
+
return { line: 0, signal: 0, histogram: 0, crossover: 'none' };
|
|
16
|
+
}
|
|
17
|
+
// Compute full EMA series (both aligned to start at index `slow - 1`)
|
|
18
|
+
const emaFastSeries = emaSeries(closes, fast);
|
|
19
|
+
const emaSlowSeries = emaSeries(closes, slow);
|
|
20
|
+
// Align: fast series starts earlier, so take the tail matching slow series length
|
|
21
|
+
const offset = emaFastSeries.length - emaSlowSeries.length;
|
|
22
|
+
const macdLine = [];
|
|
23
|
+
for (let i = 0; i < emaSlowSeries.length; i++) {
|
|
24
|
+
macdLine.push(emaFastSeries[i + offset] - emaSlowSeries[i]);
|
|
25
|
+
}
|
|
26
|
+
// Signal line = EMA of MACD line
|
|
27
|
+
const signalSeries = emaSeries(macdLine, signal);
|
|
28
|
+
const sigOffset = macdLine.length - signalSeries.length;
|
|
29
|
+
const currentLine = macdLine[macdLine.length - 1];
|
|
30
|
+
const currentSignal = signalSeries[signalSeries.length - 1];
|
|
31
|
+
const prevLine = macdLine.length >= 2 ? macdLine[macdLine.length - 2] : currentLine;
|
|
32
|
+
const prevSignalIdx = signalSeries.length >= 2 ? signalSeries.length - 2 : signalSeries.length - 1;
|
|
33
|
+
const prevSignal = signalSeries[prevSignalIdx];
|
|
34
|
+
let crossover = 'none';
|
|
35
|
+
if (prevLine <= prevSignal && currentLine > currentSignal)
|
|
36
|
+
crossover = 'bullish';
|
|
37
|
+
else if (prevLine >= prevSignal && currentLine < currentSignal)
|
|
38
|
+
crossover = 'bearish';
|
|
39
|
+
return {
|
|
40
|
+
line: currentLine,
|
|
41
|
+
signal: currentSignal,
|
|
42
|
+
histogram: currentLine - currentSignal,
|
|
43
|
+
crossover,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// ─── Bollinger Bands (20, 2σ) ───────────────────────────────────────────
|
|
47
|
+
export function computeBollingerBands(closes, period = 20, stdDev = 2) {
|
|
48
|
+
if (closes.length < period) {
|
|
49
|
+
const p = closes[closes.length - 1] ?? 0;
|
|
50
|
+
return { upper: p, middle: p, lower: p, bandwidth: 0, percentB: 0.5 };
|
|
51
|
+
}
|
|
52
|
+
const slice = closes.slice(-period);
|
|
53
|
+
const middle = mean(slice);
|
|
54
|
+
const std = computeStd(slice);
|
|
55
|
+
const upper = middle + stdDev * std;
|
|
56
|
+
const lower = middle - stdDev * std;
|
|
57
|
+
const bandwidth = middle > 0 ? ((upper - lower) / middle) * 100 : 0;
|
|
58
|
+
const price = closes[closes.length - 1];
|
|
59
|
+
const percentB = upper !== lower ? (price - lower) / (upper - lower) : 0.5;
|
|
60
|
+
return { upper, middle, lower, bandwidth, percentB };
|
|
61
|
+
}
|
|
62
|
+
// ─── VWAP ───────────────────────────────────────────────────────────────
|
|
63
|
+
export function computeVWAP(bars) {
|
|
64
|
+
if (bars.length === 0)
|
|
65
|
+
return 0;
|
|
66
|
+
let cumVolume = 0;
|
|
67
|
+
let cumTPxVol = 0;
|
|
68
|
+
for (const bar of bars) {
|
|
69
|
+
const tp = (bar.high + bar.low + bar.close) / 3;
|
|
70
|
+
cumVolume += bar.volume;
|
|
71
|
+
cumTPxVol += tp * bar.volume;
|
|
72
|
+
}
|
|
73
|
+
return cumVolume > 0 ? cumTPxVol / cumVolume : bars[bars.length - 1].close;
|
|
74
|
+
}
|
|
75
|
+
// ─── Stochastic RSI (14, 14, 3, 3) ─────────────────────────────────────
|
|
76
|
+
export function computeStochRSI(closes, rsiPeriod = 14, stochPeriod = 14, kSmooth = 3, dSmooth = 3) {
|
|
77
|
+
if (closes.length < rsiPeriod + stochPeriod + dSmooth) {
|
|
78
|
+
return { k: 50, d: 50 };
|
|
79
|
+
}
|
|
80
|
+
// Compute RSI series
|
|
81
|
+
const rsiValues = [];
|
|
82
|
+
for (let i = rsiPeriod + 1; i <= closes.length; i++) {
|
|
83
|
+
rsiValues.push(computeRSI(closes.slice(0, i), rsiPeriod));
|
|
84
|
+
}
|
|
85
|
+
if (rsiValues.length < stochPeriod)
|
|
86
|
+
return { k: 50, d: 50 };
|
|
87
|
+
// Stochastic of RSI
|
|
88
|
+
const rawK = [];
|
|
89
|
+
for (let i = stochPeriod - 1; i < rsiValues.length; i++) {
|
|
90
|
+
const window = rsiValues.slice(i - stochPeriod + 1, i + 1);
|
|
91
|
+
const min = Math.min(...window);
|
|
92
|
+
const max = Math.max(...window);
|
|
93
|
+
rawK.push(max !== min ? ((rsiValues[i] - min) / (max - min)) * 100 : 50);
|
|
94
|
+
}
|
|
95
|
+
// %K = SMA of raw stochastic
|
|
96
|
+
const kValues = sma(rawK, kSmooth);
|
|
97
|
+
// %D = SMA of %K
|
|
98
|
+
const dValues = sma(kValues, dSmooth);
|
|
99
|
+
return {
|
|
100
|
+
k: Math.round(kValues[kValues.length - 1] ?? 50),
|
|
101
|
+
d: Math.round(dValues[dValues.length - 1] ?? 50),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// ─── ADX (14) ───────────────────────────────────────────────────────────
|
|
105
|
+
export function computeADX(highs, lows, closes, period = 14) {
|
|
106
|
+
if (highs.length < period * 2 + 1) {
|
|
107
|
+
return { adx: 0, plusDI: 0, minusDI: 0 };
|
|
108
|
+
}
|
|
109
|
+
const plusDM = [];
|
|
110
|
+
const minusDM = [];
|
|
111
|
+
const tr = [];
|
|
112
|
+
for (let i = 1; i < highs.length; i++) {
|
|
113
|
+
const upMove = highs[i] - highs[i - 1];
|
|
114
|
+
const downMove = lows[i - 1] - lows[i];
|
|
115
|
+
plusDM.push(upMove > downMove && upMove > 0 ? upMove : 0);
|
|
116
|
+
minusDM.push(downMove > upMove && downMove > 0 ? downMove : 0);
|
|
117
|
+
tr.push(Math.max(highs[i] - lows[i], Math.abs(highs[i] - closes[i - 1]), Math.abs(lows[i] - closes[i - 1])));
|
|
118
|
+
}
|
|
119
|
+
// Smooth with Wilder's smoothing (equivalent to EMA with alpha=1/period)
|
|
120
|
+
const smoothPlusDM = wilderSmooth(plusDM, period);
|
|
121
|
+
const smoothMinusDM = wilderSmooth(minusDM, period);
|
|
122
|
+
const smoothTR = wilderSmooth(tr, period);
|
|
123
|
+
// +DI and -DI series
|
|
124
|
+
const plusDISeries = [];
|
|
125
|
+
const minusDISeries = [];
|
|
126
|
+
for (let i = 0; i < smoothTR.length; i++) {
|
|
127
|
+
plusDISeries.push(smoothTR[i] > 0 ? (smoothPlusDM[i] / smoothTR[i]) * 100 : 0);
|
|
128
|
+
minusDISeries.push(smoothTR[i] > 0 ? (smoothMinusDM[i] / smoothTR[i]) * 100 : 0);
|
|
129
|
+
}
|
|
130
|
+
// DX series
|
|
131
|
+
const dxSeries = [];
|
|
132
|
+
for (let i = 0; i < plusDISeries.length; i++) {
|
|
133
|
+
const sum = plusDISeries[i] + minusDISeries[i];
|
|
134
|
+
dxSeries.push(sum > 0 ? (Math.abs(plusDISeries[i] - minusDISeries[i]) / sum) * 100 : 0);
|
|
135
|
+
}
|
|
136
|
+
// ADX = Wilder smooth of DX
|
|
137
|
+
const adxSeries = wilderSmooth(dxSeries, period);
|
|
138
|
+
return {
|
|
139
|
+
adx: Math.round(adxSeries[adxSeries.length - 1] ?? 0),
|
|
140
|
+
plusDI: Math.round(plusDISeries[plusDISeries.length - 1] ?? 0),
|
|
141
|
+
minusDI: Math.round(minusDISeries[minusDISeries.length - 1] ?? 0),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
// ─── Ichimoku Cloud ─────────────────────────────────────────────────────
|
|
145
|
+
export function computeIchimoku(highs, lows, closes, tenkanPeriod = 9, kijunPeriod = 26, senkouBPeriod = 52) {
|
|
146
|
+
const n = highs.length;
|
|
147
|
+
if (n < senkouBPeriod) {
|
|
148
|
+
const p = closes[n - 1] ?? 0;
|
|
149
|
+
return { tenkan: p, kijun: p, senkouA: p, senkouB: p, chikou: p, cloudPosition: 'inside' };
|
|
150
|
+
}
|
|
151
|
+
const midpoint = (arr, period, end) => {
|
|
152
|
+
const slice = arr.slice(Math.max(0, end - period + 1), end + 1);
|
|
153
|
+
return (Math.max(...slice) + Math.min(...slice)) / 2;
|
|
154
|
+
};
|
|
155
|
+
const tenkan = midpoint(highs.concat().map((h, i) => Math.max(h, lows[i])), tenkanPeriod, n - 1);
|
|
156
|
+
const kijun = midpoint(highs.concat().map((h, i) => Math.max(h, lows[i])), kijunPeriod, n - 1);
|
|
157
|
+
// Recalculate properly using highs and lows separately
|
|
158
|
+
const tenkanH = Math.max(...highs.slice(-tenkanPeriod));
|
|
159
|
+
const tenkanL = Math.min(...lows.slice(-tenkanPeriod));
|
|
160
|
+
const tenkanVal = (tenkanH + tenkanL) / 2;
|
|
161
|
+
const kijunH = Math.max(...highs.slice(-kijunPeriod));
|
|
162
|
+
const kijunL = Math.min(...lows.slice(-kijunPeriod));
|
|
163
|
+
const kijunVal = (kijunH + kijunL) / 2;
|
|
164
|
+
const senkouA = (tenkanVal + kijunVal) / 2;
|
|
165
|
+
const senkouBH = Math.max(...highs.slice(-senkouBPeriod));
|
|
166
|
+
const senkouBL = Math.min(...lows.slice(-senkouBPeriod));
|
|
167
|
+
const senkouB = (senkouBH + senkouBL) / 2;
|
|
168
|
+
const chikou = closes[n - 1]; // Current close (projected back 26 periods)
|
|
169
|
+
const price = closes[n - 1];
|
|
170
|
+
const cloudTop = Math.max(senkouA, senkouB);
|
|
171
|
+
const cloudBottom = Math.min(senkouA, senkouB);
|
|
172
|
+
const cloudPosition = price > cloudTop ? 'above' : price < cloudBottom ? 'below' : 'inside';
|
|
173
|
+
return { tenkan: tenkanVal, kijun: kijunVal, senkouA, senkouB, chikou, cloudPosition };
|
|
174
|
+
}
|
|
175
|
+
// ─── OBV (On-Balance Volume) ────────────────────────────────────────────
|
|
176
|
+
export function computeOBV(closes, volumes) {
|
|
177
|
+
if (closes.length < 2)
|
|
178
|
+
return { obv: 0, slope: 'flat' };
|
|
179
|
+
let obv = 0;
|
|
180
|
+
const obvSeries = [0];
|
|
181
|
+
for (let i = 1; i < closes.length; i++) {
|
|
182
|
+
if (closes[i] > closes[i - 1])
|
|
183
|
+
obv += volumes[i];
|
|
184
|
+
else if (closes[i] < closes[i - 1])
|
|
185
|
+
obv -= volumes[i];
|
|
186
|
+
obvSeries.push(obv);
|
|
187
|
+
}
|
|
188
|
+
// Slope over last 10 bars
|
|
189
|
+
const lookback = Math.min(10, obvSeries.length);
|
|
190
|
+
const recent = obvSeries.slice(-lookback);
|
|
191
|
+
const first = recent[0];
|
|
192
|
+
const last = recent[recent.length - 1];
|
|
193
|
+
const threshold = Math.abs(first) * 0.01; // 1% threshold
|
|
194
|
+
const slope = last - first > threshold ? 'rising' : last - first < -threshold ? 'falling' : 'flat';
|
|
195
|
+
return { obv, slope };
|
|
196
|
+
}
|
|
197
|
+
// ─── Supertrend (10, 3) ─────────────────────────────────────────────────
|
|
198
|
+
export function computeSupertrend(highs, lows, closes, period = 10, multiplier = 3) {
|
|
199
|
+
const atrSeries = computeATRSeries(highs, lows, closes, period);
|
|
200
|
+
if (atrSeries.length === 0) {
|
|
201
|
+
return { value: closes[closes.length - 1] ?? 0, direction: 'bullish' };
|
|
202
|
+
}
|
|
203
|
+
// ATR series starts at index 1 (needs previous close for TR)
|
|
204
|
+
// Align: atrSeries[i] corresponds to bar index i+1
|
|
205
|
+
let upperBand = 0;
|
|
206
|
+
let lowerBand = 0;
|
|
207
|
+
let supertrend = 0;
|
|
208
|
+
let direction = 'bullish';
|
|
209
|
+
for (let i = 0; i < atrSeries.length; i++) {
|
|
210
|
+
const barIdx = i + 1; // offset for TR calculation
|
|
211
|
+
const hl2 = (highs[barIdx] + lows[barIdx]) / 2;
|
|
212
|
+
const atr = atrSeries[i];
|
|
213
|
+
const basicUpper = hl2 + multiplier * atr;
|
|
214
|
+
const basicLower = hl2 - multiplier * atr;
|
|
215
|
+
upperBand = i > 0 && basicUpper < upperBand && closes[barIdx - 1] > upperBand ? upperBand : basicUpper;
|
|
216
|
+
lowerBand = i > 0 && basicLower > lowerBand && closes[barIdx - 1] < lowerBand ? lowerBand : basicLower;
|
|
217
|
+
if (i === 0) {
|
|
218
|
+
supertrend = closes[barIdx] > upperBand ? lowerBand : upperBand;
|
|
219
|
+
direction = closes[barIdx] > upperBand ? 'bullish' : 'bearish';
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
if (direction === 'bullish') {
|
|
223
|
+
if (closes[barIdx] < lowerBand) {
|
|
224
|
+
direction = 'bearish';
|
|
225
|
+
supertrend = upperBand;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
supertrend = lowerBand;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
if (closes[barIdx] > upperBand) {
|
|
233
|
+
direction = 'bullish';
|
|
234
|
+
supertrend = lowerBand;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
supertrend = upperBand;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return { value: supertrend, direction };
|
|
243
|
+
}
|
|
244
|
+
// ─── Williams %R (14) ───────────────────────────────────────────────────
|
|
245
|
+
export function computeWilliamsR(highs, lows, closes, period = 14) {
|
|
246
|
+
if (highs.length < period)
|
|
247
|
+
return -50;
|
|
248
|
+
const recentHighs = highs.slice(-period);
|
|
249
|
+
const recentLows = lows.slice(-period);
|
|
250
|
+
const hh = Math.max(...recentHighs);
|
|
251
|
+
const ll = Math.min(...recentLows);
|
|
252
|
+
const close = closes[closes.length - 1];
|
|
253
|
+
return hh !== ll ? ((hh - close) / (hh - ll)) * -100 : -50;
|
|
254
|
+
}
|
|
255
|
+
// ─── Helper: EMA series ─────────────────────────────────────────────────
|
|
256
|
+
function emaSeries(data, period) {
|
|
257
|
+
if (data.length === 0)
|
|
258
|
+
return [];
|
|
259
|
+
if (data.length < period)
|
|
260
|
+
return [data[data.length - 1]];
|
|
261
|
+
const k = 2 / (period + 1);
|
|
262
|
+
const result = [];
|
|
263
|
+
let ema = mean(data.slice(0, period));
|
|
264
|
+
result.push(ema);
|
|
265
|
+
for (let i = period; i < data.length; i++) {
|
|
266
|
+
ema = data[i] * k + ema * (1 - k);
|
|
267
|
+
result.push(ema);
|
|
268
|
+
}
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
// ─── Helper: SMA series ─────────────────────────────────────────────────
|
|
272
|
+
function sma(data, period) {
|
|
273
|
+
if (data.length < period)
|
|
274
|
+
return data.length > 0 ? [mean(data)] : [];
|
|
275
|
+
const result = [];
|
|
276
|
+
for (let i = period - 1; i < data.length; i++) {
|
|
277
|
+
result.push(mean(data.slice(i - period + 1, i + 1)));
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
// ─── Helper: Wilder's smoothing ─────────────────────────────────────────
|
|
282
|
+
function wilderSmooth(data, period) {
|
|
283
|
+
if (data.length < period)
|
|
284
|
+
return [];
|
|
285
|
+
const result = [];
|
|
286
|
+
result.push(mean(data.slice(0, period)));
|
|
287
|
+
for (let i = period; i < data.length; i++) {
|
|
288
|
+
result.push((result[result.length - 1] * (period - 1) + data[i]) / period);
|
|
289
|
+
}
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// ⚠️ GENERATED FILE — DO NOT EDIT.
|
|
2
|
+
// Canonical source of truth: shared/src/shared/indicators.ts
|
|
3
|
+
// Regenerate: node scripts/sync-shared-code.mjs (enforced by shared-code-sync.test.ts)
|
|
4
|
+
//
|
|
5
|
+
// This copy exists because this package builds with tsc and deploys as a
|
|
6
|
+
// self-contained tree that strips workspace deps, so it cannot import
|
|
7
|
+
// @reefclaw/shared runtime code across the deploy boundary.
|
|
8
|
+
// Shared indicator computation helpers.
|
|
9
|
+
// Used by both regime feature engineering and signal condition evaluation.
|
|
10
|
+
export function computeEMA(data, period) {
|
|
11
|
+
if (data.length < period)
|
|
12
|
+
return data[data.length - 1] ?? 0;
|
|
13
|
+
const k = 2 / (period + 1);
|
|
14
|
+
let ema = mean(data.slice(0, period));
|
|
15
|
+
for (let i = period; i < data.length; i++) {
|
|
16
|
+
ema = data[i] * k + ema * (1 - k);
|
|
17
|
+
}
|
|
18
|
+
return ema;
|
|
19
|
+
}
|
|
20
|
+
export function computeATR(highs, lows, closes, period) {
|
|
21
|
+
const series = computeATRSeries(highs, lows, closes, period);
|
|
22
|
+
return series[series.length - 1] ?? 0;
|
|
23
|
+
}
|
|
24
|
+
export function computeATRSeries(highs, lows, closes, period) {
|
|
25
|
+
if (highs.length < 2)
|
|
26
|
+
return [];
|
|
27
|
+
const tr = [];
|
|
28
|
+
for (let i = 1; i < highs.length; i++) {
|
|
29
|
+
tr.push(Math.max(highs[i] - lows[i], Math.abs(highs[i] - closes[i - 1]), Math.abs(lows[i] - closes[i - 1])));
|
|
30
|
+
}
|
|
31
|
+
const atr = [];
|
|
32
|
+
atr[0] = mean(tr.slice(0, period));
|
|
33
|
+
for (let i = 1; i < tr.length; i++) {
|
|
34
|
+
atr[i] = (atr[i - 1] * (period - 1) + tr[i]) / period;
|
|
35
|
+
}
|
|
36
|
+
return atr;
|
|
37
|
+
}
|
|
38
|
+
export function computeRSI(closes, period) {
|
|
39
|
+
if (closes.length < period + 1)
|
|
40
|
+
return 50;
|
|
41
|
+
const changes = [];
|
|
42
|
+
for (let i = 1; i < closes.length; i++) {
|
|
43
|
+
changes.push(closes[i] - closes[i - 1]);
|
|
44
|
+
}
|
|
45
|
+
let avgGain = 0;
|
|
46
|
+
let avgLoss = 0;
|
|
47
|
+
for (let i = 0; i < period; i++) {
|
|
48
|
+
if (changes[i] > 0)
|
|
49
|
+
avgGain += changes[i];
|
|
50
|
+
else
|
|
51
|
+
avgLoss -= changes[i];
|
|
52
|
+
}
|
|
53
|
+
avgGain /= period;
|
|
54
|
+
avgLoss /= period;
|
|
55
|
+
for (let i = period; i < changes.length; i++) {
|
|
56
|
+
const gain = changes[i] > 0 ? changes[i] : 0;
|
|
57
|
+
const loss = changes[i] < 0 ? -changes[i] : 0;
|
|
58
|
+
avgGain = (avgGain * (period - 1) + gain) / period;
|
|
59
|
+
avgLoss = (avgLoss * (period - 1) + loss) / period;
|
|
60
|
+
}
|
|
61
|
+
if (avgLoss === 0)
|
|
62
|
+
return 100;
|
|
63
|
+
const rs = avgGain / avgLoss;
|
|
64
|
+
return 100 - 100 / (1 + rs);
|
|
65
|
+
}
|
|
66
|
+
export function linearSlope(y, x) {
|
|
67
|
+
const n = y.length;
|
|
68
|
+
if (n < 2)
|
|
69
|
+
return 0;
|
|
70
|
+
const xs = x ?? Array.from({ length: n }, (_, i) => i);
|
|
71
|
+
const mx = mean(xs);
|
|
72
|
+
const my = mean(y);
|
|
73
|
+
let num = 0;
|
|
74
|
+
let den = 0;
|
|
75
|
+
for (let i = 0; i < n; i++) {
|
|
76
|
+
num += (xs[i] - mx) * (y[i] - my);
|
|
77
|
+
den += (xs[i] - mx) ** 2;
|
|
78
|
+
}
|
|
79
|
+
return den > 0 ? num / den : 0;
|
|
80
|
+
}
|
|
81
|
+
export function computeStd(data) {
|
|
82
|
+
if (data.length < 2)
|
|
83
|
+
return 0;
|
|
84
|
+
const avg = mean(data);
|
|
85
|
+
const variance = data.reduce((s, v) => s + (v - avg) ** 2, 0) / (data.length - 1);
|
|
86
|
+
return Math.sqrt(variance);
|
|
87
|
+
}
|
|
88
|
+
export function mean(data) {
|
|
89
|
+
if (data.length === 0)
|
|
90
|
+
return 0;
|
|
91
|
+
return data.reduce((s, v) => s + v, 0) / data.length;
|
|
92
|
+
}
|
|
93
|
+
/** Find swing highs and lows from OHLCV bars (simple pivot-point method). */
|
|
94
|
+
export function findSwingPoints(bars, lookback = 5) {
|
|
95
|
+
const highs = [];
|
|
96
|
+
const lows = [];
|
|
97
|
+
for (let i = lookback; i < bars.length - lookback; i++) {
|
|
98
|
+
let isHigh = true;
|
|
99
|
+
let isLow = true;
|
|
100
|
+
for (let j = i - lookback; j <= i + lookback; j++) {
|
|
101
|
+
if (j === i)
|
|
102
|
+
continue;
|
|
103
|
+
if (bars[j].high >= bars[i].high)
|
|
104
|
+
isHigh = false;
|
|
105
|
+
if (bars[j].low <= bars[i].low)
|
|
106
|
+
isLow = false;
|
|
107
|
+
}
|
|
108
|
+
if (isHigh)
|
|
109
|
+
highs.push(bars[i].high);
|
|
110
|
+
if (isLow)
|
|
111
|
+
lows.push(bars[i].low);
|
|
112
|
+
}
|
|
113
|
+
return { highs, lows };
|
|
114
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MarketContext, Direction } from '../types.js';
|
|
2
|
+
import type { ConditionFn, ConditionConfig, ConditionContext } from './types.js';
|
|
3
|
+
export declare function getConditionFn(type: string): ConditionFn | undefined;
|
|
4
|
+
export declare function hasCondition(type: string): boolean;
|
|
5
|
+
export declare function listConditionTypes(): string[];
|
|
6
|
+
/** Evaluate an array of condition configs. Returns SignalCondition[] + shared context. */
|
|
7
|
+
export declare function evaluateConditions(configs: ConditionConfig[], ctx: MarketContext, direction: Direction | null): {
|
|
8
|
+
conditions: Array<{
|
|
9
|
+
name: string;
|
|
10
|
+
met: boolean;
|
|
11
|
+
currentValue: number;
|
|
12
|
+
threshold: number;
|
|
13
|
+
description: string;
|
|
14
|
+
}>;
|
|
15
|
+
condCtx: ConditionContext;
|
|
16
|
+
};
|