When interval1 > interval2, I change the color of interval1 to green!
I just want a message that tells me the number of interval1 > interval2. I don't know how to do this?
async function fetchCryptoData(symbol) {
try {
const response = await fetch(
`https://api.binance.com/api/v3/klines?symbol=${symbol}USDT&interval=5m&limit=2`
);
const data = await response.json();
// Mise à jour du tableau avec les données et la couleur
const cryptoRow = document.getElementById(symbol);
// Comparaison des intervalles
const interval1 = parseFloat(data[0][4]) - parseFloat(data[0][1]);
const interval2 = parseFloat(data[1][4]) - parseFloat(data[1][1]);
// Vérification si interval1 est strictement plus grand que les autres intervalles
const isMaxInterval = interval1 > interval2;
// Initializsation de count pour interval1 > interval2
let countIntervalGreaterThan = 0;
for (let i = 0; i < data.length; i++) {
const openPrice = parseFloat(data[i][1]);
const closePrice = parseFloat(data[i][4]);
const intervalVariation = ((closePrice - openPrice) / openPrice) * 100;
const cellIndex = i + 1; // Décalage d'une cellule pour éviter la première cellule (Crypto)
const variationCell = cryptoRow.insertCell(cellIndex);
const variationValue = intervalVariation.toFixed(2);
const timestamp = parseInt(data[i][0]);
const dateValue = new Date(timestamp);
const hour = dateValue.getHours();
const minute = dateValue.getMinutes();
const formattedTime = `${hour.toString().padStart(2, "0")}:${minute
.toString()
.padStart(2, "0")}`;
variationCell.textContent = `${formattedTime}: ${variationValue}%`;
// Ajout de la classe 'positive' uniquement si interval1 est strictement plus grand que les autres intervalles
if (i === 0 && isMaxInterval && interval1 !== 0) {
variationCell.classList.add('positive');
// Increment le compte si interval1 > interval2
countIntervalGreaterThan++;
}
}
// Affichage du count après la boucle
console.log(`Number of times interval1 > interval2: ${countIntervalGreaterThan}`);
} catch (error) {
console.error(
`Erreur lors de la récupération des données pour ${symbol}:`,
error
);
}
}
fetchCryptoData("1INCH");
fetchCryptoData("AAVE");
fetchCryptoData("ACH");
fetchCryptoData("ADA");
fetchCryptoData("AGIX");
fetchCryptoData("AGLD");
....
Why do I have several lines instead of one line that adds the total?
Simply because
fetchCryptoData
is called multiple times and youconsole.log
inside offetchCryptoData
.That also means your
countIntervalGreaterThan
variable is actually the value for the results of a single call, not all of them.Better to have
fetchCryptoData
return the count for each call (subtotal) and then have something that waits for all of them and adds them up (grand total).