Spaces:
Build error
Build error
Update app.py
#1
by Enoder - opened
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from datetime import datetime, timedelta
|
|
| 7 |
from sklearn.metrics import mean_squared_error
|
| 8 |
|
| 9 |
# Fonction pour récupérer les prix historiques
|
| 10 |
-
def get_crypto_prices(coins, start_time, end_time, interval='
|
| 11 |
prices = {}
|
| 12 |
for coin, coin_id in coins.items():
|
| 13 |
url = f'https://api.coincap.io/v2/assets/{coin_id}/history'
|
|
@@ -34,7 +34,7 @@ def predict_patterns(data, future_hours=24):
|
|
| 34 |
df.set_index("timestamp", inplace=True)
|
| 35 |
|
| 36 |
last_timestamp = df.index[-1]
|
| 37 |
-
future_timestamps = [last_timestamp + timedelta(
|
| 38 |
|
| 39 |
last_price = df["price"].iloc[-1]
|
| 40 |
trend = (df["price"].iloc[-1] - df["price"].iloc[0]) / len(df)
|
|
@@ -102,47 +102,48 @@ def identify_best_pattern(df, patterns):
|
|
| 102 |
best_pattern = min(mse_scores, key=mse_scores.get)
|
| 103 |
return best_pattern
|
| 104 |
|
| 105 |
-
# Fonction pour ajouter la prédiction dynamique basée sur les patterns historiques
|
| 106 |
-
def predict_dynamic_curve(df, patterns, future_hours=24):
|
| 107 |
-
dynamic_predictions = []
|
| 108 |
-
current_values = df["price"].values[-5:] # Les 5 dernières valeurs pour démarrer
|
| 109 |
-
|
| 110 |
-
for i in range(future_hours // 5):
|
| 111 |
-
# Identifier le pattern le plus proche des 5 dernières valeurs
|
| 112 |
-
best_pattern = identify_best_pattern(pd.DataFrame({"timestamp": df.index[-5:], "price": current_values}), patterns)
|
| 113 |
-
best_pattern_values = patterns[best_pattern]["price"].values[:5]
|
| 114 |
-
dynamic_predictions.extend(best_pattern_values)
|
| 115 |
-
current_values = dynamic_predictions[-5:] # Mise à jour avec les 5 dernières valeurs
|
| 116 |
-
|
| 117 |
-
future_timestamps = [df.index[-1] + timedelta(minutes=15 * i) for i in range(1, len(dynamic_predictions) + 1)]
|
| 118 |
-
dynamic_curve = pd.DataFrame({"timestamp": future_timestamps, "price": dynamic_predictions})
|
| 119 |
-
return dynamic_curve
|
| 120 |
-
|
| 121 |
# Configuration des dates
|
| 122 |
end_time = datetime.now()
|
| 123 |
-
start_time = end_time - timedelta(
|
| 124 |
|
| 125 |
-
#
|
| 126 |
coins = {
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
for coin,
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from sklearn.metrics import mean_squared_error
|
| 8 |
|
| 9 |
# Fonction pour récupérer les prix historiques
|
| 10 |
+
def get_crypto_prices(coins, start_time, end_time, interval='h1'):
|
| 11 |
prices = {}
|
| 12 |
for coin, coin_id in coins.items():
|
| 13 |
url = f'https://api.coincap.io/v2/assets/{coin_id}/history'
|
|
|
|
| 34 |
df.set_index("timestamp", inplace=True)
|
| 35 |
|
| 36 |
last_timestamp = df.index[-1]
|
| 37 |
+
future_timestamps = [last_timestamp + timedelta(hours=i) for i in range(1, future_hours + 1)]
|
| 38 |
|
| 39 |
last_price = df["price"].iloc[-1]
|
| 40 |
trend = (df["price"].iloc[-1] - df["price"].iloc[0]) / len(df)
|
|
|
|
| 102 |
best_pattern = min(mse_scores, key=mse_scores.get)
|
| 103 |
return best_pattern
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Configuration des dates
|
| 106 |
end_time = datetime.now()
|
| 107 |
+
start_time = end_time - timedelta(hours=48)
|
| 108 |
|
| 109 |
+
# Liste des cryptos avec leurs IDs corrects sur CoinCap
|
| 110 |
coins = {
|
| 111 |
+
"Bitcoin": "bitcoin",
|
| 112 |
+
"Ripple": "xrp",
|
| 113 |
+
"Ethereum": "ethereum",
|
| 114 |
+
"Tether": "tether",
|
| 115 |
+
"Stellar": "stellar"
|
| 116 |
}
|
| 117 |
|
| 118 |
+
# Récupération des prix
|
| 119 |
+
crypto_prices = get_crypto_prices(coins, start_time, end_time)
|
| 120 |
+
|
| 121 |
+
# Affichage des graphiques
|
| 122 |
+
st.title("Analyse et Prédictions des Cryptos avec Patterns")
|
| 123 |
+
for coin, price_data in crypto_prices.items():
|
| 124 |
+
if price_data:
|
| 125 |
+
df = pd.DataFrame(price_data, columns=["timestamp", "price"])
|
| 126 |
+
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
| 127 |
+
|
| 128 |
+
patterns = predict_patterns(price_data, future_hours=24)
|
| 129 |
+
best_pattern = identify_best_pattern(df, patterns)
|
| 130 |
+
|
| 131 |
+
st.write(f"#### {coin} - Prix et Prédictions")
|
| 132 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 133 |
+
ax.plot(df["timestamp"], df["price"], label="Prix réel", color="blue")
|
| 134 |
+
|
| 135 |
+
# Affiche uniquement le meilleur pattern
|
| 136 |
+
best_pattern_df = patterns[best_pattern]
|
| 137 |
+
ax.plot(
|
| 138 |
+
best_pattern_df["timestamp"],
|
| 139 |
+
best_pattern_df["price"],
|
| 140 |
+
label=f"Prédiction ({best_pattern})",
|
| 141 |
+
linestyle="dashed",
|
| 142 |
+
color="orange"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
ax.set_title(f"{coin} - Prix (48h réels + 24h prévu avec {best_pattern})")
|
| 146 |
+
ax.set_xlabel("Date")
|
| 147 |
+
ax.set_ylabel("Prix (USD)")
|
| 148 |
+
ax.legend()
|
| 149 |
+
st.pyplot(fig)
|