aiden-shared-calculations-unified 1.0.29 → 1.0.31
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/package.json +1 -1
- package/utils/price_data_provider.js +43 -4
package/package.json
CHANGED
|
@@ -14,6 +14,34 @@ let cache = {
|
|
|
14
14
|
// In-progress fetch promise
|
|
15
15
|
let fetchPromise = null;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Finds the most recent available price on or before a given date.
|
|
19
|
+
* @param {object} priceHistory - The map of { "YYYY-MM-DD": price }
|
|
20
|
+
* @param {string} dateStr - The target date string to start searching from.
|
|
21
|
+
* @param {number} [maxLookback=5] - Max days to look back (to skip weekends/holidays).
|
|
22
|
+
* @returns {number|null} The price, or null if not found.
|
|
23
|
+
*/
|
|
24
|
+
function _findPreviousAvailablePrice(priceHistory, dateStr, maxLookback = 5) {
|
|
25
|
+
if (!priceHistory) return null;
|
|
26
|
+
|
|
27
|
+
let checkDate = new Date(dateStr + 'T00:00:00Z');
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < maxLookback; i++) {
|
|
30
|
+
const checkDateStr = checkDate.toISOString().slice(0, 10);
|
|
31
|
+
const price = priceHistory[checkDateStr];
|
|
32
|
+
|
|
33
|
+
if (price !== undefined && price !== null && price > 0) {
|
|
34
|
+
return price; // Found it
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// If not found, look back one more day
|
|
38
|
+
checkDate.setUTCDate(checkDate.getUTCDate() - 1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// console.warn(`No price found for instrument within ${maxLookback} days of ${dateStr}`);
|
|
42
|
+
return null; // No price found within lookback
|
|
43
|
+
}
|
|
44
|
+
|
|
17
45
|
/**
|
|
18
46
|
* Loads all sharded price data from the `asset_prices` collection.
|
|
19
47
|
* This is a heavy operation and should be cached.
|
|
@@ -75,6 +103,7 @@ async function loadAllPriceData() {
|
|
|
75
103
|
|
|
76
104
|
/**
|
|
77
105
|
* A helper to safely get the price change percentage between two dates.
|
|
106
|
+
* --- THIS IS THE MODIFIED FUNCTION ---
|
|
78
107
|
* @param {string} instrumentId - The instrument ID.
|
|
79
108
|
* @param {string} yesterdayStr - YYYY-MM-DD date string for yesterday.
|
|
80
109
|
* @param {string} todayStr - YYYY-MM-DD date string for today.
|
|
@@ -86,18 +115,28 @@ function getDailyPriceChange(instrumentId, yesterdayStr, todayStr, priceMap) {
|
|
|
86
115
|
return null; // No price data for this instrument
|
|
87
116
|
}
|
|
88
117
|
|
|
89
|
-
const
|
|
90
|
-
|
|
118
|
+
const instrumentPrices = priceMap[instrumentId];
|
|
119
|
+
|
|
120
|
+
// Find the most recent available price *on or before* the target dates
|
|
121
|
+
const priceDay1 = _findPreviousAvailablePrice(instrumentPrices, yesterdayStr, 5);
|
|
122
|
+
const priceDay2 = _findPreviousAvailablePrice(instrumentPrices, todayStr, 5);
|
|
91
123
|
|
|
92
|
-
if (priceDay1 && priceDay2
|
|
124
|
+
if (priceDay1 && priceDay2) {
|
|
125
|
+
// We found prices, now check if they are the *same* price
|
|
126
|
+
// (e.g., if today is Sunday, priceDay2 would be Friday's price.
|
|
127
|
+
// If yesterday was Saturday, priceDay1 would *also* be Friday's price).
|
|
128
|
+
if (priceDay1 === priceDay2) {
|
|
129
|
+
return 0.0; // No change between the two most recent *available* dates
|
|
130
|
+
}
|
|
93
131
|
return (priceDay2 - priceDay1) / priceDay1;
|
|
94
132
|
}
|
|
95
133
|
|
|
96
|
-
return null; // Missing one or both dates,
|
|
134
|
+
return null; // Missing one or both dates, even after lookback
|
|
97
135
|
}
|
|
98
136
|
|
|
99
137
|
|
|
100
138
|
module.exports = {
|
|
101
139
|
loadAllPriceData,
|
|
102
140
|
getDailyPriceChange
|
|
141
|
+
// Add the helper to exports if you want, but it's not required
|
|
103
142
|
};
|