1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
import pandas as pd
import mplfinance as mpf
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import ttk
import os
import matplotlib.pyplot as plt
# Heures été / hiver US
df_bourse = pd.DataFrame([
{'Année': 2022, 'Période': 'Été US', 'Début': '2022-03-14', 'Fin': '2022-03-28', 'Modedate': 1},
{'Année': 2022, 'Période': 'Hiver US', 'Début': '2022-10-31', 'Fin': '2022-11-04', 'Modedate': 1},
{'Année': 2023, 'Période': 'Été US', 'Début': '2023-03-13', 'Fin': '2023-03-24', 'Modedate': 1},
{'Année': 2023, 'Période': 'Hiver US', 'Début': '2023-10-30', 'Fin': '2023-11-03', 'Modedate': 1},
{'Année': 2024, 'Période': 'Été US', 'Début': '2024-03-11', 'Fin': '2024-03-28', 'Modedate': 1},
{'Année': 2024, 'Période': 'Hiver US', 'Début': '2024-10-28', 'Fin': '2024-11-01', 'Modedate': 1}
])
df_bourse['Début'] = pd.to_datetime(df_bourse['Début'])
df_bourse['Fin'] = pd.to_datetime(df_bourse['Fin'])
csv_path = r"C:\Users\NONO\Desktop\Formation Python\FX_NAS100, 5.csv"
df = pd.read_csv(csv_path)
csv_filename = os.path.basename(csv_path)
df["time"] = pd.to_datetime(df["time"], unit='s')
df['date'] = df['time'].dt.date
def determine_modedate(date):
for _, row in df_bourse.iterrows():
if row['Début'].date() <= date <= row['Fin'].date():
return row['Modedate']
return 2
df['Modedate'] = df['date'].apply(determine_modedate)
df['amp_journée'] = 0.0
for day in df['date'].unique():
day_data = df[df['date'] == day]
if not day_data.empty:
modedate = day_data['Modedate'].iloc[0]
if modedate == 1:
start_time = pd.to_datetime("14:30").time()
end_time = pd.to_datetime("21:00").time()
else:
start_time = pd.to_datetime("15:30").time()
end_time = pd.to_datetime("21:55").time()
filtered_data = day_data[(day_data['time'].dt.time >= start_time) & (day_data['time'].dt.time <= end_time)]
if not filtered_data.empty:
high_max = filtered_data['high'].max()
low_min = filtered_data['low'].min()
amplitude = high_max - low_min
df.loc[df['date'] == day, 'amp_journée'] = amplitude
percentages = [25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275]
for p in percentages:
df[f'{p}%'] = df['amp_journée'] * (p / 100)
df[f'-{p}%'] = df['amp_journée'] * (-p / 100)
cols = ['date', 'time', 'open', 'high', 'low', 'close', 'Modedate', 'amp_journée']
percentage_cols = [f'{p}%' for p in percentages] + [f'-{p}%' for p in percentages]
df = df[cols + percentage_cols]
# Interface graphique principale
root = tk.Tk()
root.title("Graphique en chandelier japonais")
root.state('zoomed')
frame = ttk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True)
start_index = 0
num_candles = 100
# Champs pour les pas
step_frame = ttk.Frame(root)
step_frame.pack(side=tk.TOP, fill=tk.X)
tk.Label(step_frame, text="Pas graphique:").pack(side=tk.LEFT, padx=5)
graph_step_entry = ttk.Entry(step_frame, width=5)
graph_step_entry.insert(0, "30")
graph_step_entry.pack(side=tk.LEFT, padx=5)
tk.Label(step_frame, text="Pas zoom:").pack(side=tk.LEFT, padx=5)
zoom_step_entry = ttk.Entry(step_frame, width=5)
zoom_step_entry.insert(0, "30")
zoom_step_entry.pack(side=tk.LEFT, padx=5)
# Fenêtre pour afficher le DataFrame
def show_dataframe():
df_window = tk.Toplevel(root)
df_window.title("Tableau des données")
df_window.state('zoomed')
tree_frame = ttk.Frame(df_window)
tree_frame.pack(fill=tk.BOTH, expand=True)
tree_scroll_y = ttk.Scrollbar(tree_frame, orient=tk.VERTICAL)
tree_scroll_x = ttk.Scrollbar(tree_frame, orient=tk.HORIZONTAL)
tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll_y.set, xscrollcommand=tree_scroll_x.set)
tree_scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
tree_scroll_x.pack(side=tk.BOTTOM, fill=tk.X)
tree_scroll_y.config(command=tree.yview)
tree_scroll_x.config(command=tree.xview)
tree.pack(fill=tk.BOTH, expand=True)
df_display = df.copy().reset_index()
numeric_columns = ['amp_journée'] + [col for col in df_display.columns if '%' in col] + ['open', 'high', 'low', 'close']
for column in numeric_columns:
if column in df_display.columns:
df_display[column] = df_display[column].round(2)
tree["columns"] = ["index"] + list(df_display.columns)[1:]
tree["show"] = "headings"
tree.heading("index", text="Index")
tree.column("index", width=50, anchor='center')
for column in df_display.columns[1:]:
tree.heading(column, text=column)
if column == 'time':
tree.column(column, width=200, anchor='center')
else:
tree.column(column, width=100, anchor='center')
for _, row in df_display.iterrows():
tree.insert("", "end", values=[row['index']] + list(row)[1:])
# Champ de saisie pour l'index
index_frame = ttk.Frame(df_window)
index_frame.pack(side=tk.TOP, fill=tk.X)
tk.Label(index_frame, text="Index:").pack(side=tk.LEFT, padx=5)
index_entry = ttk.Entry(index_frame, width=10)
index_entry.pack(side=tk.LEFT, padx=5)
def select_by_index(event=None):
index = index_entry.get()
if index.isdigit():
index = int(index)
for item in tree.get_children():
if int(tree.item(item)['values'][0]) == index:
tree.selection_set(item)
tree.see(item)
break
index_entry.bind('<Return>', select_by_index)
df_window.mainloop()
btn_show_df = ttk.Button(root, text="Afficher le tableau", command=show_dataframe)
btn_show_df.pack(side=tk.TOP, pady=5)
# Ajouter une ligne rouge et verte
def add_lines(ax, df_slice):
try:
unique_dates = df_slice['date'].unique()
for date in unique_dates:
daily_data = df_slice[df_slice['date'] == date]
if daily_data.empty:
continue
modedate = daily_data['Modedate'].iloc[0]
if modedate == 1:
start_time = pd.to_datetime("14:30").time()
end_time = pd.to_datetime("21:00").time()
else:
start_time = pd.to_datetime("15:30").time()
end_time = pd.to_datetime("21:55").time()
filtered_data = daily_data[
(daily_data['time'].dt.time >= start_time) &
(daily_data['time'].dt.time <= end_time)
]
if filtered_data.empty:
continue
max_high = filtered_data['high'].max()
min_low = filtered_data['low'].min()
time_range = df_slice['time']
xmin = (pd.Timestamp.combine(date, start_time) - time_range.min()).total_seconds() / (time_range.max() - time_range.min()).total_seconds()
xmax = (pd.Timestamp.combine(date, end_time) - time_range.min()).total_seconds() / (time_range.max() - time_range.min()).total_seconds()
ax.axhline(y=max_high, color='red', linestyle='--', xmin=xmin, xmax=xmax)
ax.axhline(y=min_low, color='green', linestyle='--', xmin=xmin, xmax=xmax)
except Exception as e:
pass
# Gestion mouvement de la souris
def on_mouse_move(event):
if event.inaxes:
x, y = event.xdata, event.ydata
ax = event.inaxes
# Effacer les lignes précédentes
for line in ax.lines:
if line.get_label() in ['crosshair_h', 'crosshair_v']:
line.remove()
# Dessiner les nouvelles lignes
ax.axhline(y=y, color='black', linewidth=0.5, label='crosshair_h')
ax.axvline(x=x, color='black', linewidth=0.5, label='crosshair_v')
# Mettre à jour les données dans la fenêtre
if df_slice is not None and len(df_slice) > 0:
index = max(0, min(int(x), len(df_slice) - 1))
data = df_slice.iloc[index]
info_text = f"Index: {start_index + index}\nOpen: {data['open']:.2f}\nHigh: {data['high']:.2f}\nLow: {data['low']:.2f}\nClose: {data['close']:.2f}\nTime: {data['time']}"
info_window.set(info_text)
# Mise à jour des valeurs X et Y
x_value.set(f"X: {data['time']}")
y_value.set(f"Y: {y:.2f}")
# Redessiner le graphique
fig.canvas.draw_idle()
# Evènement molette de la sourie
def on_scroll(event):
global num_candles
if event.button == 'up':
num_candles = max(num_candles - int(zoom_step_entry.get()), 10)
elif event.button == 'down':
num_candles += int(zoom_step_entry.get())
update_chart()
# Rafraichissement graphique
def update_chart():
global start_index, num_candles, df_slice, fig
plt.close('all') # Ferme toutes les figures existantes
df_slice = df.iloc[start_index:start_index + num_candles]
if df_slice.empty:
return
df_ohlc = df_slice[['time', 'open', 'high', 'low', 'close']].copy()
df_ohlc.set_index('time', inplace=True)
fig, axlist = mpf.plot(df_ohlc, type='candle', style='charles', title=csv_filename,
ylabel='Prix', volume=False, returnfig=True)
ax = axlist[0]
add_lines(ax, df_slice)
if hasattr(frame, "canvas"):
frame.canvas.get_tk_widget().destroy()
canvas = FigureCanvasTkAgg(fig, master=frame)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
frame.canvas = canvas
fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
fig.canvas.mpl_connect('scroll_event', on_scroll)
def increase_candles():
global num_candles
num_candles += int(zoom_step_entry.get())
update_chart()
def decrease_candles():
global num_candles
num_candles = max(num_candles - int(zoom_step_entry.get()), 10) # Minimum de 10 chandeliers
update_chart()
def move_right():
global start_index
start_index += int(graph_step_entry.get())
update_chart()
def move_left():
global start_index
start_index = max(start_index - int(graph_step_entry.get()), 0)
update_chart()
# Boutons
button_frame = ttk.Frame(root)
button_frame.pack(side=tk.BOTTOM, fill=tk.X)
plus_button = ttk.Button(button_frame, text="+", command=decrease_candles)
plus_button.pack(side=tk.LEFT, padx=5, pady=5)
minus_button = ttk.Button(button_frame, text="-", command=increase_candles)
minus_button.pack(side=tk.LEFT, padx=5, pady=5)
left_button = ttk.Button(button_frame, text="←", command=move_left)
left_button.pack(side=tk.LEFT, padx=5, pady=5)
right_button = ttk.Button(button_frame, text="→", command=move_right)
right_button.pack(side=tk.LEFT, padx=5, pady=5)
# Fenêtre d'information
info_frame = ttk.Frame(root)
info_frame.pack(side=tk.BOTTOM, fill=tk.X)
# Sous-frame pour les informations centrées
center_info_frame = ttk.Frame(info_frame)
center_info_frame.pack(side=tk.LEFT, expand=True)
info_window = tk.StringVar()
info_label = ttk.Label(center_info_frame, textvariable=info_window, justify=tk.LEFT)
info_label.pack(pady=10)
# Sous-frame pour les valeurs X et Y à droite
xy_info_frame = ttk.Frame(info_frame)
xy_info_frame.pack(side=tk.RIGHT)
x_value = tk.StringVar()
y_value = tk.StringVar()
x_label = ttk.Label(xy_info_frame, textvariable=x_value, justify=tk.LEFT)
y_label = ttk.Label(xy_info_frame, textvariable=y_value, justify=tk.LEFT)
x_label.pack(padx=10, anchor='w')
y_label.pack(padx=10, anchor='w')
update_chart()
root.mainloop() |
Partager