aiden-shared-calculations-unified 1.0.91 → 1.0.92
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Calculation (Pass 1 - Meta) for 1-day price change.
|
|
3
|
-
*
|
|
3
|
+
* FIXED: Uses priceExtractor properly
|
|
4
4
|
*/
|
|
5
5
|
class InstrumentPriceChange1D {
|
|
6
6
|
constructor() {
|
|
@@ -29,41 +29,37 @@ class InstrumentPriceChange1D {
|
|
|
29
29
|
};
|
|
30
30
|
return { "type": "object", "patternProperties": { "^.*$": tickerSchema } };
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
_getPrices(priceData) {
|
|
34
|
-
if (!priceData || !priceData.prices) return [];
|
|
35
|
-
return Object.entries(priceData.prices)
|
|
36
|
-
.map(([date, price]) => ({ date, price }))
|
|
37
|
-
.sort((a, b) => new Date(a.date) - new Date(b.date));
|
|
38
|
-
}
|
|
39
32
|
|
|
40
33
|
process(context) {
|
|
41
|
-
const { mappings, prices } = context;
|
|
34
|
+
const { mappings, prices, date, math } = context;
|
|
42
35
|
const { instrumentToTicker } = mappings;
|
|
36
|
+
const { priceExtractor } = math;
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const results = {};
|
|
38
|
+
if (!prices || !prices.history) {
|
|
39
|
+
this.result = {};
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
49
42
|
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
const todayStr = date.today;
|
|
44
|
+
const todayDate = new Date(todayStr + 'T00:00:00Z');
|
|
45
|
+
const yesterdayDate = new Date(todayDate);
|
|
46
|
+
yesterdayDate.setUTCDate(yesterdayDate.getUTCDate() - 1);
|
|
47
|
+
const yesterdayStr = yesterdayDate.toISOString().slice(0, 10);
|
|
55
48
|
|
|
56
|
-
const
|
|
57
|
-
const pList = this._getPrices(p);
|
|
58
|
-
const lastPrice = pList.length > 0 ? pList[pList.length - 1].price : 0;
|
|
59
|
-
return [p.instrumentId, lastPrice];
|
|
60
|
-
}));
|
|
49
|
+
const results = {};
|
|
61
50
|
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
// Iterate over all instruments in price history
|
|
52
|
+
for (const [instrumentId, priceData] of Object.entries(prices.history)) {
|
|
64
53
|
const ticker = instrumentToTicker[instrumentId];
|
|
65
54
|
if (!ticker) continue;
|
|
66
55
|
|
|
56
|
+
const history = priceExtractor.getHistory(prices, instrumentId);
|
|
57
|
+
if (history.length === 0) continue;
|
|
58
|
+
|
|
59
|
+
// Find today's and yesterday's prices
|
|
60
|
+
const todayPrice = priceData.prices?.[todayStr];
|
|
61
|
+
const yesterdayPrice = priceData.prices?.[yesterdayStr];
|
|
62
|
+
|
|
67
63
|
if (todayPrice && yesterdayPrice && yesterdayPrice > 0) {
|
|
68
64
|
const changePct = ((todayPrice - yesterdayPrice) / yesterdayPrice) * 100;
|
|
69
65
|
results[ticker] = {
|
|
@@ -73,6 +69,7 @@ class InstrumentPriceChange1D {
|
|
|
73
69
|
results[ticker] = { change_1d_pct: 0 };
|
|
74
70
|
}
|
|
75
71
|
}
|
|
72
|
+
|
|
76
73
|
this.result = results;
|
|
77
74
|
}
|
|
78
75
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Calculation (Pass 1 - Meta) for 20-day momentum.
|
|
3
|
-
*
|
|
3
|
+
* FIXED: Uses priceExtractor properly
|
|
4
4
|
*/
|
|
5
5
|
class InstrumentPriceMomentum20D {
|
|
6
6
|
constructor() {
|
|
@@ -30,13 +30,14 @@ class InstrumentPriceMomentum20D {
|
|
|
30
30
|
return { "type": "object", "patternProperties": { "^.*$": tickerSchema } };
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
_findPriceOnOrBefore(
|
|
34
|
-
|
|
33
|
+
_findPriceOnOrBefore(prices, instrumentId, targetDateStr) {
|
|
34
|
+
const priceData = prices.history[instrumentId];
|
|
35
|
+
if (!priceData || !priceData.prices) return null;
|
|
35
36
|
|
|
36
37
|
let checkDate = new Date(targetDateStr + 'T00:00:00Z');
|
|
37
38
|
for (let i = 0; i < 5; i++) {
|
|
38
39
|
const str = checkDate.toISOString().slice(0, 10);
|
|
39
|
-
const price =
|
|
40
|
+
const price = priceData.prices[str];
|
|
40
41
|
if (price !== undefined && price !== null && price > 0) return price;
|
|
41
42
|
checkDate.setUTCDate(checkDate.getUTCDate() - 1);
|
|
42
43
|
}
|
|
@@ -47,8 +48,10 @@ class InstrumentPriceMomentum20D {
|
|
|
47
48
|
const { mappings, prices, date } = context;
|
|
48
49
|
const { instrumentToTicker } = mappings;
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
if (!prices || !prices.history) {
|
|
52
|
+
this.result = {};
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
52
55
|
|
|
53
56
|
const todayStr = date.today;
|
|
54
57
|
const todayDate = new Date(todayStr + 'T00:00:00Z');
|
|
@@ -58,13 +61,13 @@ class InstrumentPriceMomentum20D {
|
|
|
58
61
|
|
|
59
62
|
const results = {};
|
|
60
63
|
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
// Iterate over all instruments in price history
|
|
65
|
+
for (const [instrumentId, priceData] of Object.entries(prices.history)) {
|
|
63
66
|
const ticker = instrumentToTicker[instrumentId];
|
|
64
67
|
if (!ticker) continue;
|
|
65
68
|
|
|
66
|
-
const currentPrice = this._findPriceOnOrBefore(
|
|
67
|
-
const oldPrice = this._findPriceOnOrBefore(
|
|
69
|
+
const currentPrice = this._findPriceOnOrBefore(prices, instrumentId, todayStr);
|
|
70
|
+
const oldPrice = this._findPriceOnOrBefore(prices, instrumentId, oldStr);
|
|
68
71
|
|
|
69
72
|
if (currentPrice && oldPrice && oldPrice > 0) {
|
|
70
73
|
const momPct = ((currentPrice - oldPrice) / oldPrice) * 100;
|
|
@@ -75,6 +78,7 @@ class InstrumentPriceMomentum20D {
|
|
|
75
78
|
results[ticker] = { momentum_20d_pct: 0 };
|
|
76
79
|
}
|
|
77
80
|
}
|
|
81
|
+
|
|
78
82
|
this.result = results;
|
|
79
83
|
}
|
|
80
84
|
|
package/package.json
CHANGED
package/utils/firestore_utils.js
CHANGED
|
@@ -41,7 +41,6 @@ async function withRetry(asyncOperation, operationName = 'Firestore operation')
|
|
|
41
41
|
const code = error.code || error.details;
|
|
42
42
|
|
|
43
43
|
if (RETRYABLE_ERROR_CODES.has(code)) {
|
|
44
|
-
// Assuming logger is available globally or passed in somehow in the package context
|
|
45
44
|
if (typeof logger !== 'undefined' && logger.log) {
|
|
46
45
|
logger.log('WARN', `[Retry ${attempt}/${MAX_RETRIES}] Retrying ${operationName} due to ${code}. Waiting ${backoff}ms...`);
|
|
47
46
|
} else {
|