Enoder commited on
Commit
a0dfeca
·
verified ·
1 Parent(s): 7ec47c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -52
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, interval="m15", hours=48):
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": 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
- # --- Fonction d'affichage ---
165
- def plot_graph(df, best_pattern_name, best_pattern_df):
166
- """Affiche les prix réels et la prédiction avec le meilleur pattern."""
167
- fig, ax = plt.subplots(figsize=(12, 6))
168
- ax.plot(df.index, df["price"], label="Prix réel", color="blue")
169
- ax.plot(
170
- best_pattern_df.index,
171
- best_pattern_df["price"],
172
- label=f"Prédiction ({best_pattern_name})",
173
- color="orange",
174
- )
175
- ax.legend()
176
- ax.set_title("Évolution des prix et prédictions")
177
- ax.set_xlabel("Temps")
178
- ax.set_ylabel("Prix")
179
- st.pyplot(fig)
180
- st.write(f"### Pourquoi cette prédiction ?")
181
- st.write(
182
- f"Le pattern sélectionné est **{best_pattern_name}**, car il correspond "
183
- f"au comportement observé des prix sur la période passée."
184
- )
185
-
186
- # --- Interface utilisateur ---
187
- st.title("Prédiction des cours des cryptomonnaies")
188
- coins = {
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