movido a coingecko

This commit is contained in:
2025-06-17 19:39:01 +02:00
parent e56761aac5
commit 2b13417a66
4 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,69 @@
#!/bin/bash
# === CONFIGURACIÓN ===
COIN_ID="tether"
VS_CURRENCY="eur"
OUTPUT="usdt-eur-2025-average-gmt1.csv"
INICIO=2024-12-01
FIN=2026-12-01
# === Timestamps UNIX UTC ===
if date -j -f "%Y-%m-%d" "2025-01-01" +"%s" >/dev/null 2>&1; then
# -- Version MacOS --
FROM_TIMESTAMP=$(date -j -f "%Y-%m-%d" "$INICIO" +"%s")
TO_TIMESTAMP=$(date -j -f "%Y-%m-%d" "$FIN" +"%s")
else
# -- Versión Linux --
FROM_TIMESTAMP=$(date -d "$INICIO" +"%s")
TO_TIMESTAMP=$(date -d "$FIN" +"%s")
fi
# === Llamada API ===
URL="https://api.coingecko.com/api/v3/coins/${COIN_ID}/market_chart/range?vs_currency=${VS_CURRENCY}&from=${FROM_TIMESTAMP}&to=${TO_TIMESTAMP}"
echo "📡 Llamando a: $URL"
RESPONSE=$(curl -s "$URL")
# Verifica si viene campo .prices
if ! echo "$RESPONSE" | jq '.prices' | grep -q '\['; then
echo "❌ No se encontraron precios en la respuesta de CoinGecko."
exit 1
fi
# === Cabecera CSV ===
echo "fecha_gmt1,avg_usdt_eur" > "$OUTPUT"
# === Procesamiento + agrupación por fecha española ===
export LC_NUMERIC=C
echo "$RESPONSE" | jq -c '.prices[]' \
| while read -r entry; do
ts_ms=$(echo "$entry" | jq '.[0]')
price=$(echo "$entry" | jq '.[1]')
# Verifica que price sea válido
if [[ "$price" == "null" || -z "$price" ]]; then continue; fi
# Convertir a hora española (Europe/Madrid)
ts_s=$((ts_ms/1000))
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
date_es=$(TZ=Europe/Madrid date -j -f "%s" "$ts_s" "+%Y-%m-%d")
else
# Linux
date_es=$(TZ=Europe/Madrid date -d "@$ts_s" "+%Y-%m-%d")
fi
echo "$date_es,$price"
done \
| awk -F, '
{
sum[$1] += $2;
count[$1]++;
}
END {
for (d in sum)
printf "%s,%.6f\n", d, sum[d]/count[d];
}' | sort >> "$OUTPUT"
echo "✅ Exportado como $OUTPUT (en horario de España)"