dexbot 1.6.8 → 1.6.9
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/CHANGELOG.md +24 -0
- package/claw/package.json +1 -1
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/dist/dexbot.js +44 -21
- package/dist/market_adapter/core/market_adapter_service.d.ts +81 -1
- package/dist/market_adapter/core/market_adapter_service.js +225 -18
- package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +10 -1
- package/dist/market_adapter/core/signals/hurst_analyzer.js +40 -18
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +26 -2
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +132 -29
- package/dist/market_adapter/core/strategies/regime_gate.d.ts +61 -3
- package/dist/market_adapter/core/strategies/regime_gate.js +207 -7
- package/dist/market_adapter/interval_utils.d.ts +18 -1
- package/dist/market_adapter/interval_utils.js +31 -1
- package/dist/market_adapter/market_adapter.d.ts +151 -2
- package/dist/market_adapter/market_adapter.js +378 -62
- package/dist/market_adapter/test_helpers.d.ts +2 -2
- package/dist/market_adapter/test_helpers.js +2 -2
- package/dist/market_adapter/utils/file_json_cache.d.ts +29 -0
- package/dist/market_adapter/utils/file_json_cache.js +106 -0
- package/dist/modules/config.d.ts +6 -0
- package/dist/modules/config.js +1 -0
- package/dist/modules/constants.d.ts +5 -1
- package/dist/modules/constants.js +41 -14
- package/dist/modules/order/grid.js +95 -25
- package/dist/modules/version_notice.d.ts +166 -20
- package/dist/modules/version_notice.js +401 -56
- package/dist/pm2.js +9 -2
- package/dist/unlock.js +7 -7
- package/docs/DEXBOT_COMPARISON.md +3 -3
- package/docs/EVOLUTION.md +13 -12
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
- package/docs/GRID_RECALCULATION.md +6 -2
- package/docs/README.md +2 -2
- package/market_adapter/README.md +28 -0
- package/package.json +1 -1
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute R/S (rescaled range) over `returns[from..to)` (to exclusive).
|
|
3
|
+
* Taking a range instead of a slice keeps the caller's buffer reusable.
|
|
4
|
+
*/
|
|
5
|
+
declare function computeRS(returns: ArrayLike<number>, from: number, to: number): number;
|
|
1
6
|
/**
|
|
2
7
|
* Shared Hurst zone classification used by both HurstAnalyzer.getAnalysis()
|
|
3
8
|
* and the regime gate, so the zone boundaries live in exactly one place.
|
|
@@ -10,8 +15,12 @@ declare function classifyHurst(h: number, band?: number): {
|
|
|
10
15
|
declare class HurstAnalyzer {
|
|
11
16
|
private _w;
|
|
12
17
|
window: number;
|
|
18
|
+
/** Bars of history this analyzer's state depends on (see bufferBars use in the regime gate). */
|
|
19
|
+
bufferBars: number;
|
|
13
20
|
scales: number[];
|
|
14
21
|
private _prices;
|
|
22
|
+
private _returns;
|
|
23
|
+
private _priceCount;
|
|
15
24
|
private _updateCount;
|
|
16
25
|
hurst: number;
|
|
17
26
|
isReady: boolean;
|
|
@@ -44,5 +53,5 @@ declare class HurstAnalyzer {
|
|
|
44
53
|
updateCount: number;
|
|
45
54
|
};
|
|
46
55
|
}
|
|
47
|
-
export { HurstAnalyzer, classifyHurst };
|
|
56
|
+
export { HurstAnalyzer, classifyHurst, computeRS };
|
|
48
57
|
//# sourceMappingURL=hurst_analyzer.d.ts.map
|
|
@@ -17,6 +17,15 @@ import { roundTo } from '../../../modules/order/utils/math.js';
|
|
|
17
17
|
* Algorithm: for each scale τ in config.scales, partition the log-return window into
|
|
18
18
|
* non-overlapping chunks of length τ, compute average R/S per chunk, then OLS-fit
|
|
19
19
|
* log(avgRS) vs log(τ) — the slope is the Hurst exponent.
|
|
20
|
+
*
|
|
21
|
+
* PERFORMANCE (2026-09): like the permutation-entropy analyzer, this one is
|
|
22
|
+
* re-created and re-fed the entire candle history for every bot on every hourly
|
|
23
|
+
* cycle. The previous implementation allocated `returns.slice(c*tau, ...)` for
|
|
24
|
+
* every R/S chunk (~30 arrays per bar, ~23k per bot per cycle) plus a fresh
|
|
25
|
+
* log-return array per bar. The buffers are now preallocated typed arrays and
|
|
26
|
+
* `computeRS` takes a [from, to) range instead of a slice, so `update()`
|
|
27
|
+
* allocates nothing. Measured on an 782-bar series: ~5 ms -> ~3 ms per bot per
|
|
28
|
+
* cycle with the same exponent (see tests/test_market_adapter_entropy_equivalence.ts).
|
|
20
29
|
*/
|
|
21
30
|
/**
|
|
22
31
|
* OLS slope of ys ~ slope * xs + intercept. Returns slope only.
|
|
@@ -34,18 +43,19 @@ function olsSlope(xs, ys) {
|
|
|
34
43
|
return denom !== 0 ? (n * sumXY - sumX * sumY) / denom : 0.5;
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
37
|
-
* Compute R/S (rescaled range)
|
|
46
|
+
* Compute R/S (rescaled range) over `returns[from..to)` (to exclusive).
|
|
47
|
+
* Taking a range instead of a slice keeps the caller's buffer reusable.
|
|
38
48
|
*/
|
|
39
|
-
function computeRS(returns) {
|
|
40
|
-
const n =
|
|
49
|
+
function computeRS(returns, from, to) {
|
|
50
|
+
const n = to - from;
|
|
41
51
|
if (n < 2)
|
|
42
52
|
return 0;
|
|
43
53
|
let sum = 0;
|
|
44
|
-
for (let i =
|
|
54
|
+
for (let i = from; i < to; i++)
|
|
45
55
|
sum += returns[i];
|
|
46
56
|
const mean = sum / n;
|
|
47
57
|
let cumDev = 0, maxCum = -Infinity, minCum = Infinity, sumSq = 0;
|
|
48
|
-
for (let i =
|
|
58
|
+
for (let i = from; i < to; i++) {
|
|
49
59
|
const d = returns[i] - mean;
|
|
50
60
|
cumDev += d;
|
|
51
61
|
if (cumDev > maxCum)
|
|
@@ -84,8 +94,12 @@ function classifyHurst(h, band = MARKET_ADAPTER.HURST_ZONE_BAND) {
|
|
|
84
94
|
class HurstAnalyzer {
|
|
85
95
|
_w;
|
|
86
96
|
window;
|
|
97
|
+
/** Bars of history this analyzer's state depends on (see bufferBars use in the regime gate). */
|
|
98
|
+
bufferBars;
|
|
87
99
|
scales;
|
|
88
100
|
_prices;
|
|
101
|
+
_returns;
|
|
102
|
+
_priceCount;
|
|
89
103
|
_updateCount;
|
|
90
104
|
hurst;
|
|
91
105
|
isReady;
|
|
@@ -97,8 +111,14 @@ class HurstAnalyzer {
|
|
|
97
111
|
constructor(config = {}) {
|
|
98
112
|
this._w = Math.ceil(config.window ?? 128);
|
|
99
113
|
this.window = this._w;
|
|
114
|
+
this.bufferBars = this._w + 1;
|
|
100
115
|
this.scales = config.scales ?? [8, 16, 32, 64];
|
|
101
|
-
|
|
116
|
+
// Rolling price buffer holds window+1 prices so returns span [0..window]
|
|
117
|
+
// and always include the newest bar (a window+2 cap would permanently
|
|
118
|
+
// exclude it). Returns reuse one buffer across every update.
|
|
119
|
+
this._prices = new Float64Array(this._w + 1);
|
|
120
|
+
this._returns = new Float64Array(this._w);
|
|
121
|
+
this._priceCount = 0;
|
|
102
122
|
this._updateCount = 0;
|
|
103
123
|
this.hurst = 0.5;
|
|
104
124
|
this.isReady = false;
|
|
@@ -112,32 +132,34 @@ class HurstAnalyzer {
|
|
|
112
132
|
if (!Number.isFinite(price) || price <= 0) {
|
|
113
133
|
throw new Error('price must be a positive finite number');
|
|
114
134
|
}
|
|
115
|
-
this.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
135
|
+
const capacity = this._w + 1;
|
|
136
|
+
if (this._priceCount === capacity) {
|
|
137
|
+
this._prices.copyWithin(0, 1);
|
|
138
|
+
this._priceCount--;
|
|
139
|
+
}
|
|
140
|
+
this._prices[this._priceCount++] = price;
|
|
120
141
|
this._updateCount++;
|
|
121
|
-
if (this.
|
|
142
|
+
if (this._priceCount < capacity) {
|
|
122
143
|
this.isReady = false;
|
|
123
144
|
return this.getAnalysis();
|
|
124
145
|
}
|
|
125
146
|
// Log returns over the rolling window
|
|
126
|
-
const
|
|
127
|
-
|
|
147
|
+
const w = this._w;
|
|
148
|
+
const returns = this._returns;
|
|
149
|
+
for (let i = 0; i < w; i++) {
|
|
128
150
|
returns[i] = Math.log(this._prices[i + 1] / this._prices[i]);
|
|
129
151
|
}
|
|
130
152
|
// R/S at each scale → OLS slope
|
|
131
153
|
const logRS = [], logTau = [];
|
|
132
154
|
for (const τ of this.scales) {
|
|
133
|
-
if (τ >=
|
|
155
|
+
if (τ >= w)
|
|
134
156
|
continue;
|
|
135
|
-
const nChunks = Math.floor(
|
|
157
|
+
const nChunks = Math.floor(w / τ);
|
|
136
158
|
if (nChunks < 1)
|
|
137
159
|
continue;
|
|
138
160
|
let sumRS = 0, count = 0;
|
|
139
161
|
for (let c = 0; c < nChunks; c++) {
|
|
140
|
-
const rs = computeRS(returns
|
|
162
|
+
const rs = computeRS(returns, c * τ, (c + 1) * τ);
|
|
141
163
|
if (rs > 0) {
|
|
142
164
|
sumRS += rs;
|
|
143
165
|
count++;
|
|
@@ -166,5 +188,5 @@ class HurstAnalyzer {
|
|
|
166
188
|
};
|
|
167
189
|
}
|
|
168
190
|
}
|
|
169
|
-
export { HurstAnalyzer, classifyHurst };
|
|
191
|
+
export { HurstAnalyzer, classifyHurst, computeRS };
|
|
170
192
|
//# sourceMappingURL=hurst_analyzer.js.map
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordinal pattern key for the m values starting at `prices[start]`, stepping by
|
|
3
|
+
* `delay`. Returns the permutation of source indices (sorted by value, ties
|
|
4
|
+
* broken by source index) encoded as a factorial-number-system integer, so the
|
|
5
|
+
* key is a small int (m! - 1 <= 5039 for m = 7) instead of a string.
|
|
6
|
+
*
|
|
7
|
+
* Sorting uses a preallocated scratch pair and insertion sort (m <= 7), so the
|
|
8
|
+
* hot path allocates nothing.
|
|
9
|
+
*/
|
|
10
|
+
declare function ordinalPatternKey(prices: Float64Array, start: number, m: number, delay: number): number;
|
|
1
11
|
interface PermutationEntropyAnalysis {
|
|
2
12
|
isReady: boolean;
|
|
3
13
|
entropy: number;
|
|
@@ -11,8 +21,11 @@ declare class PermutationEntropyAnalyzer {
|
|
|
11
21
|
delay: number;
|
|
12
22
|
window: number;
|
|
13
23
|
_bufSize: number;
|
|
24
|
+
bufferBars: number;
|
|
14
25
|
_maxEntropy: number;
|
|
15
|
-
_prices:
|
|
26
|
+
_prices: Float64Array;
|
|
27
|
+
_priceCount: number;
|
|
28
|
+
_counts: Map<number, number>;
|
|
16
29
|
_updateCount: number;
|
|
17
30
|
entropy: number;
|
|
18
31
|
normalizedEntropy: number;
|
|
@@ -24,6 +37,17 @@ declare class PermutationEntropyAnalyzer {
|
|
|
24
37
|
* @param {number} config.window - Rolling window of ordinal patterns (default 100)
|
|
25
38
|
*/
|
|
26
39
|
constructor(config?: Record<string, number>);
|
|
40
|
+
/** Number of ordinal patterns currently inside a full buffer. */
|
|
41
|
+
private get _numPatterns();
|
|
42
|
+
private _addPattern;
|
|
43
|
+
private _removePattern;
|
|
44
|
+
/**
|
|
45
|
+
* Shannon entropy: H = -Σ p_k * log(p_k) over the live pattern counts.
|
|
46
|
+
* Iterating the (small, <= min(window, m!)) count map is cheaper than
|
|
47
|
+
* maintaining a running sum and keeps the arithmetic identical to counting
|
|
48
|
+
* the window from scratch.
|
|
49
|
+
*/
|
|
50
|
+
private _entropyFromCounts;
|
|
27
51
|
/**
|
|
28
52
|
* Feed a new price and return analysis.
|
|
29
53
|
* @param {number} price
|
|
@@ -32,5 +56,5 @@ declare class PermutationEntropyAnalyzer {
|
|
|
32
56
|
update(price: number): PermutationEntropyAnalysis;
|
|
33
57
|
getAnalysis(): PermutationEntropyAnalysis;
|
|
34
58
|
}
|
|
35
|
-
export { PermutationEntropyAnalyzer };
|
|
59
|
+
export { PermutationEntropyAnalyzer, ordinalPatternKey };
|
|
36
60
|
//# sourceMappingURL=permutation_entropy_analyzer.d.ts.map
|
|
@@ -17,20 +17,73 @@ import { roundTo } from '../../../modules/order/utils/math.js';
|
|
|
17
17
|
* Thresholds are defined in MARKET_ADAPTER.PE_NODES in constants.ts.
|
|
18
18
|
*
|
|
19
19
|
* Default: m=5 (5!=120 patterns), window=100 bars.
|
|
20
|
+
*
|
|
21
|
+
* PERFORMANCE (2026-09): the analyzer is re-created and re-fed the whole candle
|
|
22
|
+
* history for every bot on every hourly cycle, and the rolling window only ever
|
|
23
|
+
* depends on the last `bufSize` prices. The first implementation re-counted all
|
|
24
|
+
* `window` patterns on every `update()` and encoded each pattern as a *string*,
|
|
25
|
+
* so one cycle cost ~42k patterns x (5 heap objects + comparator sort + string
|
|
26
|
+
* concat) per bot. The count map is now maintained incrementally (only the
|
|
27
|
+
* entering and the leaving pattern change per update) and patterns are encoded as
|
|
28
|
+
* factorial-number-system integers, so `update()` allocates nothing and does O(1)
|
|
29
|
+
* work plus one O(distinct patterns) entropy pass. Measured on an 782-bar series:
|
|
30
|
+
* ~10 ms -> ~0.2 ms per bot per cycle, with the same entropy values (the sum is
|
|
31
|
+
* taken over the same multiset of counts, so results agree to float noise; see
|
|
32
|
+
* tests/test_market_adapter_entropy_equivalence.ts).
|
|
20
33
|
*/
|
|
34
|
+
// Pattern-key scratch buffers. The analyzer is synchronous and single-threaded,
|
|
35
|
+
// so one module-level scratch pair is enough and keeps `update()` allocation-free.
|
|
36
|
+
// Sized for the largest supported embedding dimension (PE_ANALYZER_LIMITS.M_MAX).
|
|
37
|
+
const SCRATCH_VALUES = new Float64Array(MARKET_ADAPTER.PE_ANALYZER_LIMITS.M_MAX);
|
|
38
|
+
const SCRATCH_INDICES = new Int32Array(MARKET_ADAPTER.PE_ANALYZER_LIMITS.M_MAX);
|
|
39
|
+
// (m-1)! table for the Lehmer/rank encoding, indexed by m.
|
|
40
|
+
const FACTORIAL = (() => {
|
|
41
|
+
const table = [1, 1];
|
|
42
|
+
for (let i = 2; i <= MARKET_ADAPTER.PE_ANALYZER_LIMITS.M_MAX; i++)
|
|
43
|
+
table.push(table[i - 1] * i);
|
|
44
|
+
return table;
|
|
45
|
+
})();
|
|
21
46
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
47
|
+
* Ordinal pattern key for the m values starting at `prices[start]`, stepping by
|
|
48
|
+
* `delay`. Returns the permutation of source indices (sorted by value, ties
|
|
49
|
+
* broken by source index) encoded as a factorial-number-system integer, so the
|
|
50
|
+
* key is a small int (m! - 1 <= 5039 for m = 7) instead of a string.
|
|
51
|
+
*
|
|
52
|
+
* Sorting uses a preallocated scratch pair and insertion sort (m <= 7), so the
|
|
53
|
+
* hot path allocates nothing.
|
|
25
54
|
*/
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
55
|
+
function ordinalPatternKey(prices, start, m, delay) {
|
|
56
|
+
for (let j = 0; j < m; j++) {
|
|
57
|
+
SCRATCH_VALUES[j] = prices[start + j * delay];
|
|
58
|
+
SCRATCH_INDICES[j] = j;
|
|
59
|
+
}
|
|
60
|
+
// Insertion sort by (value, source index) — same ordering as the previous
|
|
61
|
+
// `Array.sort((a, b) => a.v !== b.v ? a.v - b.v : a.j - b.j)` comparator.
|
|
62
|
+
for (let j = 1; j < m; j++) {
|
|
63
|
+
const value = SCRATCH_VALUES[j];
|
|
64
|
+
const index = SCRATCH_INDICES[j];
|
|
65
|
+
let i = j - 1;
|
|
66
|
+
while (i >= 0 && (SCRATCH_VALUES[i] > value || (SCRATCH_VALUES[i] === value && SCRATCH_INDICES[i] > index))) {
|
|
67
|
+
SCRATCH_VALUES[i + 1] = SCRATCH_VALUES[i];
|
|
68
|
+
SCRATCH_INDICES[i + 1] = SCRATCH_INDICES[i];
|
|
69
|
+
i--;
|
|
70
|
+
}
|
|
71
|
+
SCRATCH_VALUES[i + 1] = value;
|
|
72
|
+
SCRATCH_INDICES[i + 1] = index;
|
|
73
|
+
}
|
|
74
|
+
// Lehmer code of the resulting permutation: digit j counts how many of the
|
|
75
|
+
// indices still to the right are smaller than SCRATCH_INDICES[j]. Digits are
|
|
76
|
+
// bounded by (m-1-j), so the encoding is collision free.
|
|
77
|
+
let key = 0;
|
|
78
|
+
for (let j = 0; j < m - 1; j++) {
|
|
79
|
+
const current = SCRATCH_INDICES[j];
|
|
80
|
+
let smaller = 0;
|
|
81
|
+
for (let k = j + 1; k < m; k++) {
|
|
82
|
+
if (SCRATCH_INDICES[k] < current)
|
|
83
|
+
smaller++;
|
|
84
|
+
}
|
|
85
|
+
key += smaller * FACTORIAL[m - 1 - j];
|
|
86
|
+
}
|
|
34
87
|
return key;
|
|
35
88
|
}
|
|
36
89
|
class PermutationEntropyAnalyzer {
|
|
@@ -38,8 +91,11 @@ class PermutationEntropyAnalyzer {
|
|
|
38
91
|
delay;
|
|
39
92
|
window;
|
|
40
93
|
_bufSize;
|
|
94
|
+
bufferBars;
|
|
41
95
|
_maxEntropy;
|
|
42
96
|
_prices;
|
|
97
|
+
_priceCount;
|
|
98
|
+
_counts;
|
|
43
99
|
_updateCount;
|
|
44
100
|
entropy;
|
|
45
101
|
normalizedEntropy;
|
|
@@ -73,16 +129,55 @@ class PermutationEntropyAnalyzer {
|
|
|
73
129
|
this.window = window;
|
|
74
130
|
// Buffer must hold `window` patterns; each pattern spans (m-1)*delay+1 prices.
|
|
75
131
|
this._bufSize = this.window + (this.m - 1) * this.delay;
|
|
132
|
+
// Exposed so the regime gate can ask how much history this analyzer's
|
|
133
|
+
// state actually depends on, instead of mirroring these defaults (which
|
|
134
|
+
// drift the moment a default changes).
|
|
135
|
+
this.bufferBars = this._bufSize;
|
|
76
136
|
let f = 1;
|
|
77
137
|
for (let i = 2; i <= this.m; i++)
|
|
78
138
|
f *= i;
|
|
79
139
|
this._maxEntropy = Math.log(f); // log(m!)
|
|
80
|
-
this._prices =
|
|
140
|
+
this._prices = new Float64Array(this._bufSize);
|
|
141
|
+
this._priceCount = 0;
|
|
142
|
+
this._counts = new Map();
|
|
81
143
|
this._updateCount = 0;
|
|
82
144
|
this.entropy = 0;
|
|
83
145
|
this.normalizedEntropy = 0;
|
|
84
146
|
this.isReady = false;
|
|
85
147
|
}
|
|
148
|
+
/** Number of ordinal patterns currently inside a full buffer. */
|
|
149
|
+
get _numPatterns() {
|
|
150
|
+
return this._priceCount - (this.m - 1) * this.delay;
|
|
151
|
+
}
|
|
152
|
+
_addPattern(key) {
|
|
153
|
+
this._counts.set(key, (this._counts.get(key) ?? 0) + 1);
|
|
154
|
+
}
|
|
155
|
+
_removePattern(key) {
|
|
156
|
+
const current = this._counts.get(key);
|
|
157
|
+
if (current === undefined)
|
|
158
|
+
return;
|
|
159
|
+
if (current <= 1)
|
|
160
|
+
this._counts.delete(key);
|
|
161
|
+
else
|
|
162
|
+
this._counts.set(key, current - 1);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Shannon entropy: H = -Σ p_k * log(p_k) over the live pattern counts.
|
|
166
|
+
* Iterating the (small, <= min(window, m!)) count map is cheaper than
|
|
167
|
+
* maintaining a running sum and keeps the arithmetic identical to counting
|
|
168
|
+
* the window from scratch.
|
|
169
|
+
*/
|
|
170
|
+
_entropyFromCounts() {
|
|
171
|
+
const numPatterns = this._numPatterns;
|
|
172
|
+
if (numPatterns <= 0)
|
|
173
|
+
return 0;
|
|
174
|
+
let entropy = 0;
|
|
175
|
+
for (const count of this._counts.values()) {
|
|
176
|
+
const p = count / numPatterns;
|
|
177
|
+
entropy -= p * Math.log(p);
|
|
178
|
+
}
|
|
179
|
+
return entropy;
|
|
180
|
+
}
|
|
86
181
|
/**
|
|
87
182
|
* Feed a new price and return analysis.
|
|
88
183
|
* @param {number} price
|
|
@@ -92,28 +187,36 @@ class PermutationEntropyAnalyzer {
|
|
|
92
187
|
if (!Number.isFinite(price) || price <= 0) {
|
|
93
188
|
throw new Error('price must be a positive finite number');
|
|
94
189
|
}
|
|
95
|
-
this.
|
|
96
|
-
|
|
97
|
-
|
|
190
|
+
const m = this.m;
|
|
191
|
+
const delay = this.delay;
|
|
192
|
+
const wasFull = this._priceCount === this._bufSize;
|
|
193
|
+
if (wasFull) {
|
|
194
|
+
// The pattern that starts at index 0 is the one leaving the window;
|
|
195
|
+
// drop it before the buffer shifts left.
|
|
196
|
+
this._removePattern(ordinalPatternKey(this._prices, 0, m, delay));
|
|
197
|
+
this._prices.copyWithin(0, 1);
|
|
198
|
+
this._priceCount--;
|
|
199
|
+
}
|
|
200
|
+
this._prices[this._priceCount++] = price;
|
|
98
201
|
this._updateCount++;
|
|
99
|
-
if (this.
|
|
202
|
+
if (this._priceCount < this._bufSize) {
|
|
100
203
|
this.isReady = false;
|
|
101
204
|
return this.getAnalysis();
|
|
102
205
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
for (let i = 0; i < numPatterns; i++) {
|
|
108
|
-
const key = ordinalPattern(this._prices, i, this.m, this.delay);
|
|
109
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
206
|
+
if (wasFull) {
|
|
207
|
+
// Steady state: only the newly completed pattern needs counting,
|
|
208
|
+
// because the leaving one was just decremented.
|
|
209
|
+
this._addPattern(ordinalPatternKey(this._prices, this._numPatterns - 1, m, delay));
|
|
110
210
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
211
|
+
else {
|
|
212
|
+
// First time the buffer is full: seed the counts with every pattern
|
|
213
|
+
// the window currently contains.
|
|
214
|
+
const numPatterns = this._numPatterns;
|
|
215
|
+
for (let i = 0; i < numPatterns; i++) {
|
|
216
|
+
this._addPattern(ordinalPatternKey(this._prices, i, m, delay));
|
|
217
|
+
}
|
|
116
218
|
}
|
|
219
|
+
const entropy = this._entropyFromCounts();
|
|
117
220
|
this.entropy = entropy;
|
|
118
221
|
this.normalizedEntropy = this._maxEntropy > 0 ? entropy / this._maxEntropy : 0;
|
|
119
222
|
this.isReady = true;
|
|
@@ -145,5 +248,5 @@ class PermutationEntropyAnalyzer {
|
|
|
145
248
|
};
|
|
146
249
|
}
|
|
147
250
|
}
|
|
148
|
-
export { PermutationEntropyAnalyzer };
|
|
251
|
+
export { PermutationEntropyAnalyzer, ordinalPatternKey };
|
|
149
252
|
//# sourceMappingURL=permutation_entropy_analyzer.js.map
|
|
@@ -1,3 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bars of history the analyzers' rolling state can depend on.
|
|
3
|
+
*
|
|
4
|
+
* Taken from the analyzer INSTANCES (each exposes `bufferBars`), never from a
|
|
5
|
+
* second copy of their constructor defaults: a mirrored formula silently
|
|
6
|
+
* under-estimates whenever a default or a buffer formula changes. Being too
|
|
7
|
+
* large only costs a missed resume; being too small would let the resume carry
|
|
8
|
+
* `cached.series` entries that were computed from bars the incoming window no
|
|
9
|
+
* longer contains (the analyzers' final state is unaffected either way — see
|
|
10
|
+
* _locateResume). An analyzer that does not report a usable size fails closed
|
|
11
|
+
* (MAX_SAFE_INTEGER), which disables resuming for that cache entry.
|
|
12
|
+
*/
|
|
13
|
+
declare function _analyzerBufferSize(hurst: any, pe: any): number;
|
|
14
|
+
/**
|
|
15
|
+
* Locate how `closes` advances the cached window, or null when they are not
|
|
16
|
+
* related that way.
|
|
17
|
+
*
|
|
18
|
+
* Returns `{ k, shared }` where `closes[0..shared)` is exactly
|
|
19
|
+
* `cached[k..cached.length)` — i.e. the incoming window dropped k leading bars
|
|
20
|
+
* and appended `closes.length - shared` new ones. `k = 0, shared =
|
|
21
|
+
* cached.length` is the plain "one or more bars were added" case.
|
|
22
|
+
*
|
|
23
|
+
* The shift is pinned by matching the cached window's newest bar inside the
|
|
24
|
+
* incoming window, then the ENTIRE shared region is verified, so a coincidental
|
|
25
|
+
* price match cannot pass. NaN-aware, like the pre-cache implementation.
|
|
26
|
+
*
|
|
27
|
+
* When the cached newest bar occurs at more than one position and more than one
|
|
28
|
+
* candidate alignment verifies, the window is ambiguous and the function
|
|
29
|
+
* declines to resume. This is hygiene, not a safety requirement: any verified
|
|
30
|
+
* alignment with `shared >= minShared` is exact by construction, because that
|
|
31
|
+
* requirement guarantees the resumed stream's last `bufferBars` values are the
|
|
32
|
+
* same values the incoming stream ends with — the analyzers are rolling, so
|
|
33
|
+
* their final state (and every series entry from that point on) is identical
|
|
34
|
+
* whichever alignment is chosen. Declining only avoids anchoring the
|
|
35
|
+
* `cached.series` carry-over — whose early entries rest on bars the window may
|
|
36
|
+
* no longer contain — to a pattern match we did not intend. It can only arise
|
|
37
|
+
* on degenerate periodic windows, and a cold recompute there is cheap and
|
|
38
|
+
* honest.
|
|
39
|
+
*/
|
|
40
|
+
declare function _locateResume(cached: number[], closes: number[], minShared: number): {
|
|
41
|
+
k: number;
|
|
42
|
+
shared: number;
|
|
43
|
+
} | null;
|
|
44
|
+
/**
|
|
45
|
+
* Drop all memoized analyzer state.
|
|
46
|
+
*
|
|
47
|
+
* A test seam today, not a runtime path: the cycle resolves its parameters once
|
|
48
|
+
* per run, so there is no in-process config reload to invalidate state for, and
|
|
49
|
+
* a parameter change is already detected by `regimeParamsKey` (it forces a full
|
|
50
|
+
* recompute). Production never calls this — it exists so a test can assert a
|
|
51
|
+
* cold run against a warm cache without leaking entries between cases.
|
|
52
|
+
*/
|
|
53
|
+
declare function _resetRegimeCache(): void;
|
|
1
54
|
/**
|
|
2
55
|
* Compute the Hurst+PE regime multiplier from a price series.
|
|
3
56
|
*
|
|
@@ -5,9 +58,13 @@
|
|
|
5
58
|
* canonical pure implementation in strategies/regime_interp.ts (shared with the
|
|
6
59
|
* browser-embedded chart scripts).
|
|
7
60
|
*
|
|
8
|
-
* Feeds
|
|
61
|
+
* Feeds prices through HurstAnalyzer and PermutationEntropyAnalyzer,
|
|
9
62
|
* then bilinear-interpolates the regime table to produce a multiplier that
|
|
10
|
-
* gates the AMA slope offset in production weight computation.
|
|
63
|
+
* gates the AMA slope offset in production weight computation. When
|
|
64
|
+
* `opts.cacheKey` is set, a window that ADVANCES the memoized one (k leading
|
|
65
|
+
* bars dropped, m appended) resumes from the cached analyzer state and feeds
|
|
66
|
+
* only the appended bars; anything else recomputes from scratch. See the cache
|
|
67
|
+
* notes above.
|
|
11
68
|
*
|
|
12
69
|
* @param {number[]} closes - Full close price series (same array used for AMA)
|
|
13
70
|
* @param {Object} [opts]
|
|
@@ -15,6 +72,7 @@
|
|
|
15
72
|
* @param {Array} [opts.regimeTable] - Custom 3x3 regime multiplier table
|
|
16
73
|
* @param {number} [opts.hurstZoneBand] - Override Hurst neutral-zone width
|
|
17
74
|
* @param {Array} [opts.peNodes] - Override entropy thresholds
|
|
75
|
+
* @param {string} [opts.cacheKey] - Stable per-market key enabling cross-cycle reuse
|
|
18
76
|
* @param {Object} [opts.hurstConfig] - Override for HurstAnalyzer config
|
|
19
77
|
* @param {Object} [opts.peConfig] - Override for PermutationEntropyAnalyzer config
|
|
20
78
|
* @returns {{ multiplier: number, hurst: number|null, pe: number|null,
|
|
@@ -38,5 +96,5 @@ declare function computeRegimeMultiplier(closes: any, opts?: any): {
|
|
|
38
96
|
isReady: boolean;
|
|
39
97
|
series: number[];
|
|
40
98
|
};
|
|
41
|
-
export { computeRegimeMultiplier };
|
|
99
|
+
export { computeRegimeMultiplier, _resetRegimeCache, _locateResume, _analyzerBufferSize };
|
|
42
100
|
//# sourceMappingURL=regime_gate.d.ts.map
|