|
import streamlit as st |
|
import yfinance as yf |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
import plotly.express as px |
|
from datetime import datetime, timedelta |
|
|
|
|
|
default_end_date = datetime.today().date() |
|
default_start_date = default_end_date - timedelta(days=365) |
|
|
|
|
|
st.title("📈 Interaktive Analyse von Aktienindizes") |
|
|
|
|
|
start_date = st.date_input("Startdatum", default_start_date) |
|
end_date = st.date_input("Enddatum", default_end_date) |
|
|
|
|
|
ticker1 = st.text_input("Ticker 1", "^GSPC") |
|
ticker2 = st.text_input("Ticker 2", "^NDX") |
|
|
|
|
|
if st.button("Daten abrufen & Diagramme anzeigen"): |
|
|
|
|
|
indices = {ticker1: ticker1, ticker2: ticker2} |
|
data = {} |
|
|
|
for name, symbol in indices.items(): |
|
ticker = yf.Ticker(symbol) |
|
df = ticker.history(start=start_date, end=end_date) |
|
data[name] = df["Close"] |
|
|
|
|
|
df_indices = pd.DataFrame(data) |
|
|
|
|
|
if df_indices.empty: |
|
st.error("❌ Keine Daten für die angegebenen Ticker gefunden!") |
|
else: |
|
|
|
fig1 = go.Figure() |
|
|
|
|
|
fig1.add_trace(go.Scatter(x=df_indices.index, y=df_indices[ticker1], mode='lines', |
|
name=ticker1, yaxis='y1')) |
|
|
|
|
|
fig1.add_trace(go.Scatter(x=df_indices.index, y=df_indices[ticker2], mode='lines', |
|
name=ticker2, yaxis='y2')) |
|
|
|
|
|
fig1.update_layout( |
|
title=f"Vergleich der Close-Preise: {ticker1} vs. {ticker2}", |
|
xaxis=dict(title="Datum"), |
|
yaxis=dict(title=f"{ticker1} Preis (USD)", side='left', showgrid=False), |
|
yaxis2=dict(title=f"{ticker2} Preis (USD)", side='right', overlaying='y', showgrid=False), |
|
legend=dict(x=0, y=1), |
|
hovermode="x" |
|
) |
|
|
|
|
|
st.plotly_chart(fig1, use_container_width=True) |
|
|
|
|
|
df_normalized = df_indices / df_indices.iloc[0] * 100 |
|
|
|
fig2 = px.line(df_normalized, x=df_normalized.index, y=df_normalized.columns, |
|
title=f"Normierter Vergleich der Close-Preise: {ticker1} vs. {ticker2}", |
|
labels={"value": "Index (Startwert = 100)", "variable": "Index"}, |
|
template="plotly_white") |
|
|
|
|
|
st.plotly_chart(fig2, use_container_width=True) |
|
|