| import streamlit as st |
| import yfinance as yf |
| import pandas as pd |
| import numpy as np |
| import plotly.express as px |
|
|
| |
| st.set_page_config(page_title="Portfolio Optimization Dashboard", layout="wide") |
|
|
| |
|
|
| st.title("π Markowitz Portfolio Optimization Dashboard") |
| st.markdown("An interactive application to analyze returns and risks for a portfolio of financial assets using real market data.") |
| st.title("Dr .medini Atmane university of Eloued Algeria") |
| |
| st.sidebar.header("Portfolio Settings") |
| tickers_input = st.sidebar.text_input("Enter Ticker Symbols (comma-separated)", "AAPL, MSFT, GOOGL") |
| start_date = st.sidebar.date_input("Start Date", pd.to_datetime("2023-01-01")) |
| end_date = st.sidebar.date_input("End Date", pd.to_datetime("today")) |
|
|
| |
| if st.sidebar.button("Analyze Data"): |
| |
| tickers = [t.strip().upper() for t in tickers_input.split(",")] |
| |
| with st.spinner('Fetching financial data and calculating returns...'): |
| try: |
| |
| data = yf.download(tickers, start=start_date, end=end_date)['Close'] |
| |
| |
| if data.empty: |
| st.error("No data found for these tickers. Please verify the symbols and try again.") |
| else: |
| |
| returns = data.pct_change().dropna() |
| |
| |
| st.subheader("π Cumulative Returns Performance") |
| cumulative_returns = (1 + returns).cumprod() |
| st.line_chart(cumulative_returns) |
|
|
| |
| col1, col2 = st.columns(2) |
| |
| with col1: |
| |
| st.subheader("π Correlation Matrix") |
| st.markdown("Analyzes how assets move together to ensure effective diversification.") |
| corr = returns.corr() |
| |
| st.dataframe(corr.style.background_gradient(cmap='Blues'), use_container_width=True) |
|
|
| with col2: |
| |
| st.subheader("βοΈ Expected Return & Annualized Risk") |
| |
| annual_returns = returns.mean() * 252 |
| annual_volatility = returns.std() * np.sqrt(252) |
| |
| |
| summary_df = pd.DataFrame({ |
| "Expected Annual Return": annual_returns, |
| "Annualized Volatility (Risk)": annual_volatility |
| }) |
| st.dataframe(summary_df.style.format("{:.2%}"), use_container_width=True) |
| |
| except Exception as e: |
| st.error(f"An error occurred while processing the data: {e}") |
| else: |
| |
| st.info("π Please enter the stock tickers in the sidebar and click 'Analyze Data' to begin.") |