-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmacro_indice_engine.py
More file actions
287 lines (220 loc) · 12.4 KB
/
Copy pathmacro_indice_engine.py
File metadata and controls
287 lines (220 loc) · 12.4 KB
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
""" MACRO ISM (Institute for Supply Management) de la FRED (Federal Reserve Bank of St. Louis)
==========================================================================================
Indice synthétique de santé économique, à partir des séries disponibles sur FRED (sources : OCDE / Eurostat / BCE).
Pour créer une clef API FRED :
- https://fred.stlouisfed.org/docs/api/api_key.html
Cette clef est à placer dans le fichier :
- C:\\Users\\{Nom d'utilisateur}\\AppData\\Local\\TradingInPython\\config.env
sous la forme :
FRED_API_KEY = xxx
Avantage France vs Zone Euro :
- Plusieurs séries sont disponibles en mensuel directement (pas besoin
d'interpoler trimestriel : mensuel pour tous les indicateurs)
- Couverture : production, emploi, inflation, commerce de détail,
confiance consommateur, immatriculations automobiles (proxy demande)
"""
import os
import sys
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.style
import matplotlib.gridspec as gridspec
from matplotlib.figure import Figure
from matplotlib.patches import Patch
from matplotlib.ticker import FuncFormatter
from matplotlib import cm
import warnings
warnings.filterwarnings('ignore')
from datetime import datetime
from fredapi import Fred
from pathlib import Path
base = Path(__file__).resolve().parent.parent
sys.path.append( str(base) )
from styles.watermark import Watermark
from config.path import BASE_CONFIG_DIR
from dotenv import load_dotenv
load_dotenv( BASE_CONFIG_DIR / '.env' )
# ── Configuration ────────────────────────────────────────────────────────────
API_KEY = os.getenv("FRED_API_KEY")
START = '2020-01-01'
END = datetime.now().strftime('%Y-%m-%d')
ROLL_ZSCORE = 36 # fenêtre rolling Z-score (mois) — anti look-ahead
SMOOTH_SHORT = 3
SMOOTH_LONG = 6
fred = Fred(api_key=API_KEY)
# ── Séries France disponibles sur FRED ───────────────────────────────────────
#
# (ticker, label, pct_change_lag, inversion, poids)
# inversion=True : une hausse est négative pour la macro (chômage, inflation)
#from macro_indices.allemagne import INDEX_CONFIG
from macro_indices.france import INDEX_CONFIG
INDEX_NAME = INDEX_CONFIG['name']
SERIES_CONFIG = INDEX_CONFIG['series']
# Normalisation des poids (somme = 1)
total_weight = sum(r[4] for r in SERIES_CONFIG)
SERIES_CONFIG = [(t, l, lag, inv, w / total_weight) for t, l, lag, inv, w in SERIES_CONFIG]
# ── Téléchargement ────────────────────────────────────────────────────────────
print(f"[{END}] Téléchargement des séries FRED — {INDEX_NAME}")
raw = {}
for ticker, label, *_ in SERIES_CONFIG:
_msg = f" • {ticker} ({label}) last date: "
raw[ticker] = fred.get_series( ticker, observation_start=START, observation_end=END )
_msg = _msg + str( raw[ticker].index[-1] )
print( _msg )
# ── Harmonisation mensuelle ───────────────────────────────────────────────────
# Certaines séries peuvent être trimestrielles ; interpolation linéaire en mensuel.
df_raw = {}
for ticker, series in raw.items():
df_raw[ticker] = (
series
.resample('MS').interpolate('time')
.interpolate('time')
)
df_raw = pd.DataFrame(df_raw)
df_raw = df_raw.loc[START:]
# ── Transformations ───────────────────────────────────────────────────────────
df = pd.DataFrame(index=df_raw.index)
for ticker, label, lag, inv, _ in SERIES_CONFIG:
s = df_raw[ticker]
transformed = s.pct_change(lag) if lag > 1 else s.diff(1)
df[f'T_{ticker}'] = -transformed if inv else transformed
# ── Z-score ROLLING (anti look-ahead) ────────────────────────────────────────
for ticker, *_ in SERIES_CONFIG:
col = f'T_{ticker}'
roll = df[col].rolling(ROLL_ZSCORE, min_periods=12)
df[f'Z_{ticker}'] = (df[col] - roll.mean()) / roll.std()
# ── Score composite pondéré ───────────────────────────────────────────────────
df['MACRO_ISM'] = sum(
w * df[f'Z_{ticker}']
for ticker, _, _lag, _inv, w in SERIES_CONFIG
)
df['MACRO_ISM_S'] = df['MACRO_ISM'].rolling(SMOOTH_LONG).mean()
df['MACRO_ISM_MO'] = df['MACRO_ISM'].rolling(SMOOTH_SHORT).mean()
df['MOMENTUM'] = df['MACRO_ISM_S'].diff(1)
# ── Probabilité de récession (sigmoïde inversée) ──────────────────────────────
df['RECESSION_PROB'] = 1 / (1 + np.exp(2 * df['MACRO_ISM_S']))
# ── Régimes (5 états) ────────────────────────────────────────────────────────
REGIME_COLORS = {
'forte_expansion': ('#1a7a2e', '#c8f5d4'),
'expansion': ('#4caf50', '#e8f5e9'),
'neutre': ('#90a4ae', '#f0f4f8'),
'contraction': ('#ef9a9a', '#fff3f3'),
'forte_contraction': ('#c62828', '#ffebee'),
}
def classify(score):
if score > 1.0: return 'forte_expansion'
elif score > 0.4: return 'expansion'
elif score > -0.4: return 'neutre'
elif score > -1.0: return 'contraction'
else: return 'forte_contraction'
df['REGIME'] = df['MACRO_ISM_S'].apply(classify)
df = df.dropna(subset=['MACRO_ISM_S'])
# ── Export CSV ────────────────────────────────────────────────────────────────
# export_cols = (
# ['MACRO_ISM', 'MACRO_ISM_S', 'MACRO_ISM_MO', 'MOMENTUM', 'RECESSION_PROB', 'REGIME']
# + [f'Z_{t}' for t, *_ in SERIES_CONFIG]
# )
# df[export_cols].to_csv('macro_ism_france.csv')
# print("Export CSV → macro_ism_france.csv")
# ── Résumé terminal ───────────────────────────────────────────────────────────
last = df.iloc[-1]
print(f"""
╔══════════════════════════════════════════════════╗
║ MACRO ISM {INDEX_NAME} — DERNIER POINT ║
╠══════════════════════════════════════════════════╣
║ Date : {df.index[-1].strftime('%Y-%m')}
║ Score brut : {last['MACRO_ISM']:+.3f}
║ Score lissé : {last['MACRO_ISM_S']:+.3f}
║ Momentum : {last['MOMENTUM']:+.3f}
║ Proba récession : {last['RECESSION_PROB']:.1%}
║ Régime : {last['REGIME'].upper()}
╚══════════════════════════════════════════════════╝
""")
# ── Graphique multi-panneaux ──────────────────────────────────────────────────
def plot_graphs():
matplotlib.style.use("seaborn-v0_8-notebook")
fig = Figure( figsize=(14, 10) )
Watermark.apply( fig )
gs = gridspec.GridSpec(4, 1, figure=fig, hspace=0.45, wspace=0.30)
ax_main = fig.add_subplot(gs[0, :])
ax_mom = fig.add_subplot(gs[1, :])
ax_rec = fig.add_subplot(gs[2, :])
ax_comp = fig.add_subplot(gs[3, :])
# Panneau 1 — Score principal ─────────────────────────────────────────────────
for i in range(1, len(df)):
regime = df['REGIME'].iloc[i]
_, bg = REGIME_COLORS[regime]
ax_main.axvspan(df.index[i-1], df.index[i], color=bg, alpha=0.9, linewidth=0)
ax_main.plot(df.index, df['MACRO_ISM'], color='#9e9e9e', lw=0.8, alpha=0.6, label='Brut')
ax_main.plot(df.index, df['MACRO_ISM_S'], color='#00267F', lw=2.2, label=f'Lissé ({SMOOTH_LONG}m)')
ax_main.axhline( 1.0, ls=':', color='#2e7d32', lw=1)
ax_main.axhline( 0.4, ls='--', color='#4caf50', lw=0.8)
ax_main.axhline( 0, ls='-', color='black', lw=1)
ax_main.axhline(-0.4, ls='--', color='#ef9a9a', lw=0.8)
ax_main.axhline(-1.0, ls=':', color='#c62828', lw=1)
legend_patches = [Patch(facecolor=bg, label=r.replace('_', ' ').title()) for r, (_, bg) in REGIME_COLORS.items()]
ax_main.legend(handles=legend_patches + ax_main.get_lines()[:2],
loc='lower left', fontsize=8, ncol=4)
ax_main.set_title( f"Macro ISM {INDEX_NAME} — Indice composite pondéré (FRED / OCDE)", fontsize=13, fontweight='bold')
ax_main.set_ylabel('Z-score pondéré')
# Panneau 2 — Momentum ────────────────────────────────────────────────────────
colors_mom = ['#c62828' if v < 0 else '#2e7d32' for v in df['MOMENTUM']]
ax_mom.bar(df.index, df['MOMENTUM'], color=colors_mom, width=20, alpha=0.8)
ax_mom.axhline(0, color='black', lw=0.8)
ax_mom.set_title('Momentum (Δ score lissé 1m)', fontsize=10)
ax_mom.set_ylabel('Δ score')
# Panneau 3 — Probabilité de récession ───────────────────────────────────────
ax_rec.fill_between(df.index, df['RECESSION_PROB'], alpha=0.7, color='#EF4135')
ax_rec.axhline(0.5, ls='--', color='black', lw=1)
ax_rec.set_ylim(0, 1)
ax_rec.set_title( f"Probabilité de récession {INDEX_NAME} (sigmoïde)", fontsize=10 )
ax_rec.set_ylabel('Probabilité')
ax_rec.yaxis.set_major_formatter(FuncFormatter(lambda x, _: f'{x:.0%}'))
# Panneau 4 — Contributions par composante ────────────────────────────────────
comp_cols = [f'Z_{t}' for t, *_ in SERIES_CONFIG]
labels = [l for _, l, *_ in SERIES_CONFIG]
weights = [w for *_, w in SERIES_CONFIG]
LAST_MONTHS = 50
recent = df[comp_cols].tail(LAST_MONTHS)
weighted = recent.multiply(weights)
bottom_pos = pd.Series(0.0, index=recent.index)
bottom_neg = pd.Series(0.0, index=recent.index)
colors_comp = cm.tab10(np.linspace(0, 1, len(comp_cols)))
for col, label, color in zip(weighted.columns, labels, colors_comp):
vals = weighted[col]
pos = vals.clip(lower=0)
neg = vals.clip(upper=0)
ax_comp.bar(recent.index, pos, bottom=bottom_pos, width=20,
label=label, color=color, alpha=0.85)
ax_comp.bar(recent.index, neg, bottom=bottom_neg, width=20,
color=color, alpha=0.85)
bottom_pos = bottom_pos + pos
bottom_neg = bottom_neg + neg
ax_comp.axhline(0, color='black', lw=0.8)
ax_comp.set_title(f'Contributions pondérées par composante ({LAST_MONTHS} derniers mois)', fontsize=10)
ax_comp.set_ylabel('Contribution au score')
ax_comp.legend(loc='upper left', fontsize=7, ncol=3)
fig.suptitle( f'Macro ISM {INDEX_NAME} | Données FRED / OCDE | Généré le {END}', fontsize=11, style='italic', color='#555')
fig.tight_layout()
return fig
def main():
matplotlib.use("Agg")
from user_scripts.api import api
fig = plot_graphs()
api.show_figure( fig, title=f"{api.ticker} - Bull/Bear Strength Index" )
if __name__ == "__main__":
matplotlib.use("TkAgg")
fig = plot_graphs()
fig.tight_layout()
# Créer la fenêtre Tkinter
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
root = tk.Tk()
root.title(f"Bull/Bear Strength Index")
canvas = FigureCanvasTkAgg(fig, master=root)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.draw()
canvas.get_tk_widget().pack(fill="both", expand=True)
root.mainloop()