@reefclaw/openclaw-plugin 0.1.1 → 0.1.2
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/bridge/bridge.d.ts +28 -1
- package/bridge/bridge.js +99 -4
- package/ccxt/binance-ban-gate.js +9 -0
- package/config/agent-config-client.d.ts +22 -0
- package/config/agent-config-client.js +44 -1
- package/config/agent-config-poller.d.ts +8 -1
- package/config/agent-config-poller.js +1 -0
- package/config/entitlement-gate.d.ts +51 -0
- package/config/entitlement-gate.js +137 -0
- package/index.js +40 -23
- package/ingest/pending-entry-metadata.d.ts +31 -9
- package/ingest/pending-entry-metadata.js +70 -16
- package/ingest/position-auto-capture.js +14 -3
- package/ingest/readiness-reporter.d.ts +19 -0
- package/ingest/readiness-reporter.js +142 -0
- package/live/exchange-info-cache.d.ts +3 -1
- package/live/exchange-info-cache.js +17 -2
- package/package.json +1 -1
- package/signals/strategy-adapter.d.ts +35 -2
- package/signals/strategy-adapter.js +87 -10
- package/tools/create-order.js +26 -20
|
@@ -9,6 +9,7 @@ import { evaluateConditions } from './conditions/registry.js';
|
|
|
9
9
|
import { resolveDirection } from './direction-rules.js';
|
|
10
10
|
import { computeEntry } from './entry-rules.js';
|
|
11
11
|
import { computeStop } from './stop-rules.js';
|
|
12
|
+
import { computeATR } from '../shared/indicators.js';
|
|
12
13
|
/**
|
|
13
14
|
* Higher-timeframe tick gating state, keyed by
|
|
14
15
|
* `${gateNamespace}\x1f${strategyName}:${symbol}`.
|
|
@@ -41,6 +42,73 @@ function pickTimeframeBars(ctx, tf) {
|
|
|
41
42
|
return ctx.ohlcv4h ?? [];
|
|
42
43
|
return ctx.ohlcv1h;
|
|
43
44
|
}
|
|
45
|
+
const TF_MS = {
|
|
46
|
+
'1h': 3_600_000,
|
|
47
|
+
'4h': 4 * 3_600_000,
|
|
48
|
+
'1d': 24 * 3_600_000,
|
|
49
|
+
};
|
|
50
|
+
/** Median spacing of the last few bars — robust bar-cadence probe. */
|
|
51
|
+
function barSpacingMs(bars) {
|
|
52
|
+
const n = bars.length;
|
|
53
|
+
if (n < 2)
|
|
54
|
+
return 0;
|
|
55
|
+
const deltas = [];
|
|
56
|
+
for (let i = Math.max(1, n - 4); i < n; i++) {
|
|
57
|
+
deltas.push(bars[i].time.getTime() - bars[i - 1].time.getTime());
|
|
58
|
+
}
|
|
59
|
+
deltas.sort((a, b) => a - b);
|
|
60
|
+
return deltas[Math.floor(deltas.length / 2)];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Live parity for higher-timeframe strategies (the tfHours-aware-stops fix,
|
|
64
|
+
* 2026-07 — see docs/STRATEGY_RESEARCH_2026-07.md §6.4 / CLAUDE.md ★).
|
|
65
|
+
*
|
|
66
|
+
* Every implicit bar read in this engine — stop rules
|
|
67
|
+
* (`findSwingPoints(ctx.ohlcv1h.slice(-48))`), entry rules
|
|
68
|
+
* (`computeEMA(ctx.ohlcv1h…)`), conditions without a `tfHours` param
|
|
69
|
+
* (ema_proximity, stoch_rsi_extreme, adx_*, …) and `ctx.atr14` — targets the
|
|
70
|
+
* `ohlcv1h` slot. The backtest engine feeds MAIN-timeframe bars into that
|
|
71
|
+
* slot (and computes atr14 from them), so a 4h/1d strategy backtests against
|
|
72
|
+
* primary-timeframe geometry. LIVE contexts put real 1h bars there, so the
|
|
73
|
+
* same strategy would compute stops/EMAs/ATR from 1h data — a 1d ATR is ~8×
|
|
74
|
+
* the 1h ATR, so live stops came out ~8× too tight. This helper gives the
|
|
75
|
+
* evaluation the exact context shape the backtest validated: primary bars in
|
|
76
|
+
* the `ohlcv1h` slot, atr14 recomputed from them (same computeATR the
|
|
77
|
+
* backtest and live context builders use).
|
|
78
|
+
*
|
|
79
|
+
* Detection, not configuration: when the `ohlcv1h` slot already carries
|
|
80
|
+
* primary-cadence bars (median spacing ≥ 90% of the primary bar duration —
|
|
81
|
+
* i.e. a backtest context), the context is returned UNTOUCHED, so backtest
|
|
82
|
+
* behaviour is byte-identical by construction (including warm-up: the
|
|
83
|
+
* backtest engine already refuses to build a context below 50 main bars).
|
|
84
|
+
* A live 1h series can only look primary-spaced through a data gap, in
|
|
85
|
+
* which case we fall back to the untouched context (pre-fix behaviour)
|
|
86
|
+
* rather than guessing.
|
|
87
|
+
*
|
|
88
|
+
* Returns null for a LIVE context whose primary-timeframe history is below
|
|
89
|
+
* the backtest's 50-bar warm-up — the caller skips evaluation, mirroring
|
|
90
|
+
* the backtest's null-context warm-up window.
|
|
91
|
+
*
|
|
92
|
+
* Exported for tests.
|
|
93
|
+
*/
|
|
94
|
+
export function resolvePrimaryContext(ctx, tf, tfBars) {
|
|
95
|
+
if (tf === '1h')
|
|
96
|
+
return ctx;
|
|
97
|
+
const spacing = barSpacingMs(ctx.ohlcv1h);
|
|
98
|
+
if (spacing === 0 || spacing >= TF_MS[tf] * 0.9)
|
|
99
|
+
return ctx; // already primary (backtest) or undecidable
|
|
100
|
+
if (tfBars.length < 50)
|
|
101
|
+
return null; // live warm-up parity with the backtest engine
|
|
102
|
+
const highs = tfBars.map(b => b.high);
|
|
103
|
+
const lows = tfBars.map(b => b.low);
|
|
104
|
+
const closes = tfBars.map(b => b.close);
|
|
105
|
+
const atr14 = computeATR(highs, lows, closes, 14);
|
|
106
|
+
return {
|
|
107
|
+
...ctx,
|
|
108
|
+
ohlcv1h: tfBars,
|
|
109
|
+
atr14: Number.isFinite(atr14) && atr14 > 0 ? atr14 : ctx.atr14,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
44
112
|
/** Empty no-signal evaluation — used when gating skips a strategy. */
|
|
45
113
|
const SKIPPED = { direction: null, conditions: [], trade: undefined };
|
|
46
114
|
/**
|
|
@@ -80,40 +148,49 @@ export function adaptStrategy(config, gateNamespace) {
|
|
|
80
148
|
return SKIPPED;
|
|
81
149
|
}
|
|
82
150
|
lastEvaluatedBarTime.set(gateKey, latestBarTime);
|
|
151
|
+
// ─── Higher-timeframe live parity ──────────────────────────────
|
|
152
|
+
// Evaluate against a context whose implicit-1h slot carries
|
|
153
|
+
// primary-timeframe bars — see resolvePrimaryContext. Backtest
|
|
154
|
+
// contexts pass through untouched; only live contexts for 4h/1d
|
|
155
|
+
// strategies are adapted, and a live context below the backtest's
|
|
156
|
+
// 50-bar warm-up resolves to null → skip.
|
|
157
|
+
const ectx = resolvePrimaryContext(ctx, tf, tfBars);
|
|
158
|
+
if (ectx === null)
|
|
159
|
+
return SKIPPED;
|
|
83
160
|
// ─── SkipIf gates ──────────────────────────────────────────────
|
|
84
161
|
// Portfolio-wide / cross-symbol filters. If any are met the
|
|
85
162
|
// strategy is skipped this tick. Evaluated before main conditions
|
|
86
163
|
// so the bulk of the work is short-circuited.
|
|
87
164
|
if (config.skipIf && config.skipIf.length > 0) {
|
|
88
|
-
const { conditions: skipResults } = evaluateConditions(config.skipIf,
|
|
165
|
+
const { conditions: skipResults } = evaluateConditions(config.skipIf, ectx, null);
|
|
89
166
|
if (skipResults.some(c => c.met))
|
|
90
167
|
return SKIPPED;
|
|
91
168
|
}
|
|
92
169
|
// Pass 1: evaluate conditions with direction = null
|
|
93
|
-
const { conditions: pass1, condCtx } = evaluateConditions(config.conditions,
|
|
170
|
+
const { conditions: pass1, condCtx } = evaluateConditions(config.conditions, ectx, null);
|
|
94
171
|
// Determine direction
|
|
95
|
-
const direction = resolveDirection(config.directionRule,
|
|
172
|
+
const direction = resolveDirection(config.directionRule, ectx, condCtx);
|
|
96
173
|
// Pass 2: re-evaluate direction-sensitive conditions now that we know direction
|
|
97
174
|
// (orderbook_imbalance and funding_contrarian behave differently per direction)
|
|
98
175
|
const directionSensitive = new Set(['orderbook_imbalance', 'funding_contrarian', 'funding_extreme_skip', 'funding_position_ok', 'return_momentum']);
|
|
99
176
|
const hasDirSensitive = config.conditions.some(c => directionSensitive.has(c.type));
|
|
100
177
|
let finalConditions = pass1;
|
|
101
178
|
if (direction && hasDirSensitive) {
|
|
102
|
-
const { conditions: pass2 } = evaluateConditions(config.conditions,
|
|
179
|
+
const { conditions: pass2 } = evaluateConditions(config.conditions, ectx, direction);
|
|
103
180
|
// Merge: use pass2 results for direction-sensitive, pass1 for others
|
|
104
181
|
finalConditions = pass1.map((c, i) => directionSensitive.has(config.conditions[i].type) ? pass2[i] : c);
|
|
105
182
|
}
|
|
106
183
|
const allMet = finalConditions.every(c => c.met);
|
|
107
184
|
let trade;
|
|
108
185
|
if (allMet && direction) {
|
|
109
|
-
const entryZone = computeEntry(config.entryRule,
|
|
110
|
-
const stopLevel = computeStop(config.stopRule,
|
|
186
|
+
const entryZone = computeEntry(config.entryRule, ectx, direction, condCtx);
|
|
187
|
+
const stopLevel = computeStop(config.stopRule, ectx, direction, condCtx);
|
|
111
188
|
const risk = direction === 'LONG'
|
|
112
|
-
?
|
|
113
|
-
: stopLevel -
|
|
189
|
+
? ectx.currentPrice - stopLevel
|
|
190
|
+
: stopLevel - ectx.currentPrice;
|
|
114
191
|
const targets = config.targetRMultiples.map(rm => direction === 'LONG'
|
|
115
|
-
?
|
|
116
|
-
:
|
|
192
|
+
? ectx.currentPrice + risk * rm
|
|
193
|
+
: ectx.currentPrice - risk * rm);
|
|
117
194
|
trade = { entryZone, stopLevel, targets };
|
|
118
195
|
}
|
|
119
196
|
return { direction, conditions: finalConditions, trade };
|
package/tools/create-order.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Tool: create_order — order execution with real price data + pre-trade risk gate
|
|
2
2
|
// Readiness gate: BLOCKED unless adapter.readiness === 'READY'.
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
3
4
|
import { formatError } from '../logger.js';
|
|
4
5
|
import { getQuoteBalance, getQuoteWalletBalance } from '../balance-utils.js';
|
|
5
6
|
import { fetchCurrentPrice, fetchOrderBook, isError } from './helpers.js';
|
|
@@ -293,32 +294,37 @@ export async function createOrderTool(args, deps) {
|
|
|
293
294
|
}
|
|
294
295
|
}
|
|
295
296
|
try {
|
|
296
|
-
|
|
297
|
+
// Stash the metadata BEFORE submission, keyed by a pre-generated
|
|
298
|
+
// clientOrderId (fixed 2026-07-07; supersedes the 2026-07-05 post-return
|
|
299
|
+
// stash). For market orders the user-data WS fill routinely arrives
|
|
300
|
+
// BEFORE the REST ack resolves — observed on prod (FIL 2026-07-06
|
|
301
|
+
// 20:28:17: WS capture consumed nothing and journaled metadata=none while
|
|
302
|
+
// the post-return stash landed milliseconds later). The WS
|
|
303
|
+
// ORDER_TRADE_UPDATE carries the clientOrderId (`o.c`), so
|
|
304
|
+
// onWsFillObserved falls back to it when the exchange orderId lookup
|
|
305
|
+
// misses; promote() below adds the exchange-orderId alias once the REST
|
|
306
|
+
// ack returns for the normal (WS-after-REST) ordering. If the submission
|
|
307
|
+
// throws, the unused stash entry simply expires (24h TTL, pruned).
|
|
308
|
+
let stashCid;
|
|
309
|
+
if (deps.autoCapture?.pendingEntries && metadata) {
|
|
310
|
+
stashCid = randomUUID();
|
|
311
|
+
deps.autoCapture.pendingEntries.put({
|
|
312
|
+
orderId: stashCid,
|
|
313
|
+
clientOrderId: stashCid,
|
|
314
|
+
symbol: args.symbol,
|
|
315
|
+
side: side,
|
|
316
|
+
metadata,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
const order = await deps.adapter.createOrder(args.symbol, side, type, args.amount, args.price, metadata, stashCid ? { clientOrderId: stashCid } : undefined);
|
|
297
320
|
// Auto-capture entry to the Position Decision Journal — fail-open, never
|
|
298
321
|
// block the trading hot path on a webapp ingest blip.
|
|
299
322
|
if (deps.autoCapture) {
|
|
300
|
-
|
|
301
|
-
// market orders routinely come back filled>0 but with NO average price
|
|
302
|
-
// in the immediate REST response — the synchronous capture below then
|
|
303
|
-
// bails ("missing fill price; skipping capture") and, before this fix,
|
|
304
|
-
// the metadata was dropped on the floor because the stash only ran in
|
|
305
|
-
// the not-filled branch. The WS-driven onWsFillObserved captured those
|
|
306
|
-
// entries seconds later with metadata=none → the live journal filled
|
|
307
|
-
// with '(no thesis recorded)' / setup_type='unknown' rows (majority of
|
|
308
|
-
// 2026-07 live entries) even though the agent supplied full v2.10.0
|
|
309
|
-
// metadata every time. When the synchronous capture DOES succeed, the
|
|
310
|
-
// WS dedup (openedFromExchangeTradeId === orderId) skips the duplicate
|
|
311
|
-
// and the unused stash entry simply expires (24h TTL, pruned).
|
|
312
|
-
if (metadata &&
|
|
323
|
+
if (stashCid &&
|
|
313
324
|
deps.autoCapture.pendingEntries &&
|
|
314
325
|
typeof order.id === 'string' &&
|
|
315
326
|
order.id.length > 0) {
|
|
316
|
-
deps.autoCapture.pendingEntries.
|
|
317
|
-
orderId: order.id,
|
|
318
|
-
symbol: args.symbol,
|
|
319
|
-
side: side,
|
|
320
|
-
metadata,
|
|
321
|
-
});
|
|
327
|
+
deps.autoCapture.pendingEntries.promote(stashCid, order.id);
|
|
322
328
|
}
|
|
323
329
|
const filledNow = typeof order.filled === 'number' && order.filled > 0;
|
|
324
330
|
if (filledNow) {
|