Update app.py
Browse files
app.py
CHANGED
|
@@ -10,13 +10,12 @@ from sklearn.metrics import mean_squared_error
|
|
| 10 |
st.set_page_config(page_title="Analyse Crypto", layout="wide")
|
| 11 |
|
| 12 |
# --- Fonction pour récupérer les données ---
|
| 13 |
-
def fetch_crypto_prices(coin_id,
|
| 14 |
-
"""Récupère les données de prix d'une cryptomonnaie."""
|
| 15 |
end_time = datetime.utcnow()
|
| 16 |
start_time = end_time - timedelta(hours=hours)
|
| 17 |
url = f"https://api.coincap.io/v2/assets/{coin_id}/history"
|
| 18 |
params = {
|
| 19 |
-
"interval":
|
| 20 |
"start": int(start_time.timestamp() * 1000),
|
| 21 |
"end": int(end_time.timestamp() * 1000),
|
| 22 |
}
|
|
@@ -161,52 +160,28 @@ def identify_best_pattern(df, patterns):
|
|
| 161 |
best_pattern = min(mse_values, key=mse_values.get)
|
| 162 |
return best_pattern, patterns[best_pattern]
|
| 163 |
|
| 164 |
-
# ---
|
| 165 |
-
def
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
"Bitcoin": "bitcoin",
|
| 190 |
-
"Ethereum": "ethereum",
|
| 191 |
-
"Ripple": "ripple",
|
| 192 |
-
"Cardano": "cardano",
|
| 193 |
-
}
|
| 194 |
-
coin = st.selectbox("Choisissez une cryptomonnaie :", list(coins.keys()))
|
| 195 |
-
interval = st.selectbox("Intervalle :", ["m15", "h1", "h6", "h12", "h24"], index=0)
|
| 196 |
-
|
| 197 |
-
if st.button("Analyser"):
|
| 198 |
-
st.write("Chargement des données...")
|
| 199 |
-
data = fetch_crypto_prices(coins[coin], interval=interval)
|
| 200 |
-
if data:
|
| 201 |
-
df = pd.DataFrame(data)
|
| 202 |
-
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
| 203 |
-
df.set_index("timestamp", inplace=True)
|
| 204 |
-
|
| 205 |
-
# Prévoir les tendances
|
| 206 |
-
patterns = predict_patterns(data)
|
| 207 |
-
best_pattern_name, best_pattern_df = identify_best_pattern(df, patterns)
|
| 208 |
-
plot_graph(df, best_pattern_name, best_pattern_df)
|
| 209 |
-
|
| 210 |
-
st.write("Analyse terminée.")
|
| 211 |
-
else:
|
| 212 |
-
st.error("Impossible de récupérer les données.")
|
|
|
|
| 10 |
st.set_page_config(page_title="Analyse Crypto", layout="wide")
|
| 11 |
|
| 12 |
# --- Fonction pour récupérer les données ---
|
| 13 |
+
def fetch_crypto_prices(coin_id, currency_id, hours=48):
|
|
|
|
| 14 |
end_time = datetime.utcnow()
|
| 15 |
start_time = end_time - timedelta(hours=hours)
|
| 16 |
url = f"https://api.coincap.io/v2/assets/{coin_id}/history"
|
| 17 |
params = {
|
| 18 |
+
"interval": "m15", # Interval fixe à 15 minutes
|
| 19 |
"start": int(start_time.timestamp() * 1000),
|
| 20 |
"end": int(end_time.timestamp() * 1000),
|
| 21 |
}
|
|
|
|
| 160 |
best_pattern = min(mse_values, key=mse_values.get)
|
| 161 |
return best_pattern, patterns[best_pattern]
|
| 162 |
|
| 163 |
+
# --- Affichage des résultats ---
|
| 164 |
+
def plot_best_pattern(df, best_pattern_name, best_pattern_df):
|
| 165 |
+
plt.figure(figsize=(10, 6))
|
| 166 |
+
plt.plot(df.index, df["price"], label="Prix réel", color="blue")
|
| 167 |
+
plt.plot(best_pattern_df["timestamp"], best_pattern_df["price"], label=best_pattern_name, color="orange", linestyle="--")
|
| 168 |
+
plt.xlabel("Timestamp")
|
| 169 |
+
plt.ylabel("Prix")
|
| 170 |
+
plt.title(f"Comparaison des prix réels avec le pattern {best_pattern_name}")
|
| 171 |
+
plt.legend()
|
| 172 |
+
st.pyplot()
|
| 173 |
+
|
| 174 |
+
# --- Application Streamlit ---
|
| 175 |
+
st.title("Analyse des Cryptomonnaies")
|
| 176 |
+
|
| 177 |
+
# Sélection de la cryptomonnaie
|
| 178 |
+
coin_id = st.selectbox("Choisir une cryptomonnaie", ["bitcoin", "ethereum"])
|
| 179 |
+
currency_id = "usd" # Monnaie par défaut en USD
|
| 180 |
+
|
| 181 |
+
# Récupérer les données
|
| 182 |
+
data = fetch_crypto_prices(coin_id, currency_id)
|
| 183 |
+
if data:
|
| 184 |
+
df = pd.DataFrame(data)
|
| 185 |
+
patterns = predict_patterns(data) # Prédire les patterns
|
| 186 |
+
best_pattern_name, best_pattern_df = identify_best_pattern(df, patterns) # Identifier le meilleur pattern
|
| 187 |
+
plot_best_pattern(df, best_pattern_name, best_pattern_df) # Afficher les résultats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|