Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import numpy as np
|
|
| 6 |
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='m15'):
|
| 11 |
prices = {}
|
|
@@ -24,9 +25,10 @@ def get_crypto_prices(coins, start_time, end_time, interval='m15'):
|
|
| 24 |
prices[coin].append([int(end_time.timestamp() * 1000), float(data[-1]['priceUsd'])])
|
| 25 |
else:
|
| 26 |
st.error(f"Erreur lors de la récupération des prix pour {coin}: {response.status_code}")
|
| 27 |
-
st.write(response.text) #
|
| 28 |
return prices
|
| 29 |
|
|
|
|
| 30 |
# Fonction pour prédire les tendances basées sur les patterns
|
| 31 |
def predict_patterns(data, future_hours=24):
|
| 32 |
df = pd.DataFrame(data, columns=["timestamp", "price"])
|
|
@@ -40,21 +42,25 @@ def predict_patterns(data, future_hours=24):
|
|
| 40 |
trend = (df["price"].iloc[-1] - df["price"].iloc[0]) / len(df)
|
| 41 |
sma = df["price"].rolling(window=10).mean().iloc[-1]
|
| 42 |
ema = df["price"].ewm(span=10, adjust=False).mean().iloc[-1]
|
|
|
|
|
|
|
| 43 |
|
| 44 |
patterns = {}
|
| 45 |
|
| 46 |
patterns["Trend"] = [last_price + i * trend for i in range(1, len(future_timestamps) + 1)]
|
|
|
|
| 47 |
patterns["SMA"] = [sma] * len(future_timestamps)
|
| 48 |
patterns["EMA"] = [ema] * len(future_timestamps)
|
| 49 |
-
cycle_amplitude = (df["price"].max() - df["price"].min()) / 2
|
| 50 |
patterns["Cycle"] = [
|
| 51 |
last_price + cycle_amplitude * np.sin(2 * np.pi * i / len(future_timestamps))
|
| 52 |
for i in range(len(future_timestamps))
|
| 53 |
]
|
| 54 |
patterns["Rebound"] = [last_price + trend * (0.5 ** i) for i in range(len(future_timestamps))]
|
| 55 |
patterns["Plateau"] = [last_price] * len(future_timestamps)
|
| 56 |
-
patterns["
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
patterns["Recovery"] = [last_price + (mean_price - last_price) * (i / len(future_timestamps)) for i in range(len(future_timestamps))]
|
| 59 |
patterns["Acceleration"] = [last_price + (trend * 1.5) * i for i in range(len(future_timestamps))]
|
| 60 |
|
|
@@ -64,6 +70,7 @@ def predict_patterns(data, future_hours=24):
|
|
| 64 |
}
|
| 65 |
return pattern_dfs
|
| 66 |
|
|
|
|
| 67 |
# Fonction pour identifier le pattern le plus ressemblant
|
| 68 |
def identify_best_pattern(df, patterns):
|
| 69 |
mse_scores = {}
|
|
@@ -71,12 +78,15 @@ def identify_best_pattern(df, patterns):
|
|
| 71 |
|
| 72 |
for pattern_name, pattern_df in patterns.items():
|
| 73 |
predicted_values = np.array(pattern_df["price"].values[:len(actual_values)])
|
| 74 |
-
|
|
|
|
| 75 |
mse_scores[pattern_name] = mse
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
return best_pattern
|
| 79 |
|
|
|
|
| 80 |
# Configuration des dates
|
| 81 |
end_time = datetime.now()
|
| 82 |
start_time = end_time - timedelta(hours=48)
|
|
@@ -84,7 +94,7 @@ start_time = end_time - timedelta(hours=48)
|
|
| 84 |
# Liste des cryptos avec leurs IDs corrects sur CoinCap
|
| 85 |
coins = {
|
| 86 |
"Bitcoin": "bitcoin",
|
| 87 |
-
"Ripple": "xrp",
|
| 88 |
"Ethereum": "ethereum",
|
| 89 |
"Tether": "tether",
|
| 90 |
"Stellar": "stellar"
|
|
@@ -94,7 +104,7 @@ coins = {
|
|
| 94 |
crypto_prices = get_crypto_prices(coins, start_time, end_time)
|
| 95 |
|
| 96 |
# Affichage des graphiques
|
| 97 |
-
st.title("Analyse et Prédictions des Cryptos")
|
| 98 |
for coin, price_data in crypto_prices.items():
|
| 99 |
if price_data:
|
| 100 |
df = pd.DataFrame(price_data, columns=["timestamp", "price"])
|
|
|
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from sklearn.metrics import mean_squared_error
|
| 8 |
|
| 9 |
+
|
| 10 |
# Fonction pour récupérer les prix historiques
|
| 11 |
def get_crypto_prices(coins, start_time, end_time, interval='m15'):
|
| 12 |
prices = {}
|
|
|
|
| 25 |
prices[coin].append([int(end_time.timestamp() * 1000), float(data[-1]['priceUsd'])])
|
| 26 |
else:
|
| 27 |
st.error(f"Erreur lors de la récupération des prix pour {coin}: {response.status_code}")
|
| 28 |
+
st.write(response.text) # Debug pour afficher l'erreur complète
|
| 29 |
return prices
|
| 30 |
|
| 31 |
+
|
| 32 |
# Fonction pour prédire les tendances basées sur les patterns
|
| 33 |
def predict_patterns(data, future_hours=24):
|
| 34 |
df = pd.DataFrame(data, columns=["timestamp", "price"])
|
|
|
|
| 42 |
trend = (df["price"].iloc[-1] - df["price"].iloc[0]) / len(df)
|
| 43 |
sma = df["price"].rolling(window=10).mean().iloc[-1]
|
| 44 |
ema = df["price"].ewm(span=10, adjust=False).mean().iloc[-1]
|
| 45 |
+
cycle_amplitude = (df["price"].max() - df["price"].min()) / 2
|
| 46 |
+
mean_price = df["price"].mean()
|
| 47 |
|
| 48 |
patterns = {}
|
| 49 |
|
| 50 |
patterns["Trend"] = [last_price + i * trend for i in range(1, len(future_timestamps) + 1)]
|
| 51 |
+
patterns["Reversal"] = [last_price - i * trend for i in range(1, len(future_timestamps) + 1)]
|
| 52 |
patterns["SMA"] = [sma] * len(future_timestamps)
|
| 53 |
patterns["EMA"] = [ema] * len(future_timestamps)
|
|
|
|
| 54 |
patterns["Cycle"] = [
|
| 55 |
last_price + cycle_amplitude * np.sin(2 * np.pi * i / len(future_timestamps))
|
| 56 |
for i in range(len(future_timestamps))
|
| 57 |
]
|
| 58 |
patterns["Rebound"] = [last_price + trend * (0.5 ** i) for i in range(len(future_timestamps))]
|
| 59 |
patterns["Plateau"] = [last_price] * len(future_timestamps)
|
| 60 |
+
patterns["Breakout"] = [
|
| 61 |
+
last_price + (trend * 2 if i < len(future_timestamps) // 2 else -trend * 2)
|
| 62 |
+
for i in range(len(future_timestamps))
|
| 63 |
+
]
|
| 64 |
patterns["Recovery"] = [last_price + (mean_price - last_price) * (i / len(future_timestamps)) for i in range(len(future_timestamps))]
|
| 65 |
patterns["Acceleration"] = [last_price + (trend * 1.5) * i for i in range(len(future_timestamps))]
|
| 66 |
|
|
|
|
| 70 |
}
|
| 71 |
return pattern_dfs
|
| 72 |
|
| 73 |
+
|
| 74 |
# Fonction pour identifier le pattern le plus ressemblant
|
| 75 |
def identify_best_pattern(df, patterns):
|
| 76 |
mse_scores = {}
|
|
|
|
| 78 |
|
| 79 |
for pattern_name, pattern_df in patterns.items():
|
| 80 |
predicted_values = np.array(pattern_df["price"].values[:len(actual_values)])
|
| 81 |
+
min_length = min(len(actual_values), len(predicted_values))
|
| 82 |
+
mse = mean_squared_error(actual_values[:min_length], predicted_values[:min_length])
|
| 83 |
mse_scores[pattern_name] = mse
|
| 84 |
|
| 85 |
+
# Sélection du pattern avec la MSE la plus faible
|
| 86 |
+
best_pattern = min(mse_scores, key=mse_scores.get)
|
| 87 |
return best_pattern
|
| 88 |
|
| 89 |
+
|
| 90 |
# Configuration des dates
|
| 91 |
end_time = datetime.now()
|
| 92 |
start_time = end_time - timedelta(hours=48)
|
|
|
|
| 94 |
# Liste des cryptos avec leurs IDs corrects sur CoinCap
|
| 95 |
coins = {
|
| 96 |
"Bitcoin": "bitcoin",
|
| 97 |
+
"Ripple": "xrp",
|
| 98 |
"Ethereum": "ethereum",
|
| 99 |
"Tether": "tether",
|
| 100 |
"Stellar": "stellar"
|
|
|
|
| 104 |
crypto_prices = get_crypto_prices(coins, start_time, end_time)
|
| 105 |
|
| 106 |
# Affichage des graphiques
|
| 107 |
+
st.title("Analyse et Prédictions des Cryptos avec Patterns")
|
| 108 |
for coin, price_data in crypto_prices.items():
|
| 109 |
if price_data:
|
| 110 |
df = pd.DataFrame(price_data, columns=["timestamp", "price"])
|