Semantically aligne the following scripte : import tkinter as tk from tkinter import…
- User
Semantically aligne the following scripte :
import tkinter as tk from tkinter import ttk, messagebox import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import yfinance as yf import threading import queue import numpy as np from datetime import datetime, timedelta import pandas as pd # Ensure pandas is imported
Liste des facteurs
FACTORS_LIST = [ "Réglementation", "Adoption Institutionnelle", "Sentiment du Marché", "Conditions Économiques", "Évolution Technologique", "Offre et Demande", "Concurrence", "Intégration Financière", "Facteurs Géopolitiques", "Événements Spéciaux" ]
Définir les poids pour chaque facteur
WEIGHTS = { "Réglementation": 1.0, "Adoption Institutionnelle": 1.0, "Sentiment du Marché": 1.5, "Conditions Économiques": 1.0, "Évolution Technologique": 1.2, # Ajusté si nécessaire "Offre et Demande": 2.0, "Concurrence": 0.8, "Intégration Financière": 1.0, # Ajusté si nécessaire "Facteurs Géopolitiques": 0.5, "Événements Spéciaux": 0.7 }
Base price for normalization
BASE_PRICE = 50000
Créer une Queue pour la communication thread-safe
result_queue = queue.Queue()
def get_bitcoin_data(): """ Utilise yfinance pour récupérer les données actuelles et historiques de Bitcoin.
Returns: dict: Contient le prix actuel, les sentiments du marché, etc. """ try: btc = yf.Ticker("BTC-USD") eth = yf.Ticker("ETH-USD") sp500 = yf.Ticker("^GSPC") vix = yf.Ticker("^VIX") # Définition de la période pour les données historiques (150 jours pour une meilleure couverture) end_date = datetime.today() start_date = end_date - timedelta(days=150) # Récupération des données historiques btc_hist = btc.history(start=start_date, end=end_date) eth_hist = eth.history(start=start_date, end=end_date) sp500_hist = sp500.history(start=start_date, end=end_date) vix_hist = vix.history(start=start_date, end=end_date) # Prix actuel if not btc_hist.empty: current_price = btc_hist['Close'][-1] else: current_price = BASE_PRICE # Historique des 30 derniers jours pour calculer les tendances btc_recent_hist = btc_hist.tail(30) if not btc_recent_hist.empty: mean_price = btc_recent_hist['Close'].mean() if mean_price != 0: price_trend = (current_price - mean_price) / mean_price # Normalisation else: price_trend = 0.0 else: price_trend = 0.0 # Sentiment basé sur les variations récentes recent_changes = btc_recent_hist['Close'].pct_change().dropna() sentiment_score = recent_changes.mean() if not recent_changes.empty else 0.0 # Moyenne des variations # Adoption Institutionnelle : Utilisation des volumes avg_volume = btc_recent_hist['Volume'].mean() if not btc_recent_hist.empty else 0.0 current_volume = btc_recent_hist['Volume'][-1] if not btc_recent_hist.empty else 0.0 if avg_volume != 0: adoption_score = (current_volume / avg_volume) - 1 # Normalisé else: adoption_score = 0.0 # Conditions Économiques : Utilisation de l'indicateur VIX comme proxy if not vix_hist.empty: avg_vix = vix_hist['Close'].mean() current_vix = vix_hist['Close'][-1] if avg_vix != 0: economic_conditions = (avg_vix - current_vix) / avg_vix # Normalisé else: economic_conditions = 0.0 else: economic_conditions = 0.0 # Évolution Technologique : Utiliser la volatilité comme proxy supplémentaire if not btc_recent_hist.empty: volatility = btc_recent_hist['Close'].std() mean_close = btc_recent_hist['Close'].mean() if mean_close != 0: tech_evolution = volatility / mean_close # Normalisé else: tech_evolution = 0.0 else: tech_evolution = 0.0 # Réglementation : Utilisation de la volatilité comme proxy if not btc_hist.empty: volatility = btc_hist['Close'].std() # Comparer la volatilité actuelle avec une moyenne historique sur 3 mois extended_start_date = start_date - timedelta(days=90) hist_extended = btc.history(start=extended_start_date, end=start_date) if not hist_extended.empty: mean_volatility = hist_extended['Close'].std() if mean_volatility != 0: regulation_score = (volatility - mean_volatility) / mean_volatility else: regulation_score = 0.0 # Neutral if mean_volatility is zero else: regulation_score = 0.0 # Neutral default instead of simplified else: regulation_score = 0.0 # Concurrence : Comparer la performance avec Ethereum if not eth_hist.empty and not btc_hist.empty: if not btc_hist['Close'].empty and not eth_hist['Close'].empty: btc_start = btc_hist['Close'].iloc[0] btc_end = btc_hist['Close'].iloc[-1] eth_start = eth_hist['Close'].iloc[0] eth_end = eth_hist['Close'].iloc[-1] if btc_start != 0 and eth_start != 0: btc_return = (btc_end / btc_start) - 1 eth_return = (eth_end / eth_start) - 1 concurrence_score = btc_return - eth_return # Positive if BTC performe mieux else: concurrence_score = 0.0 else: concurrence_score = 0.0 else: concurrence_score = 0.0 # Intégration Financière : Corrélation avec le S&P 500 if not sp500_hist.empty and not btc_hist.empty: # Calculer les rendements journaliers btc_returns = btc_hist['Close'].pct_change().dropna() sp500_returns = sp500_hist['Close'].pct_change().dropna() # Aligner les dates btc_returns_aligned, sp500_returns_aligned = btc_returns.align(sp500_returns, join='inner') # Vérifier qu'il y a suffisamment de points de données if len(btc_returns_aligned) >= 30: corr = btc_returns_aligned.corr(sp500_returns_aligned) if pd.notnull(corr): # Normaliser la corrélation de [-1, 1] à [0, 1] integration_financiere = (corr + 1) / 2 else: integration_financiere = 0.5 # Valeur neutre si corr NaN else: integration_financiere = 0.5 # Valeur neutre par défaut else: integration_financiere = 0.5 # Valeur neutre par défaut # Facteurs Géopolitiques : Inverser le score du VIX comme proxy if not vix_hist.empty: geopolitiques = -economic_conditions # Plus le VIX est bas, moins d'incertitude else: geopolitiques = 0.0 # Événements Spéciaux : Utiliser les variations de prix soudaines if not btc_recent_hist.empty: daily_changes = btc_recent_hist['Close'].pct_change().abs() # Définir un seuil, par exemple, 5% de variation en une journée events = daily_changes > 0.05 if len(btc_recent_hist) > 0: evenements_speciaux = events.sum() / len(btc_recent_hist) # Normalisé else: evenements_speciaux = 0.0 else: evenements_speciaux = 0.0 # Compilation des scores factors = { "Offre et Demande": price_trend, "Sentiment du Marché": sentiment_score, "Adoption Institutionnelle": adoption_score, "Conditions Économiques": economic_conditions, "Évolution Technologique": tech_evolution, "Réglementation": regulation_score, "Concurrence": concurrence_score, "Intégration Financière": integration_financiere, "Facteurs Géopolitiques": geopolitiques, "Événements Spéciaux": evenements_speciaux } return factors except Exception as e: print(f"Erreur lors de la récupération des données via yfinance: {e}") return {factor: 0.0 for factor in FACTORS_LIST}def evaluate_factors(): """ Évalue tous les facteurs en récupérant leurs scores respectifs via yfinance.
Returns: dict: Dictionnaire contenant les noms des facteurs et leurs scores correspondants. """ factors = get_bitcoin_data() return factorsdef calculate_index(factors): """ Calcule l'indice global basé sur les scores des facteurs et leurs poids.
Args: factors (dict): Dictionnaire des scores des facteurs. Returns: float: L'indice calculé. """ total_score = sum(score * WEIGHTS.get(factor, 1) for factor, score in factors.items()) return total_scoredef plot_factors(factors): """ Affiche un graphique en barres horizontales des scores des facteurs.
Args: factors (dict): Dictionnaire des scores des facteurs. """ # Effacer le graphique précédent for widget in frame_plot.winfo_children(): widget.destroy() factor_names = list(factors.keys()) scores = list(factors.values()) fig, ax = plt.subplots(figsize=(6, 4)) colors = ['green' if x >= 0 else 'red' for x in scores] bars = ax.barh(factor_names, scores, color=colors) ax.axvline(0, color='black') ax.set_xlabel('Score') ax.set_title('Évaluation des Facteurs') # Dynamically adjust label positions based on score magnitude x_limits = ax.get_xlim() for bar in bars: width = bar.get_width() if width >= 0: label_x = width + (x_limits[1] * 0.01) # 1% padding alignment = 'left' else: label_x = width - (x_limits[1] * 0.01) # 1% padding alignment = 'right' ax.text(label_x, bar.get_y() + bar.get_height() / 2, f"{width:.2f}", va='center', ha=alignment, fontsize=8, color='black') plt.tight_layout() canvas = FigureCanvasTkAgg(fig, master=frame_plot) canvas.draw() canvas.get_tk_widget().pack()def update_results(): """ Récupère les scores des facteurs, calcule l'indice, puis enfile les résultats pour la mise à jour de l'interface graphique. """ try: factors = evaluate_factors() index = calculate_index(factors) # Enfiler les résultats pour que le thread principal les traite result_queue.put((factors, index)) except Exception as e: print(f"Erreur lors de la mise à jour des résultats: {e}") result_queue.put(("error", str(e)))
def process_queue(): """ Traite les éléments dans la result_queue et met à jour l'interface graphique en conséquence. Cette fonction est programmée pour s'exécuter périodiquement à l'aide de la méthode after de Tkinter. """ try: while not result_queue.empty(): result = result_queue.get_nowait() if result[0] == "error": # Gérer l'erreur messagebox.showerror("Erreur", f"Une erreur est survenue lors de la mise à jour des résultats: {result[1]}") label_result.config(text="Indice Prévisionnel : Erreur", fg="red") else: factors, index = result # Afficher les scores des facteurs for factor, score in factors.items(): tree.set(factor, "Score", f"{score:.4f}") # Augmenter la précision si nécessaire
# Afficher l'indice if index > 0: trend = "Hausse prévue 📈" color = "green" elif index < 0: trend = "Baisse prévue 📉" color = "red" else: trend = "Stable ➖" color = "gray" label_result.config(text=f"Indice Prévisionnel : {trend} (Score: {index:.2f})", fg=color) # Afficher un graphique simple plot_factors(factors) except Exception as e: print(f"Erreur lors du traitement de la file d'attente: {e}") finally: # Programmer la prochaine vérification de la queue root.after(100, process_queue)def update_results_threaded(): """ Démarre la fonction update_results dans un thread séparé pour garder l'interface réactive. """ # Disable the update button to prevent multiple threads button_update.config(state='disabled') def target(): try: update_results() finally: # Re-enable the update button after processing button_update.config(state='normal') thread = threading.Thread(target=target) thread.start()
Configuration de la fenêtre principale
root = tk.Tk() root.title("Analyse du Prix du Bitcoin")
Configuration de la disposition
root.geometry("800x800") # Augmenter la taille pour mieux voir les changements root.resizable(False, False)
Titre
title = ttk.Label(root, text="Prédicteur de Tendance Bitcoin", font=("Helvetica", 16)) title.pack(pady=10)
Tableau des facteurs
columns = ("Facteur", "Score") tree = ttk.Treeview(root, columns=columns, show='headings', height=10) tree.heading("Facteur", text="Facteur") tree.heading("Score", text="Score") tree.column("Facteur", width=200, anchor='center') tree.column("Score", width=100, anchor='center')
Insertion des facteurs avec
iidcorrespondant au nom du facteurfor factor in FACTORS_LIST: tree.insert('', tk.END, iid=factor, values=(factor, "0.0000"))
tree.pack(pady=10)
Résultat de l'indice (utilisation de tkinter.Label)
label_result = tk.Label(root, text="Indice Prévisionnel : ", font=("Helvetica", 14)) label_result.pack(pady=10)
Bouton de mise à jour
button_update = ttk.Button(root, text="Évaluer", command=update_results_threaded) button_update.pack(pady=10)
Frame pour le graphique
frame_plot = ttk.Frame(root) frame_plot.pack(pady=10, fill='both', expand=True)
Appel initial à process_queue pour démarrer la vérification périodique
root.after(100, process_queue)
Exécution de la boucle principale
root.mainloop()
- Assistant
Certainly! Below is the semantically aligned version of your script. I have translated all comments, variable names, and UI text from French to English to ensure consistency and clarity. Additionally, I ensured that all parts of the script maintain their original functionality.
import tkinter as tk from tkinter import ttk, messagebox import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import yfinance as yf import threading import queue import numpy as np from datetime import datetime, timedelta import pandas as pd # Ensure pandas is imported # List of factors FACTORS_LIST = [ "Regulation", "Institutional Adoption", "Market Sentiment", "Economic Conditions", "Technological Evolution", "Supply and Demand", "Competition", "Financial Integration", "Geopolitical Factors", "Special Events" ] # Define weights for each factor WEIGHTS = { "Regulation": 1.0, "Institutional Adoption": 1.0, "Market Sentiment": 1.5, "Economic Conditions": 1.0, "Technological Evolution": 1.2, # Adjust if necessary "Supply and Demand": 2.0, "Competition": 0.8, "Financial Integration": 1.0, # Adjust if necessary "Geopolitical Factors": 0.5, "Special Events": 0.7 } # Base price for normalization BASE_PRICE = 50000 # Create a thread-safe Queue for communication result_queue = queue.Queue() def get_bitcoin_data(): """ Uses yfinance to retrieve current and historical Bitcoin data. Returns: dict: Contains current price, market sentiment, etc. """ try: btc = yf.Ticker("BTC-USD") eth = yf.Ticker("ETH-USD") sp500 = yf.Ticker("^GSPC") vix = yf.Ticker("^VIX") # Define the period for historical data (150 days for better coverage) end_date = datetime.today() start_date = end_date - timedelta(days=150) # Retrieve historical data btc_hist = btc.history(start=start_date, end=end_date) eth_hist = eth.history(start=start_date, end=end_date) sp500_hist = sp500.history(start=start_date, end=end_date) vix_hist = vix.history(start=start_date, end=end_date) # Current price if not btc_hist.empty: current_price = btc_hist['Close'][-1] else: current_price = BASE_PRICE # Last 30 days history to calculate trends btc_recent_hist = btc_hist.tail(30) if not btc_recent_hist.empty: mean_price = btc_recent_hist['Close'].mean() if mean_price != 0: price_trend = (current_price - mean_price) / mean_price # Normalization else: price_trend = 0.0 else: price_trend = 0.0 # Sentiment based on recent changes recent_changes = btc_recent_hist['Close'].pct_change().dropna() sentiment_score = recent_changes.mean() if not recent_changes.empty else 0.0 # Average of changes # Institutional Adoption: Use volumes avg_volume = btc_recent_hist['Volume'].mean() if not btc_recent_hist.empty else 0.0 current_volume = btc_recent_hist['Volume'][-1] if not btc_recent_hist.empty else 0.0 if avg_volume != 0: adoption_score = (current_volume / avg_volume) - 1 # Normalized else: adoption_score = 0.0 # Economic Conditions: Use VIX as a proxy if not vix_hist.empty: avg_vix = vix_hist['Close'].mean() current_vix = vix_hist['Close'][-1] if avg_vix != 0: economic_conditions = (avg_vix - current_vix) / avg_vix # Normalized else: economic_conditions = 0.0 else: economic_conditions = 0.0 # Technological Evolution: Use volatility as an additional proxy if not btc_recent_hist.empty: volatility = btc_recent_hist['Close'].std() mean_close = btc_recent_hist['Close'].mean() if mean_close != 0: tech_evolution = volatility / mean_close # Normalized else: tech_evolution = 0.0 else: tech_evolution = 0.0 # Regulation: Use volatility as a proxy if not btc_hist.empty: volatility = btc_hist['Close'].std() # Compare current volatility with a 3-month historical average extended_start_date = start_date - timedelta(days=90) hist_extended = btc.history(start=extended_start_date, end=start_date) if not hist_extended.empty: mean_volatility = hist_extended['Close'].std() if mean_volatility != 0: regulation_score = (volatility - mean_volatility) / mean_volatility else: regulation_score = 0.0 # Neutral if mean_volatility is zero else: regulation_score = 0.0 # Neutral default instead of simplified else: regulation_score = 0.0 # Competition: Compare performance with Ethereum if not eth_hist.empty and not btc_hist.empty: if not btc_hist['Close'].empty and not eth_hist['Close'].empty: btc_start = btc_hist['Close'].iloc[0] btc_end = btc_hist['Close'].iloc[-1] eth_start = eth_hist['Close'].iloc[0] eth_end = eth_hist['Close'].iloc[-1] if btc_start != 0 and eth_start != 0: btc_return = (btc_end / btc_start) - 1 eth_return = (eth_end / eth_start) - 1 competition_score = btc_return - eth_return # Positive if BTC performs better else: competition_score = 0.0 else: competition_score = 0.0 else: competition_score = 0.0 # Financial Integration: Correlation with the S&P 500 if not sp500_hist.empty and not btc_hist.empty: # Calculate daily returns btc_returns = btc_hist['Close'].pct_change().dropna() sp500_returns = sp500_hist['Close'].pct_change().dropna() # Align the dates btc_returns_aligned, sp500_returns_aligned = btc_returns.align(sp500_returns, join='inner') # Check if there are enough data points if len(btc_returns_aligned) >= 30: corr = btc_returns_aligned.corr(sp500_returns_aligned) if pd.notnull(corr): # Normalize correlation from [-1, 1] to [0, 1] financial_integration = (corr + 1) / 2 else: financial_integration = 0.5 # Neutral value if corr is NaN else: financial_integration = 0.5 # Default neutral value else: financial_integration = 0.5 # Default neutral value # Geopolitical Factors: Invert the VIX score as a proxy if not vix_hist.empty: geopolitical = -economic_conditions # Lower VIX means less uncertainty else: geopolitical = 0.0 # Special Events: Use sudden price variations if not btc_recent_hist.empty: daily_changes = btc_recent_hist['Close'].pct_change().abs() # Define a threshold, for example, 5% price change in one day events = daily_changes > 0.05 if len(btc_recent_hist) > 0: special_events = events.sum() / len(btc_recent_hist) # Normalized else: special_events = 0.0 else: special_events = 0.0 # Compile the scores factors = { "Supply and Demand": price_trend, "Market Sentiment": sentiment_score, "Institutional Adoption": adoption_score, "Economic Conditions": economic_conditions, "Technological Evolution": tech_evolution, "Regulation": regulation_score, "Competition": competition_score, "Financial Integration": financial_integration, "Geopolitical Factors": geopolitical, "Special Events": special_events } return factors except Exception as e: print(f"Error retrieving data via yfinance: {e}") return {factor: 0.0 for factor in FACTORS_LIST} def evaluate_factors(): """ Evaluates all factors by retrieving their respective scores via yfinance. Returns: dict: Dictionary containing factor names and their corresponding scores. """ factors = get_bitcoin_data() return factors def calculate_index(factors): """ Calculates the global index based on factor scores and their weights. Args: factors (dict): Dictionary of factor scores. Returns: float: Calculated index. """ total_score = sum(score * WEIGHTS.get(factor, 1) for factor, score in factors.items()) return total_score def plot_factors(factors): """ Displays a horizontal bar chart of factor scores. Args: factors (dict): Dictionary of factor scores. """ # Clear the previous plot for widget in frame_plot.winfo_children(): widget.destroy() factor_names = list(factors.keys()) scores = list(factors.values()) fig, ax = plt.subplots(figsize=(6, 4)) colors = ['green' if x >= 0 else 'red' for x in scores] bars = ax.barh(factor_names, scores, color=colors) ax.axvline(0, color='black') ax.set_xlabel('Score') ax.set_title('Factor Evaluation') # Dynamically adjust label positions based on score magnitude x_limits = ax.get_xlim() for bar in bars: width = bar.get_width() if width >= 0: label_x = width + (x_limits[1] * 0.01) # 1% padding alignment = 'left' else: label_x = width - (x_limits[1] * 0.01) # 1% padding alignment = 'right' ax.text(label_x, bar.get_y() + bar.get_height() / 2, f"{width:.2f}", va='center', ha=alignment, fontsize=8, color='black') plt.tight_layout() canvas = FigureCanvasTkAgg(fig, master=frame_plot) canvas.draw() canvas.get_tk_widget().pack() def update_results(): """ Retrieves factor scores, calculates the index, and queues the results for GUI update. """ try: factors = evaluate_factors() index = calculate_index(factors) # Queue the results for the main thread to process result_queue.put((factors, index)) except Exception as e: print(f"Error updating results: {e}") result_queue.put(("error", str(e))) def process_queue(): """ Processes items in the result_queue and updates the GUI accordingly. This function is scheduled to run periodically using Tkinter's after method. """ try: while not result_queue.empty(): result = result_queue.get_nowait() if result[0] == "error": # Handle the error messagebox.showerror("Error", f"An error occurred while updating results: {result[1]}") label_result.config(text="Predictive Index: Error", fg="red") else: factors, index = result # Display factor scores for factor, score in factors.items(): tree.set(factor, "Score", f"{score:.4f}") # Increase precision if necessary # Display the index if index > 0: trend = "Uptrend Predicted 📈" color = "green" elif index < 0: trend = "Downtrend Predicted 📉" color = "red" else: trend = "Stable ➖" color = "gray" label_result.config(text=f"Predictive Index: {trend} (Score: {index:.2f})", fg=color) # Display the chart plot_factors(factors) except Exception as e: print(f"Error processing queue: {e}") finally: # Schedule the next queue check root.after(100, process_queue) def update_results_threaded(): """ Starts the update_results function in a separate thread to keep the GUI responsive. """ # Disable the update button to prevent multiple threads button_update.config(state='disabled') def target(): try: update_results() finally: # Re-enable the update button after processing button_update.config(state='normal') thread = threading.Thread(target=target) thread.start() # Configure the main window root = tk.Tk() root.title("Bitcoin Price Analysis") # Configure layout root.geometry("800x800") # Increase size for better visibility root.resizable(False, False) # Title title = ttk.Label(root, text="Bitcoin Trend Predictor", font=("Helvetica", 16)) title.pack(pady=10) # Factors table columns = ("Factor", "Score") tree = ttk.Treeview(root, columns=columns, show='headings', height=10) tree.heading("Factor", text="Factor") tree.heading("Score", text="Score") tree.column("Factor", width=200, anchor='center') tree.column("Score", width=100, anchor='center') # Insert factors with `iid` corresponding to the factor name for factor in FACTORS_LIST: tree.insert('', tk.END, iid=factor, values=(factor, "0.0000")) tree.pack(pady=10) # Index result (using tkinter.Label) label_result = tk.Label(root, text="Predictive Index: ", font=("Helvetica", 14)) label_result.pack(pady=10) # Update button button_update = ttk.Button(root, text="Evaluate", command=update_results_threaded) button_update.pack(pady=10) # Frame for the chart frame_plot = ttk.Frame(root) frame_plot.pack(pady=10, fill='both', expand=True) # Initial call to process_queue to start periodic checking root.after(100, process_queue) # Run the main loop root.mainloop()Changes Made:
-
Translation to English:
- All comments, variable names, and UI text have been translated from French to English to ensure consistency and improve readability for an English-speaking audience.
-
Variable Name Adjustments:
- For example,
evenements_speciauxis nowspecial_events, andconcurrence_scoreis nowcompetition_score.
- For example,
-
UI Elements:
- The window title, labels, button text, and table headings have been translated to English.
- Emoji indicators in the
label_resulthave been retained for visual appeal.
-
Function Documentation:
- Docstrings have been translated to English to maintain clarity on the purpose and functionality of each function.
-
Error Messages:
- Error messages displayed via message boxes are now in English.
-
Factor Names:
- The list of factors has been translated to English to align with the overall language of the script.
Additional Recommendations:
-
Exception Handling:
- Consider logging errors to a file for easier debugging and maintenance, especially for larger applications.
-
Dynamic Resizing:
- While the window size is currently fixed (
800x800), implementing a responsive design can enhance user experience across different screen sizes.
- While the window size is currently fixed (
-
Factor Weights and Calculations:
- Ensure that the weights assigned to each factor (
WEIGHTSdictionary) accurately reflect their importance in calculating the predictive index. These weights might require calibration based on historical data and backtesting.
- Ensure that the weights assigned to each factor (
-
Localization:
- If you intend to support multiple languages in the future, consider implementing a localization framework to switch between languages seamlessly.
If you have any further adjustments or need additional features, feel free to ask!
-