aiden-shared-calculations-unified 1.0.103 → 1.0.105

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,97 @@
1
+ Scanning 92 files...
2
+
3
+ --- Files MISSING getSchema() ---
4
+
5
+ --- Files WITH getSchema() ---
6
+ capitulation\asset-volatility-estimator.js
7
+ capitulation\retail-capitulation-risk-forecast.js
8
+ core\asset-cost-basis-profile.js
9
+ core\asset-pnl-status.js
10
+ core\asset-position-size.js
11
+ core\average-daily-pnl-all-users.js
12
+ core\average-daily-pnl-per-sector.js
13
+ core\average-daily-pnl-per-stock.js
14
+ core\average-daily-position-pnl.js
15
+ core\crowd-cost-basis.js
16
+ core\holding-duration-per-asset.js
17
+ core\insights-daily-bought-vs-sold-count.js
18
+ core\insights-daily-ownership-delta.js
19
+ core\insights-sentimet-per-stock.js
20
+ core\insights-total-long-per-sector.js
21
+ core\Insights-total-long-per-stock.js
22
+ core\insights-total-positions-held.js
23
+ core\instrument-price-change-1d.js
24
+ core\instrument-price-momentum-20d.js
25
+ core\leverage-divergence.js
26
+ core\liquidation-cascade.js
27
+ core\long-position-per-stock.js
28
+ core\overall-holding-duration.js
29
+ core\overall-profitability-ratio.js
30
+ core\ownership-vs-performance-ytd.js
31
+ core\ownership-vs-volatility.js
32
+ core\platform-buy-sell-sentiment.js
33
+ core\platform-daily-bought-vs-sold-count.js
34
+ core\platform-daily-ownership-delta.js
35
+ core\platform-ownership-per-sector.js
36
+ core\platform-total-positions-held.js
37
+ core\pnl-distribution-per-stock.js
38
+ core\price-metrics.js
39
+ core\profitability-ratio-per-sector.js
40
+ core\profitability-ratio-per-stock.js
41
+ core\profitability-skew-per-stock.js
42
+ core\profitable-and-unprofitable-status.js
43
+ core\sentiment-per-stock.js
44
+ core\short-interest-growth.js
45
+ core\short-position-per-stock.js
46
+ core\social-activity-aggregation.js
47
+ core\social-asset-posts-trend.js
48
+ core\social-event-correlation.js
49
+ core\social-sentiment-aggregation.js
50
+ core\social-top-mentioned-words.js
51
+ core\social-topic-interest-evolution.js
52
+ core\social-topic-sentiment-matrix.js
53
+ core\social-word-mentions-trend.js
54
+ core\speculator-asset-sentiment.js
55
+ core\speculator-danger-zone.js
56
+ core\speculator-distance-to-stop-loss-per-leverage.js
57
+ core\speculator-distance-to-tp-per-leverage.js
58
+ core\speculator-entry-distance-to-sl-per-leverage.js
59
+ core\speculator-entry-distance-to-tp-per-leverage.js
60
+ core\speculator-leverage-per-asset.js
61
+ core\speculator-leverage-per-sector.js
62
+ core\speculator-risk-reward-ratio-per-asset.js
63
+ core\speculator-stop-loss-distance-by-sector-short-long-breakdown.js
64
+ core\speculator-stop-loss-distance-by-ticker-short-long-breakdown.js
65
+ core\speculator-stop-loss-per-asset.js
66
+ core\speculator-take-profit-per-asset.js
67
+ core\speculator-tsl-per-asset.js
68
+ core\total-long-figures.js
69
+ core\total-long-per-sector.js
70
+ core\total-short-figures.js
71
+ core\total-short-per-sector.js
72
+ core\trending-ownership-momentum.js
73
+ core\user-history-reconstructor.js
74
+ core\users-processed.js
75
+ gauss\cohort-capital-flow.js
76
+ gauss\cohort-definer.js
77
+ gauss\daily-dna-filter.js
78
+ gauss\gauss-divergence-signal.js
79
+ gem\cohort-momentum-state.js
80
+ gem\cohort-skill-definition.js
81
+ gem\platform-conviction-divergence.js
82
+ gem\quant-skill-alpha-signal.js
83
+ gem\skilled-cohort-flow.js
84
+ gem\skilled-unskilled-divergence.js
85
+ gem\unskilled-cohort-flow.js
86
+ ghost-book\cost-basis-density.js
87
+ ghost-book\liquidity-vacuum.js
88
+ ghost-book\retail-gamma-exposure.js
89
+ helix\helix-contrarian-signal.js
90
+ helix\herd-consensus-score.js
91
+ helix\winner-loser-flow.js
92
+ predicative-alpha\cognitive-dissonance.js
93
+ predicative-alpha\diamond-hand-fracture.js
94
+ predicative-alpha\mimetic-latency.js
95
+ pyro\risk-appetite-index.js
96
+ pyro\squeeze-potential.js
97
+ pyro\volatility-signal.js
@@ -0,0 +1,47 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const rootDir = __dirname;
5
+
6
+ function getAllFiles(dirPath, arrayOfFiles) {
7
+ const files = fs.readdirSync(dirPath);
8
+
9
+ arrayOfFiles = arrayOfFiles || [];
10
+
11
+ files.forEach(function (file) {
12
+ if (fs.statSync(dirPath + "/" + file).isDirectory()) {
13
+ arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
14
+ } else {
15
+ if (file.endsWith('.js') && file !== 'audit_schemas.js') {
16
+ arrayOfFiles.push(path.join(dirPath, "/", file));
17
+ }
18
+ }
19
+ });
20
+
21
+ return arrayOfFiles;
22
+ }
23
+
24
+ const files = getAllFiles(rootDir);
25
+ let missingSchema = [];
26
+ let hasSchema = [];
27
+
28
+ const output = [];
29
+ output.push(`Scanning ${files.length} files...`);
30
+
31
+ files.forEach(file => {
32
+ const content = fs.readFileSync(file, 'utf8');
33
+ if (content.includes('static getSchema()')) {
34
+ hasSchema.push(file);
35
+ } else {
36
+ missingSchema.push(file);
37
+ }
38
+ });
39
+
40
+ output.push('\n--- Files MISSING getSchema() ---');
41
+ missingSchema.forEach(f => output.push(path.relative(rootDir, f)));
42
+
43
+ output.push('\n--- Files WITH getSchema() ---');
44
+ hasSchema.forEach(f => output.push(path.relative(rootDir, f)));
45
+
46
+ fs.writeFileSync(path.join(rootDir, 'audit_results.txt'), output.join('\n'));
47
+ console.log('Audit complete. Results written to audit_results.txt');
@@ -28,7 +28,7 @@ class InsightsTotalPositionsHeld {
28
28
  if (insights.length === 0) return;
29
29
 
30
30
  for (const insight of insights) {
31
- this.totalPositions += insightsHelper.getTotalOwners(insight);
31
+ this.totalPositions += insightsHelper.getLongCount(insight); // Corrected to use getLongCount
32
32
  }
33
33
  }
34
34
 
@@ -1,6 +1,6 @@
1
1
  class CrowdCostBasis {
2
2
  constructor() {
3
- this.results = {};
3
+ this.results = {};
4
4
  }
5
5
 
6
6
  static getMetadata() {
@@ -18,9 +18,23 @@ class CrowdCostBasis {
18
18
  return ['user-history-reconstructor'];
19
19
  }
20
20
 
21
+ static getSchema() {
22
+ const schema = {
23
+ "type": "object",
24
+ "properties": {
25
+ "avgEntry": { "type": "number" },
26
+ "holderCount": { "type": "number" },
27
+ "profitabilityPct": { "type": "number" },
28
+ "state": { "type": "string" }
29
+ },
30
+ "required": ["avgEntry", "holderCount", "profitabilityPct", "state"]
31
+ };
32
+ return { "type": "object", "patternProperties": { "^.*$": schema } };
33
+ }
34
+
21
35
  async process(context) {
22
36
  const { computed, prices, math } = context;
23
-
37
+
24
38
  // 1. Access the output of the Standard calculation
25
39
  const userReconstructions = computed['user-history-reconstructor'];
26
40
  if (!userReconstructions) return;
@@ -30,12 +44,12 @@ class CrowdCostBasis {
30
44
  // 2. Iterate over all users' reconstructed states
31
45
  for (const userId in userReconstructions) {
32
46
  const userPortfolio = userReconstructions[userId];
33
-
47
+
34
48
  for (const ticker in userPortfolio) {
35
49
  const position = userPortfolio[ticker];
36
-
50
+
37
51
  if (!aggregator[ticker]) aggregator[ticker] = { sumEntry: 0, count: 0 };
38
-
52
+
39
53
  // Aggregating global average entry
40
54
  aggregator[ticker].sumEntry += position.avgEntry;
41
55
  aggregator[ticker].count++;
@@ -48,7 +62,7 @@ class CrowdCostBasis {
48
62
  if (data.count < 10) continue; // Noise filter
49
63
 
50
64
  const globalAvgEntry = data.sumEntry / data.count;
51
-
65
+
52
66
  // Get today's closing price
53
67
  const priceHistory = math.priceExtractor.getHistory(prices, ticker);
54
68
  // The last item in price history for this context is "Today"
@@ -16,12 +16,27 @@ class LeverageDivergence {
16
16
  return ['user-history-reconstructor'];
17
17
  }
18
18
 
19
+ static getSchema() {
20
+ const schema = {
21
+ "type": "object",
22
+ "properties": {
23
+ "levHolders": { "type": "number" },
24
+ "spotHolders": { "type": "number" },
25
+ "levDelta": { "type": "number" },
26
+ "spotDelta": { "type": "number" },
27
+ "signal": { "type": "string" }
28
+ },
29
+ "required": ["levHolders", "spotHolders", "levDelta", "spotDelta", "signal"]
30
+ };
31
+ return { "type": "object", "patternProperties": { "^.*$": schema } };
32
+ }
33
+
19
34
  async process(context) {
20
35
  const { computed, previousComputed } = context;
21
-
36
+
22
37
  const currentReconstruction = computed['user-history-reconstructor'];
23
38
  // Access SELF from yesterday
24
- const previousResult = previousComputed['leverage-divergence'];
39
+ const previousResult = previousComputed['leverage-divergence'];
25
40
 
26
41
  const currentAgg = {}; // { AAPL: { levHolders: 0, spotHolders: 0 } }
27
42
 
@@ -30,9 +45,9 @@ class LeverageDivergence {
30
45
  const userPortfolio = currentReconstruction[userId];
31
46
  for (const ticker in userPortfolio) {
32
47
  const pos = userPortfolio[ticker];
33
-
48
+
34
49
  if (!currentAgg[ticker]) currentAgg[ticker] = { levHolders: 0, spotHolders: 0 };
35
-
50
+
36
51
  // If avg leverage > 1.1, count as "Leveraged Holder"
37
52
  if (pos.avgLeverage > 1.1) {
38
53
  currentAgg[ticker].levHolders++;
@@ -56,10 +71,10 @@ class LeverageDivergence {
56
71
  const spotDelta = curr.spotHolders - prev.spotHolders;
57
72
 
58
73
  let signal = 'NEUTRAL';
59
-
74
+
60
75
  // Retail (Spot) Buying + Speculators (Lev) Selling = Smart Money Exit
61
- if (spotDelta > 0 && levDelta < 0) signal = 'SMART_EXIT';
62
-
76
+ if (spotDelta > 0 && levDelta < 0) signal = 'SMART_EXIT';
77
+
63
78
  // Retail (Spot) Selling + Speculators (Lev) Buying = High Conviction Pump
64
79
  if (spotDelta < 0 && levDelta > 0) signal = 'SPECULATIVE_PUMP';
65
80
 
@@ -8,7 +8,7 @@ class LiquidationCascade {
8
8
  category: 'History Reconstruction',
9
9
  userType: 'n/a',
10
10
  isHistorical: false,
11
- rootDataDependencies: []
11
+ rootDataDependencies: []
12
12
  };
13
13
  }
14
14
 
@@ -16,23 +16,37 @@ class LiquidationCascade {
16
16
  return ['user-history-reconstructor'];
17
17
  }
18
18
 
19
+ static getSchema() {
20
+ const schema = {
21
+ "type": "object",
22
+ "properties": {
23
+ "totalClosures": { "type": "number" },
24
+ "forcedClosures": { "type": "number" },
25
+ "painIndex": { "type": "number" },
26
+ "isFlushEvent": { "type": "boolean" }
27
+ },
28
+ "required": ["totalClosures", "forcedClosures", "painIndex", "isFlushEvent"]
29
+ };
30
+ return { "type": "object", "patternProperties": { "^.*$": schema } };
31
+ }
32
+
19
33
  async process(context) {
20
34
  const { computed } = context;
21
35
  const userReconstructions = computed['user-history-reconstructor'];
22
36
  if (!userReconstructions) return;
23
37
 
24
- const aggregator = {};
38
+ const aggregator = {};
25
39
 
26
40
  // 1. Aggregate Forced Exits
27
41
  for (const userId in userReconstructions) {
28
42
  const userPortfolio = userReconstructions[userId];
29
-
43
+
30
44
  for (const ticker in userPortfolio) {
31
45
  const position = userPortfolio[ticker];
32
-
46
+
33
47
  if (position.closedToday > 0) {
34
48
  if (!aggregator[ticker]) aggregator[ticker] = { closed: 0, forced: 0 };
35
-
49
+
36
50
  aggregator[ticker].closed += position.closedToday;
37
51
  aggregator[ticker].forced += position.forcedExits;
38
52
  }
@@ -0,0 +1,125 @@
1
+ class OwnershipVsPerformanceYTD {
2
+ constructor() { this.results = {}; }
3
+
4
+ static getMetadata() {
5
+ return {
6
+ name: 'ownership-vs-performance-ytd',
7
+ type: 'meta',
8
+ category: 'core',
9
+ userType: 'n/a',
10
+ isHistorical: true, // Required to carry forward Jan 1st baseline
11
+ rootDataDependencies: ['insights', 'price']
12
+ };
13
+ }
14
+
15
+ static getDependencies() { return []; }
16
+
17
+ static getSchema() {
18
+ const metricSchema = {
19
+ "type": "object",
20
+ "properties": {
21
+ "priceYtd": { "type": "number" },
22
+ "ownersYtd": { "type": "number" },
23
+ "currentPrice": { "type": "number" },
24
+ "currentOwners": { "type": "number" }
25
+ },
26
+ "required": ["priceYtd", "ownersYtd", "currentPrice", "currentOwners"]
27
+ };
28
+
29
+ const baselineSchema = {
30
+ "type": "object",
31
+ "patternProperties": {
32
+ "^.*$": {
33
+ "type": "object",
34
+ "properties": {
35
+ "startPrice": { "type": "number" },
36
+ "startOwners": { "type": "number" },
37
+ "date": { "type": "string" }
38
+ }
39
+ }
40
+ }
41
+ };
42
+
43
+ return {
44
+ "type": "object",
45
+ "properties": {
46
+ "baselines": baselineSchema
47
+ },
48
+ "additionalProperties": metricSchema
49
+ };
50
+ }
51
+
52
+ async process(context) {
53
+ const { insights: insightsHelper, priceExtractor } = context.math;
54
+ const { previousComputed, mappings, prices, date } = context;
55
+
56
+ const dailyInsights = insightsHelper.getInsights(context, 'today');
57
+
58
+ // 1. Manage Baseline State (Jan 1st snapshot)
59
+ // If today is Jan 1st (or first run), we reset. Otherwise, we load from yesterday.
60
+ let baselineState = previousComputed['ownership-vs-performance-ytd']?.baselines || {};
61
+ const isStartOfYear = date.today.endsWith('-01-01') || date.today.endsWith('-01-02');
62
+
63
+ if (isStartOfYear) {
64
+ baselineState = {}; // Reset for new year
65
+ }
66
+
67
+ const currentMetrics = {};
68
+
69
+ for (const insight of dailyInsights) {
70
+ const instId = insight.instrumentId;
71
+ const ticker = mappings.instrumentToTicker[instId];
72
+ if (!ticker) continue;
73
+
74
+ const currentOwners = insightsHelper.getTotalOwners(insight);
75
+
76
+ // Get Price History
77
+ const priceHist = priceExtractor.getHistory(prices, ticker);
78
+ if (!priceHist.length) continue;
79
+
80
+ const currentPrice = priceHist[priceHist.length - 1].price;
81
+
82
+ // 2. Set Baseline if missing
83
+ if (!baselineState[ticker]) {
84
+ baselineState[ticker] = {
85
+ startPrice: currentPrice,
86
+ startOwners: currentOwners,
87
+ date: date.today
88
+ };
89
+ }
90
+
91
+ const base = baselineState[ticker];
92
+
93
+ // 3. Calculate Deltas
94
+ const priceYtd = base.startPrice > 0
95
+ ? ((currentPrice - base.startPrice) / base.startPrice) * 100
96
+ : 0;
97
+
98
+ const ownersYtd = base.startOwners > 0
99
+ ? ((currentOwners - base.startOwners) / base.startOwners) * 100
100
+ : 0;
101
+
102
+ currentMetrics[ticker] = {
103
+ priceYtd,
104
+ ownersYtd,
105
+ currentPrice,
106
+ currentOwners
107
+ };
108
+ }
109
+
110
+ // 4. Output structure includes the baselines so they are saved for tomorrow
111
+ this.results = {
112
+ metrics: currentMetrics,
113
+ baselines: baselineState
114
+ };
115
+ }
116
+
117
+ async getResult() {
118
+ // We flatten the 'metrics' for easy API consumption,
119
+ // but we must ensure 'baselines' is preserved in the stored document
120
+ // so 'previousComputed' picks it up tomorrow.
121
+ return { ...this.results.metrics, baselines: this.results.baselines };
122
+ }
123
+ }
124
+
125
+ module.exports = OwnershipVsPerformanceYTD;
@@ -0,0 +1,86 @@
1
+ class OwnershipVsVolatility {
2
+ constructor() { this.results = {}; }
3
+
4
+ static getMetadata() {
5
+ return {
6
+ name: 'ownership-vs-volatility',
7
+ type: 'meta',
8
+ category: 'core',
9
+ userType: 'n/a',
10
+ isHistorical: true, // To calculate ownership change over window
11
+ rootDataDependencies: ['insights', 'price']
12
+ };
13
+ }
14
+
15
+ static getDependencies() { return []; }
16
+
17
+ static getSchema() {
18
+ const schema = {
19
+ "type": "object",
20
+ "properties": {
21
+ "volatilityScore": { "type": "number" },
22
+ "ownerChange7d": { "type": "number" },
23
+ "laggedOwners": { "type": "number" },
24
+ "_history": {
25
+ "type": "array",
26
+ "items": { "type": "number" }
27
+ }
28
+ },
29
+ "required": ["volatilityScore", "ownerChange7d", "laggedOwners"]
30
+ };
31
+ return { "type": "object", "patternProperties": { "^.*$": schema } };
32
+ }
33
+
34
+ async process(context) {
35
+ const { insights: insightsHelper, priceExtractor, compute } = context.math;
36
+ const { previousComputed, mappings, prices } = context;
37
+
38
+ const dailyInsights = insightsHelper.getInsights(context, 'today');
39
+
40
+ // Use 7-day lookback for ownership change
41
+ const prevState = previousComputed['ownership-vs-volatility'] || {};
42
+
43
+ for (const insight of dailyInsights) {
44
+ const instId = insight.instrumentId;
45
+ const ticker = mappings.instrumentToTicker[instId];
46
+ if (!ticker) continue;
47
+
48
+ // 1. Calculate Recent Volatility (14 Day StdDev of Returns)
49
+ const priceHist = priceExtractor.getHistory(prices, ticker);
50
+ if (priceHist.length < 14) continue;
51
+
52
+ const recentPrices = priceHist.slice(-14);
53
+ const returns = [];
54
+ for (let i = 1; i < recentPrices.length; i++) {
55
+ returns.push((recentPrices[i].price - recentPrices[i - 1].price) / recentPrices[i - 1].price);
56
+ }
57
+
58
+ const volatility = compute.standardDeviation(returns); // Raw std dev
59
+
60
+ // 2. Calculate Ownership Change (Current vs 7 days ago stored state)
61
+ const currentOwners = insightsHelper.getTotalOwners(insight);
62
+ const prevOwners = prevState[ticker]?.laggedOwners || currentOwners;
63
+
64
+ const ownerChangePct = prevOwners > 0
65
+ ? ((currentOwners - prevOwners) / prevOwners) * 100
66
+ : 0;
67
+
68
+ this.results[ticker] = {
69
+ volatilityScore: volatility,
70
+ ownerChange7d: ownerChangePct,
71
+ // Store current owners as 'laggedOwners' for next week?
72
+ // Actually, for daily rolling 7d change, we need a queue.
73
+ // Simplified: We store today's value, and the API/UI compares.
74
+ // Better: Use a simple rolling queue like the Momentum calc.
75
+ _history: [...(prevState[ticker]?._history || []), currentOwners].slice(-7),
76
+
77
+ // For the output: Calculate change vs the oldest in history (approx 7d ago)
78
+ laggedOwners: (prevState[ticker]?._history?.[0] || currentOwners)
79
+ };
80
+ }
81
+ }
82
+
83
+ async getResult() { return this.results; }
84
+ }
85
+
86
+ module.exports = OwnershipVsVolatility;
@@ -0,0 +1,69 @@
1
+ class ShortInterestGrowth {
2
+ constructor() { this.results = {}; }
3
+
4
+ static getMetadata() {
5
+ return {
6
+ name: 'short-interest-growth',
7
+ type: 'meta',
8
+ category: 'core',
9
+ userType: 'n/a',
10
+ isHistorical: true,
11
+ rootDataDependencies: ['insights']
12
+ };
13
+ }
14
+
15
+ static getDependencies() { return []; }
16
+
17
+ static getSchema() {
18
+ return {
19
+ "TICKER": {
20
+ "shortCount": 0,
21
+ "shortSparkline": [0, 0, 0, 0, 0, 0, 0],
22
+ "growth7d": 0.0
23
+ }
24
+ };
25
+ }
26
+
27
+ async process(context) {
28
+ const { insights: insightsHelper } = context.math;
29
+ const { previousComputed, mappings } = context;
30
+
31
+ const dailyInsights = insightsHelper.getInsights(context, 'today');
32
+ const previousState = previousComputed['short-interest-growth'] || {};
33
+
34
+ for (const insight of dailyInsights) {
35
+ const instId = insight.instrumentId;
36
+ const ticker = mappings.instrumentToTicker[instId];
37
+ if (!ticker) continue;
38
+
39
+ // Calculate Short Count: Total Owners * (Sell % / 100)
40
+ const shortCount = insightsHelper.getShortCount(insight);
41
+
42
+ const prevState = previousState[ticker] || { shortSparkline: [] };
43
+ const sparkline = [...(prevState.shortSparkline || [])];
44
+
45
+ sparkline.push(shortCount);
46
+ if (sparkline.length > 7) sparkline.shift();
47
+
48
+ // Calculate simple 7-day growth %
49
+ let growth7d = 0;
50
+ if (sparkline.length > 1) {
51
+ const start = sparkline[0];
52
+ const end = sparkline[sparkline.length - 1];
53
+ if (start > 0) {
54
+ growth7d = ((end - start) / start) * 100;
55
+ }
56
+ }
57
+
58
+ this.results[ticker] = {
59
+ shortCount,
60
+ shortSparkline: sparkline,
61
+ growth7d
62
+ };
63
+ }
64
+ }
65
+
66
+ async getResult() { return this.results; }
67
+ }
68
+
69
+ module.exports = ShortInterestGrowth;
@@ -0,0 +1,87 @@
1
+ class TrendingOwnershipMomentum {
2
+ constructor() { this.results = {}; }
3
+
4
+ static getMetadata() {
5
+ return {
6
+ name: 'trending-ownership-momentum',
7
+ type: 'meta',
8
+ category: 'core',
9
+ userType: 'n/a',
10
+ isHistorical: true, // Needed for rolling history
11
+ rootDataDependencies: ['insights']
12
+ };
13
+ }
14
+
15
+ static getDependencies() { return []; }
16
+
17
+ static getSchema() {
18
+ return {
19
+ "TICKER": {
20
+ "currentOwners": 0,
21
+ "sparkline": [0, 0, 0, 0, 0, 0, 0], // 7-Day History
22
+ "momentumScore": 0.0, // Slope of the trend
23
+ "trend": "string" // 'RISING', 'FALLING', 'STABLE'
24
+ }
25
+ };
26
+ }
27
+
28
+ async process(context) {
29
+ const { insights: insightsHelper } = context.math;
30
+ const { previousComputed, mappings } = context;
31
+
32
+ // 1. Get Today's Data
33
+ const dailyInsights = insightsHelper.getInsights(context, 'today');
34
+ if (!dailyInsights.length) return;
35
+
36
+ // 2. Get Yesterday's State (for rolling history)
37
+ const previousState = previousComputed['trending-ownership-momentum'] || {};
38
+
39
+ for (const insight of dailyInsights) {
40
+ const instId = insight.instrumentId;
41
+ const ticker = mappings.instrumentToTicker[instId];
42
+ if (!ticker) continue;
43
+
44
+ const currentCount = insightsHelper.getTotalOwners(insight);
45
+ const prevState = previousState[ticker] || { sparkline: [] };
46
+
47
+ // 3. Update Rolling Window (Last 7 Days)
48
+ // Create new array copy to avoid mutation issues
49
+ const sparkline = [...(prevState.sparkline || [])];
50
+ sparkline.push(currentCount);
51
+
52
+ // Keep only last 7 entries
53
+ if (sparkline.length > 7) sparkline.shift();
54
+
55
+ // 4. Calculate Momentum Score (Linear Regression Slope)
56
+ let momentumScore = 0;
57
+ if (sparkline.length >= 2) {
58
+ const n = sparkline.length;
59
+ let sumX = 0, sumY = 0, sumXY = 0, sumXX = 0;
60
+ for (let i = 0; i < n; i++) {
61
+ sumX += i;
62
+ sumY += sparkline[i];
63
+ sumXY += i * sparkline[i];
64
+ sumXX += i * i;
65
+ }
66
+ const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
67
+ // Normalize slope by current count to get percentage-like momentum
68
+ momentumScore = currentCount > 0 ? (slope / currentCount) * 100 : 0;
69
+ }
70
+
71
+ let trend = 'STABLE';
72
+ if (momentumScore > 0.5) trend = 'RISING';
73
+ if (momentumScore < -0.5) trend = 'FALLING';
74
+
75
+ this.results[ticker] = {
76
+ currentOwners: currentCount,
77
+ sparkline,
78
+ momentumScore,
79
+ trend
80
+ };
81
+ }
82
+ }
83
+
84
+ async getResult() { return this.results; }
85
+ }
86
+
87
+ module.exports = TrendingOwnershipMomentum;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.103",
3
+ "version": "1.0.105",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [