aiden-shared-calculations-unified 1.0.96 → 1.0.98

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.
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @fileoverview Calculation (Pass 1) for total long figures.
3
+ * REFACTORED: Uses INSIGHTS DATA.
4
+ * TYPE: META
5
+ */
6
+ class InsightsTotalLongFigures {
7
+ constructor() {
8
+ this.totalPositions = 0;
9
+ }
10
+
11
+ static getMetadata() {
12
+ return {
13
+ type: 'meta',
14
+ rootDataDependencies: ['insights'],
15
+ isHistorical: false,
16
+ category: 'core_sentiment'
17
+ };
18
+ }
19
+
20
+ static getDependencies() { return []; }
21
+
22
+ static getSchema() {
23
+ return {
24
+ "type": "object",
25
+ "properties": {
26
+ "total_long_exposure_weight": { "type": "number" },
27
+ "total_positions_count": { "type": "number" }
28
+ },
29
+ "required": ["total_long_exposure_weight", "total_positions_count"]
30
+ };
31
+ }
32
+
33
+ process(context) {
34
+ const { insights: insightsHelper } = context.math;
35
+ const insights = insightsHelper.getInsights(context);
36
+
37
+ if (!insights) return;
38
+
39
+ for (const insight of insights) {
40
+ this.totalPositions += insightsHelper.getLongCount(insight);
41
+ }
42
+ }
43
+
44
+ getResult() {
45
+ return {
46
+ total_long_exposure_weight: 0, // Not available in Insights
47
+ total_positions_count: this.totalPositions
48
+ };
49
+ }
50
+
51
+ reset() {
52
+ this.totalPositions = 0;
53
+ }
54
+ }
55
+
56
+ module.exports = InsightsTotalLongFigures;
@@ -0,0 +1,32 @@
1
+ class InsightsDailyBoughtVsSoldCount {
2
+ constructor() { this.results = {}; }
3
+ static getMetadata() { return { type: 'meta', rootDataDependencies: ['insights'], isHistorical: true, category: 'core_metrics' }; }
4
+ static getDependencies() { return []; }
5
+ static getSchema() {
6
+ const tickerSchema = { "type": "object", "properties": { "positions_bought": { "type": "number" }, "positions_sold": { "type": "number" }, "net_change": { "type": "number" } }, "required": ["positions_bought", "positions_sold", "net_change"] };
7
+ return { "type": "object", "patternProperties": { "^.*$": tickerSchema } };
8
+ }
9
+ process(context) {
10
+ const { insights: insightsHelper } = context.math;
11
+ const insights = insightsHelper.getInsights(context);
12
+ const { mappings } = context;
13
+ const tickerMap = mappings.instrumentToTicker || {};
14
+
15
+ if (!insights || !Array.isArray(insights)) return; // FIX: Strict array check
16
+
17
+ for (const insight of insights) {
18
+ const instId = insight.instrumentId;
19
+ const ticker = tickerMap[instId] || `id_${instId}`;
20
+ const netChange = insightsHelper.getNetOwnershipChange(insight);
21
+ const bought = netChange > 0 ? netChange : 0;
22
+ const sold = netChange < 0 ? Math.abs(netChange) : 0;
23
+
24
+ if (bought > 0 || sold > 0) {
25
+ this.results[ticker] = { positions_bought: bought, positions_sold: sold, net_change: netChange };
26
+ }
27
+ }
28
+ }
29
+ async getResult() { return this.results; }
30
+ reset() { this.results = {}; }
31
+ }
32
+ module.exports = InsightsDailyBoughtVsSoldCount;
@@ -0,0 +1,31 @@
1
+ class InsightsDailyOwnershipDelta {
2
+ constructor() { this.results = {}; }
3
+ static getMetadata() { return { type: 'meta', rootDataDependencies: ['insights'], isHistorical: true, category: 'core_metrics' }; }
4
+ static getDependencies() { return []; }
5
+ static getSchema() {
6
+ const tickerSchema = { "type": "object", "properties": { "owners_added": { "type": "number" }, "owners_removed": { "type": "number" }, "net_ownership_change": { "type": "number" } }, "required": ["owners_added", "owners_removed", "net_ownership_change"] };
7
+ return { "type": "object", "patternProperties": { "^.*$": tickerSchema } };
8
+ }
9
+ process(context) {
10
+ const { insights: insightsHelper } = context.math;
11
+ const insights = insightsHelper.getInsights(context);
12
+ const { mappings } = context;
13
+ const tickerMap = mappings.instrumentToTicker || {};
14
+
15
+ if (!insights || !Array.isArray(insights)) return; // FIX: Strict array check
16
+
17
+ for (const insight of insights) {
18
+ const netChange = insightsHelper.getNetOwnershipChange(insight);
19
+ if (netChange === 0) continue;
20
+ const instId = insight.instrumentId;
21
+ const ticker = tickerMap[instId] || `id_${instId}`;
22
+ const added = netChange > 0 ? netChange : 0;
23
+ const removed = netChange < 0 ? Math.abs(netChange) : 0;
24
+
25
+ this.results[ticker] = { owners_added: added, owners_removed: removed, net_ownership_change: netChange };
26
+ }
27
+ }
28
+ async getResult() { return this.results; }
29
+ reset() { this.results = {}; }
30
+ }
31
+ module.exports = InsightsDailyOwnershipDelta;
@@ -0,0 +1,30 @@
1
+ class InsightsSentimentPerStock {
2
+ constructor() { this.results = {}; }
3
+ static getSchema() {
4
+ const tickerSchema = { "type": "object", "properties": { "long_count": { "type": "number" }, "short_count": { "type": "number" }, "sentiment_ratio": { "type": ["number", "null"] } }, "required": ["long_count", "short_count", "sentiment_ratio"] };
5
+ return { "type": "object", "patternProperties": { "^.*$": tickerSchema } };
6
+ }
7
+ static getMetadata() { return { type: 'meta', rootDataDependencies: ['insights'], isHistorical: false, category: 'core_sentiment' }; }
8
+ static getDependencies() { return []; }
9
+ process(context) {
10
+ const { insights: insightsHelper } = context.math;
11
+ const insights = insightsHelper.getInsights(context);
12
+ const { mappings } = context;
13
+ const tickerMap = mappings.instrumentToTicker || {};
14
+
15
+ if (!insights || !Array.isArray(insights)) return; // FIX: Strict array check
16
+
17
+ for (const insight of insights) {
18
+ const longCount = insightsHelper.getLongCount(insight);
19
+ const shortCount = insightsHelper.getShortCount(insight);
20
+ if (longCount === 0 && shortCount === 0) continue;
21
+ const instId = insight.instrumentId;
22
+ const ticker = tickerMap[instId] || `id_${instId}`;
23
+
24
+ this.results[ticker] = { long_count: longCount, short_count: shortCount, sentiment_ratio: (shortCount > 0) ? (longCount / shortCount) : null };
25
+ }
26
+ }
27
+ async getResult() { return this.results; }
28
+ reset() { this.results = {}; }
29
+ }
30
+ module.exports = InsightsSentimentPerStock;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @fileoverview Calculation (Pass 1) for long figures per sector.
3
+ * REFACTORED: Uses INSIGHTS DATA.
4
+ * TYPE: META
5
+ */
6
+ class InsightsTotalLongPerSector {
7
+ constructor() {
8
+ this.sectorData = new Map();
9
+ }
10
+
11
+ static getMetadata() {
12
+ return {
13
+ type: 'meta',
14
+ rootDataDependencies: ['insights'],
15
+ isHistorical: false,
16
+ category: 'core_sentiment'
17
+ };
18
+ }
19
+
20
+ static getDependencies() { return []; }
21
+
22
+ static getSchema() {
23
+ const schema = {
24
+ "type": "object",
25
+ "properties": {
26
+ "total_long_exposure_weight": { "type": "number" },
27
+ "total_positions_count": { "type": "number" }
28
+ },
29
+ "required": ["total_long_exposure_weight", "total_positions_count"]
30
+ };
31
+ return { "type": "object", "patternProperties": { "^.*$": schema } };
32
+ }
33
+
34
+ process(context) {
35
+ const { insights: insightsHelper } = context.math;
36
+ const insights = insightsHelper.getInsights(context);
37
+ const { mappings } = context;
38
+ const sectorMap = mappings.instrumentToSector || {};
39
+
40
+ if (!insights) return;
41
+
42
+ for (const insight of insights) {
43
+ const longCount = insightsHelper.getLongCount(insight);
44
+ if (longCount <= 0) continue;
45
+
46
+ const sector = sectorMap[insight.instrumentId] || 'Unknown';
47
+
48
+ if (!this.sectorData.has(sector)) {
49
+ this.sectorData.set(sector, { count: 0 });
50
+ }
51
+ const data = this.sectorData.get(sector);
52
+ data.count += longCount;
53
+ }
54
+ }
55
+
56
+ async getResult() {
57
+ const result = {};
58
+ for (const [sector, data] of this.sectorData.entries()) {
59
+ if (data.count > 0) {
60
+ result[sector] = {
61
+ total_long_exposure_weight: 0, // Not available
62
+ total_positions_count: data.count
63
+ };
64
+ }
65
+ }
66
+ return result;
67
+ }
68
+
69
+ reset() {
70
+ this.sectorData.clear();
71
+ }
72
+ }
73
+ module.exports = InsightsTotalLongPerSector;
@@ -0,0 +1,19 @@
1
+ class InsightsTotalPositionsHeld {
2
+ constructor() { this.totalPositions = 0; }
3
+ static getMetadata() { return { type: 'meta', rootDataDependencies: ['insights'], isHistorical: false, category: 'Global-Platform-Metrics' }; }
4
+ static getDependencies() { return []; }
5
+ static getSchema() { return { "type": "object", "properties": { "total_positions_count": { "type": "number" } }, "required": ["total_positions_count"] }; }
6
+ process(context) {
7
+ const { insights: insightsHelper } = context.math;
8
+ const insights = insightsHelper.getInsights(context);
9
+
10
+ if (!insights || !Array.isArray(insights)) return; // FIX: Strict array check
11
+
12
+ for (const insight of insights) {
13
+ this.totalPositions += insightsHelper.getTotalOwners(insight);
14
+ }
15
+ }
16
+ getResult() { return { total_positions_count: this.totalPositions }; }
17
+ reset() { this.totalPositions = 0; }
18
+ }
19
+ module.exports = InsightsTotalPositionsHeld;
@@ -16,13 +16,14 @@ class LiquidityVacuum {
16
16
 
17
17
  process(context) {
18
18
  const { computed, math } = context;
19
- const { distribution } = math; // Correctly accesses 'distribution' from context
19
+ const { distribution } = math;
20
20
 
21
21
  const tickers = Object.keys(computed['asset-cost-basis-profile'] || {});
22
22
 
23
23
  for (const ticker of tickers) {
24
24
  const data = computed['asset-cost-basis-profile'][ticker];
25
- if (!data) continue;
25
+ // BUG FIX: Ensure data and profile exist before accessing length/integration
26
+ if (!data || !data.profile || !Array.isArray(data.profile)) continue;
26
27
 
27
28
  const current = data.current_price;
28
29
  const totalInv = data.total_inventory_weight;
@@ -16,7 +16,7 @@ class WinnerLoserFlow {
16
16
  userType: 'all',
17
17
  category: 'helix'
18
18
  };
19
- }
19
+ }s
20
20
 
21
21
  static getDependencies() { return []; }
22
22
 
@@ -1,7 +1,6 @@
1
1
  /**
2
- * @fileoverview Mimetic Latency Oscillator (MLO) v2.2
3
- * Measures the lag between "Smart Money" flow and "Herd" conviction.
4
- * Self-healing state: Warms up over 15-30 days automatically.
2
+ * @fileoverview Mimetic Latency Oscillator (MLO) v2.3
3
+ * FIXED: Safe state access to prevent 'undefined' property errors.
5
4
  */
6
5
  class MimeticLatencyOscillator {
7
6
  constructor() {
@@ -47,25 +46,24 @@ class MimeticLatencyOscillator {
47
46
  const rawFlow = SignalPrimitives.getMetric(computed, 'skilled-cohort-flow', ticker, 'net_flow_pct', 0);
48
47
  const rawHerd = SignalPrimitives.getMetric(computed, 'herd-consensus-score', ticker, 'herd_conviction_score', 0);
49
48
 
50
- // 2. Restore State (Handle Cold Start)
49
+ // 2. Restore State (FIX: Safe Access)
51
50
  const prevResult = SignalPrimitives.getPreviousState(previousComputed, 'mimetic-latency', ticker);
52
- const prevState = prevResult ? prevResult._state : { flow_buffer: [], herd_buffer: [], last_flow: 0, last_herd: 0 };
51
+ const prevState = prevResult ? prevResult._state : null;
53
52
 
54
- const prevFlow = (prevState.last_flow !== undefined) ? prevState.last_flow : 0;
55
- const prevHerd = (prevState.last_herd !== undefined) ? prevState.last_herd : 0;
53
+ // FIX: Check if prevState exists before accessing properties
54
+ const prevFlow = (prevState && prevState.last_flow !== undefined) ? prevState.last_flow : 0;
55
+ const prevHerd = (prevState && prevState.last_herd !== undefined) ? prevState.last_herd : 0;
56
+ const flowBuffer = (prevState && Array.isArray(prevState.flow_buffer)) ? [...prevState.flow_buffer] : [];
57
+ const herdBuffer = (prevState && Array.isArray(prevState.herd_buffer)) ? [...prevState.herd_buffer] : [];
56
58
 
57
59
  // 3. Calculate Detrended Delta
58
60
  const flowDelta = rawFlow - prevFlow;
59
61
  const herdDelta = rawHerd - prevHerd;
60
62
 
61
63
  // 4. Update Buffers
62
- let flowBuffer = [...(prevState.flow_buffer || [])];
63
- let herdBuffer = [...(prevState.herd_buffer || [])];
64
-
65
64
  flowBuffer.push(flowDelta);
66
65
  herdBuffer.push(herdDelta);
67
66
 
68
- // Cap memory usage
69
67
  if (flowBuffer.length > this.windowSize) flowBuffer.shift();
70
68
  if (herdBuffer.length > this.windowSize) herdBuffer.shift();
71
69
 
@@ -74,7 +72,6 @@ class MimeticLatencyOscillator {
74
72
  let bestLag = 0;
75
73
  let regime = "WARM_UP";
76
74
 
77
- // Only compute if we have statistical significance (>= 15 samples)
78
75
  if (flowBuffer.length >= 15) {
79
76
  for (let k = 0; k <= this.maxLag; k++) {
80
77
  const len = flowBuffer.length;
@@ -91,7 +88,6 @@ class MimeticLatencyOscillator {
91
88
  }
92
89
  }
93
90
 
94
- // 6. Regime Classification
95
91
  if (maxCorr > 0.3) {
96
92
  if (bestLag >= 3) regime = "EARLY_ALPHA";
97
93
  else if (bestLag >= 1) regime = "MARKUP";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.96",
3
+ "version": "1.0.98",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [