Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- READ.ME +118 -0
- app.py +130 -0
- best_forex_model.pkl +3 -0
- final_dataset.csv +0 -0
- requirement.txt +8 -0
READ.ME
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Forex Wechselkurs Prognose
|
| 2 |
+
|
| 3 |
+
## Projektbeschreibung
|
| 4 |
+
Prognostiziert zukünftige Wechselkurse für wichtige Währungspaare (EUR/USD, GBP/USD, AUD/USD, USD/CHF, USD/JPY) mithilfe von Machine Learning-Modellen und Wirtschaftsindikatoren. Das Projekt kombiniert Finanzmarktdaten mit makroökonomischen Variablen zur Vorhersage von Währungsbewegungen.
|
| 5 |
+
|
| 6 |
+
## Installation & Setup
|
| 7 |
+
|
| 8 |
+
### Voraussetzungen PIP
|
| 9 |
+
!pip install yfinance pandas_datareader gradio matplotlib scikit-learn statsmodels
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Abhängigkeiten installieren
|
| 13 |
+
pip install -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Web-App starten
|
| 16 |
+
python app.py
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
Die Gradio-App startet auf: http://127.0.0.1:7860
|
| 20 |
+
|
| 21 |
+
### Vollständige Analyse ausführen
|
| 22 |
+
```bash
|
| 23 |
+
jupyter notebook project.ipynb
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
## Ergebnisse
|
| 27 |
+
Linear Regression erwies sich als das beste Modell für alle Währungspaare und erreichte R²-Werte über 0,89 für alle Paare. Das Modell für AUD/USD zeigte die beste Performance mit einem R² von 0,9291. ARIMA-Modelle zeigten durchweg schlechte Performance mit stark negativen R²-Werten, was auf deren Ungeeignetheit für diese Datenstruktur hinweist.
|
| 28 |
+
|
| 29 |
+
## Name & URL
|
| 30 |
+
|
| 31 |
+
| Name | URL |
|
| 32 |
+
|------|-----|
|
| 33 |
+
| Gradio App | Lokale Anwendung (http://127.0.0.1:7860) |
|
| 34 |
+
| Code | GitHub Repository |
|
| 35 |
+
|
| 36 |
+
## Datenquellen und Features pro Quelle
|
| 37 |
+
|
| 38 |
+
| Datenquelle | Features |
|
| 39 |
+
|-------------|----------|
|
| 40 |
+
| Yahoo Finance | EUR/USD, GBP/USD, AUD/USD, USD/CHF, USD/JPY (Schlusskurse) |
|
| 41 |
+
| FRED (Federal Reserve Economic Data) | CPI (Verbraucherpreisindex), FEDFUNDS (Leitzins), UNRATE (Arbeitslosenquote) |
|
| 42 |
+
|
| 43 |
+
## Erstellte Features
|
| 44 |
+
|
| 45 |
+
| Feature | Beschreibung |
|
| 46 |
+
|---------|--------------|
|
| 47 |
+
| lag_1, lag_2, lag_3 | Verzögerte Werte (1, 2 und 3 Tage) für alle Währungspaare |
|
| 48 |
+
| ma_3, ma_7, ma_14 | Gleitende Durchschnitte (3, 7, 14 Tage) für Kurse und Wirtschaftsdaten |
|
| 49 |
+
| std_7, std_14 | Standardabweichung (7 und 14 Tage) für Volatilitätsmessung |
|
| 50 |
+
| CPI_lag1, Rate_lag1, Unemployment_lag1 | Verzögerte Wirtschaftsindikatoren |
|
| 51 |
+
| month, quarter, day_of_week, day_of_year | Zeitbasierte Features |
|
| 52 |
+
| target | Verschobener Zielwert (nächster Tag) für jedes Währungspaar |
|
| 53 |
+
|
| 54 |
+
## Modelltraining
|
| 55 |
+
|
| 56 |
+
### Datenmenge
|
| 57 |
+
* Insgesamt 2.557 Tageswerte pro Währungspaar (2015-2025)
|
| 58 |
+
* 5 Währungspaare × 3 Modelle = 15 trainierte Modelle
|
| 59 |
+
|
| 60 |
+
### Datenaufteilung (Train/Test)
|
| 61 |
+
* Zeitbasierte Aufteilung: Training bis 31.12.2023, Test ab 01.01.2024
|
| 62 |
+
* Verwendung von TimeSeriesSplit für zeitreihengerechte Validierung
|
| 63 |
+
|
| 64 |
+
## Performance
|
| 65 |
+
|
| 66 |
+
| Nr. | Modell | Performance | Features | Beschreibung |
|
| 67 |
+
|-----|--------|-------------|----------|--------------|
|
| 68 |
+
| 1 | Random Forest (EUR/USD) | R²: 0,8947, RMSE: 0,0072 | Alle technischen + fundamentalen Features | Gute Performance, robust |
|
| 69 |
+
| 2 | Linear Regression (EUR/USD) | R²: 0,9168, RMSE: 0,0064 | Alle Features | Beste Performance für EUR/USD |
|
| 70 |
+
| 3 | Random Forest (GBP/USD) | R²: 0,8760, RMSE: 0,0083 | Alle Features | Solide Performance |
|
| 71 |
+
| 4 | Linear Regression (GBP/USD) | R²: 0,8959, RMSE: 0,0076 | Alle Features | Beste Performance für GBP/USD |
|
| 72 |
+
| 5 | Random Forest (AUD/USD) | R²: 0,7959, RMSE: 0,0079 | Alle Features | Schwächere Performance |
|
| 73 |
+
| 6 | Linear Regression (AUD/USD) | R²: 0,9291, RMSE: 0,0047 | Alle Features | **Bestes Gesamtmodell** |
|
| 74 |
+
| 7 | Random Forest (USD/CHF) | R²: 0,9088, RMSE: 0,0064 | Alle Features | Starke Performance |
|
| 75 |
+
| 8 | Linear Regression (USD/CHF) | R²: 0,9248, RMSE: 0,0058 | Alle Features | Beste Performance für USD/CHF |
|
| 76 |
+
| 9 | Random Forest (USD/JPY) | R²: 0,2209, RMSE: 4,1909 | Alle Features | Schwache Performance bei JPY |
|
| 77 |
+
| 10 | Linear Regression (USD/JPY) | R²: 0,9236, RMSE: 1,3126 | Alle Features | Sehr gute Performance |
|
| 78 |
+
| 11 | ARIMA (alle Paare) | R²: -36.703.921 (Durchschnitt) | Nur Zeitreihe | Durchweg schlechte Performance |
|
| 79 |
+
|
| 80 |
+
**Bestes Modell**: Linear Regression für AUD/USD mit R² = 0,9291 und einer Richtungsgenauigkeit von 51,1%
|
| 81 |
+
|
| 82 |
+
## Feature Importance (Top Features - Random Forest)
|
| 83 |
+
|
| 84 |
+
**Wichtigste Features über alle Währungspaare:**
|
| 85 |
+
- **ma30**: Gleitender 30-Tage-Durchschnitt (höchste Vorhersagekraft)
|
| 86 |
+
- **lag1**: Vorheriger Tageskurs
|
| 87 |
+
- **month**: Saisonale Effekte
|
| 88 |
+
- **day_of_year**: Jahreszeit-Indikatoren
|
| 89 |
+
- **CPI/Rate Features**: Makroökonomische Indikatoren
|
| 90 |
+
|
| 91 |
+
## Technische Hinweise
|
| 92 |
+
|
| 93 |
+
### Bekannte Einschränkungen
|
| 94 |
+
- ARIMA-Modelle zeigten extrem schlechte Performance aufgrund der Forex-Datencharakteristika
|
| 95 |
+
- Verwendung von Random Walk als Baseline für Vergleichszwecke
|
| 96 |
+
- Feature Engineering fokussiert auf technische und makroökonomische Indikatoren
|
| 97 |
+
|
| 98 |
+
### Dateistruktur
|
| 99 |
+
```
|
| 100 |
+
├── project.ipynb # Hauptanalyse & Training
|
| 101 |
+
├── app.py # Gradio Web-App für Deployment
|
| 102 |
+
├── requirements.txt # Python-Abhängigkeiten
|
| 103 |
+
├── final_dataset.csv # Verarbeiteter Datensatz
|
| 104 |
+
├── best_forex_model.pkl # Trainiertes Modell
|
| 105 |
+
└── README.md # Projektdokumentation
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### Dependencies (requirements.txt)
|
| 109 |
+
```
|
| 110 |
+
yfinance==0.2.61
|
| 111 |
+
pandas_datareader==0.10.0
|
| 112 |
+
gradio==5.21.0
|
| 113 |
+
matplotlib==3.9.3
|
| 114 |
+
scikit-learn==1.6.1
|
| 115 |
+
statsmodels==0.14.4
|
| 116 |
+
pandas==2.2.3
|
| 117 |
+
numpy==2.2.6
|
| 118 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Modell und Zielspalte laden
|
| 7 |
+
with open("best_forex_model.pkl", "rb") as f:
|
| 8 |
+
model_data = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
model = model_data['model']
|
| 11 |
+
model_name = model_data['model_name']
|
| 12 |
+
model_type = model_data['model_type']
|
| 13 |
+
features = model_data['features']
|
| 14 |
+
performance = model_data['performance']
|
| 15 |
+
target_col = model_data['target_column']
|
| 16 |
+
|
| 17 |
+
print(f"🚀 Modell geladen: {model_name} für {target_col}")
|
| 18 |
+
print(f"📊 Performance: R² = {performance.get('R²', 'N/A'):.4f}")
|
| 19 |
+
|
| 20 |
+
def predict_best_forex_model(current_value, cpi, rate, unemployment):
|
| 21 |
+
try:
|
| 22 |
+
# Aktuelle Datenbasis laden
|
| 23 |
+
final_df = pd.read_csv("final_dataset.csv", parse_dates=['Date'], index_col='Date')
|
| 24 |
+
|
| 25 |
+
# Einfache Feature-Map basierend auf Input
|
| 26 |
+
# Für die meisten Features verwenden wir den aktuellen Input oder 0
|
| 27 |
+
feature_values = []
|
| 28 |
+
|
| 29 |
+
for feat in features:
|
| 30 |
+
if 'lag1' in feat and target_col in feat:
|
| 31 |
+
# Lag1 des Zielpaares = aktueller Input
|
| 32 |
+
feature_values.append(current_value)
|
| 33 |
+
elif 'ma' in feat and target_col in feat:
|
| 34 |
+
# Moving Averages des Zielpaares ≈ aktueller Wert
|
| 35 |
+
feature_values.append(current_value)
|
| 36 |
+
elif 'CPI' in feat:
|
| 37 |
+
if 'lag1' in feat:
|
| 38 |
+
feature_values.append(cpi)
|
| 39 |
+
elif 'ma30' in feat:
|
| 40 |
+
feature_values.append(cpi)
|
| 41 |
+
elif 'change' in feat:
|
| 42 |
+
feature_values.append(0.02) # ~2% typische CPI Änderung
|
| 43 |
+
else:
|
| 44 |
+
feature_values.append(cpi)
|
| 45 |
+
elif 'Rate' in feat:
|
| 46 |
+
if 'lag1' in feat:
|
| 47 |
+
feature_values.append(rate)
|
| 48 |
+
elif 'ma30' in feat:
|
| 49 |
+
feature_values.append(rate)
|
| 50 |
+
elif 'diff' in feat:
|
| 51 |
+
feature_values.append(0.0) # Keine Rate-Änderung angenommen
|
| 52 |
+
else:
|
| 53 |
+
feature_values.append(rate)
|
| 54 |
+
elif 'Unemployment' in feat:
|
| 55 |
+
if 'lag1' in feat:
|
| 56 |
+
feature_values.append(unemployment)
|
| 57 |
+
elif 'ma30' in feat:
|
| 58 |
+
feature_values.append(unemployment)
|
| 59 |
+
elif 'diff' in feat:
|
| 60 |
+
feature_values.append(0.0) # Keine Unemployment-Änderung
|
| 61 |
+
else:
|
| 62 |
+
feature_values.append(unemployment)
|
| 63 |
+
elif feat in ['month', 'quarter', 'day_of_week', 'day_of_year']:
|
| 64 |
+
# Zeitfeatures: Verwende letzte bekannte Werte
|
| 65 |
+
if not final_df.empty:
|
| 66 |
+
last_date = final_df.index[-1]
|
| 67 |
+
if feat == 'month':
|
| 68 |
+
feature_values.append(last_date.month)
|
| 69 |
+
elif feat == 'quarter':
|
| 70 |
+
feature_values.append(last_date.quarter)
|
| 71 |
+
elif feat == 'day_of_week':
|
| 72 |
+
feature_values.append(last_date.dayofweek)
|
| 73 |
+
elif feat == 'day_of_year':
|
| 74 |
+
feature_values.append(last_date.dayofyear)
|
| 75 |
+
else:
|
| 76 |
+
feature_values.append(1) # Default
|
| 77 |
+
else:
|
| 78 |
+
# Andere Currency Pairs oder unbekannte Features
|
| 79 |
+
# Verwende Durchschnittswerte oder 0
|
| 80 |
+
if any(pair in feat for pair in ['EUR/USD', 'GBP/USD', 'AUD/USD', 'USD/CHF', 'USD/JPY']):
|
| 81 |
+
feature_values.append(1.0) # Typischer FX-Wert
|
| 82 |
+
else:
|
| 83 |
+
feature_values.append(0.0)
|
| 84 |
+
|
| 85 |
+
# Feature-Vektor erstellen
|
| 86 |
+
X_pred = np.array([feature_values])
|
| 87 |
+
|
| 88 |
+
# Vorhersage
|
| 89 |
+
if model_type == "regression":
|
| 90 |
+
prediction = model.predict(X_pred)[0]
|
| 91 |
+
confidence = "Hoch" if performance.get('R²', 0) > 0.9 else "Mittel"
|
| 92 |
+
return f"📈 Prognose für {target_col}: {prediction:.4f}\n🎯 Modell: {model_name}\n📊 Konfidenz: {confidence} (R² = {performance.get('R²', 0):.3f})"
|
| 93 |
+
else:
|
| 94 |
+
return f"⚠️ Zeitreihenmodell nicht unterstützt in dieser Demo"
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
error_msg = str(e)
|
| 98 |
+
return f"❌ Fehler bei Vorhersage: {error_msg}\n\n🔍 Debug Info:\n- Features benötigt: {len(features)}\n- Target: {target_col}\n- Model: {model_name}"
|
| 99 |
+
|
| 100 |
+
# Gradio-Interface definieren
|
| 101 |
+
iface = gr.Interface(
|
| 102 |
+
fn=predict_best_forex_model,
|
| 103 |
+
inputs=[
|
| 104 |
+
gr.Number(value=1.0500, label=f"Aktueller Kurs von {target_col}", precision=4),
|
| 105 |
+
gr.Number(value=310.0, label="US Verbraucherpreisindex (CPI)", precision=1),
|
| 106 |
+
gr.Number(value=5.25, label="US Leitzins (%)", precision=2),
|
| 107 |
+
gr.Number(value=4.0, label="US Arbeitslosenquote (%)", precision=1),
|
| 108 |
+
],
|
| 109 |
+
outputs=gr.Textbox(label="📈 Forex Prognose", lines=4),
|
| 110 |
+
title="🚀 Forex-Kursprognose mit Machine Learning",
|
| 111 |
+
description=f"""
|
| 112 |
+
Dieses Tool prognostiziert den zukünftigen Wechselkurs für **{target_col}** basierend auf US-Wirtschaftsdaten.
|
| 113 |
+
|
| 114 |
+
**Modell**: {model_name} | **Performance**: R² = {performance.get('R²', 'N/A'):.4f}
|
| 115 |
+
|
| 116 |
+
Geben Sie US-Wirtschaftsdaten ein, um eine Prognose zu erhalten.
|
| 117 |
+
""",
|
| 118 |
+
theme=gr.themes.Soft(),
|
| 119 |
+
examples=[
|
| 120 |
+
[1.0500, 310.0, 5.25, 4.0],
|
| 121 |
+
[1.0600, 315.0, 5.50, 3.8],
|
| 122 |
+
[1.0400, 308.0, 5.00, 4.2]
|
| 123 |
+
]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# App starten
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
print(f"\n🎯 Starting Gradio App für {target_col} Prognose...")
|
| 129 |
+
print(f"📊 Features verwendet: {len(features)}")
|
| 130 |
+
iface.launch(share=True, debug=True)
|
best_forex_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:38d95a73112674ab3b175ebc3f13757c88de1a4123e63cabfc6e8604d0864a16
|
| 3 |
+
size 2911
|
final_dataset.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirement.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
yfinance==0.2.61
|
| 2 |
+
pandas_datareader==0.10.0
|
| 3 |
+
gradio==5.21.0
|
| 4 |
+
matplotlib==3.9.3
|
| 5 |
+
scikit-learn==1.6.1
|
| 6 |
+
statsmodels==0.14.4
|
| 7 |
+
pandas==2.2.3
|
| 8 |
+
numpy==2.2.6
|