replace the mock RSI calculation with a real one
Browse files
script.js
CHANGED
|
@@ -22,17 +22,43 @@ async function fetchCryptoData() {
|
|
| 22 |
return [];
|
| 23 |
}
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
//
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
// Create crypto card
|
| 33 |
function createCryptoCard(crypto) {
|
| 34 |
-
const rsi = calculateRSI(crypto.
|
| 35 |
-
|
| 36 |
|
| 37 |
let rsiClass = 'rsi-medium';
|
| 38 |
if (rsi < 30) rsiClass = 'rsi-low';
|
|
|
|
| 22 |
return [];
|
| 23 |
}
|
| 24 |
}
|
| 25 |
+
// Calculate RSI (Relative Strength Index)
|
| 26 |
+
function calculateRSI(priceHistory, period = 14) {
|
| 27 |
+
if (!priceHistory || priceHistory.length < period + 1) return 50; // Default neutral value
|
| 28 |
+
|
| 29 |
+
// Calculate price changes
|
| 30 |
+
const changes = [];
|
| 31 |
+
for (let i = 1; i < priceHistory.length; i++) {
|
| 32 |
+
changes.push(priceHistory[i] - priceHistory[i-1]);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Separate gains and losses
|
| 36 |
+
const gains = changes.map(change => change > 0 ? change : 0);
|
| 37 |
+
const losses = changes.map(change => change < 0 ? Math.abs(change) : 0);
|
| 38 |
+
|
| 39 |
+
// Calculate average gains and losses for the period
|
| 40 |
+
let avgGain = gains.slice(0, period).reduce((sum, gain) => sum + gain, 0) / period;
|
| 41 |
+
let avgLoss = losses.slice(0, period).reduce((sum, loss) => sum + loss, 0) / period;
|
| 42 |
+
|
| 43 |
+
// Calculate subsequent average gains/losses using smoothing
|
| 44 |
+
for (let i = period; i < changes.length; i++) {
|
| 45 |
+
avgGain = (avgGain * (period - 1) + gains[i]) / period;
|
| 46 |
+
avgLoss = (avgLoss * (period - 1) + losses[i]) / period;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Avoid division by zero
|
| 50 |
+
if (avgLoss === 0) return 100;
|
| 51 |
+
|
| 52 |
+
// Calculate relative strength and RSI
|
| 53 |
+
const relativeStrength = avgGain / avgLoss;
|
| 54 |
+
const rsi = 100 - (100 / (1 + relativeStrength));
|
| 55 |
+
|
| 56 |
+
return Math.round(rsi * 100) / 100; // Round to 2 decimal places
|
| 57 |
}
|
| 58 |
// Create crypto card
|
| 59 |
function createCryptoCard(crypto) {
|
| 60 |
+
const rsi = calculateRSI(crypto.sparkline_in_7d.price);
|
| 61 |
+
const isPumping = crypto.price_change_percentage_24h > 0;
|
| 62 |
|
| 63 |
let rsiClass = 'rsi-medium';
|
| 64 |
if (rsi < 30) rsiClass = 'rsi-low';
|