File size: 3,304 Bytes
ba37076
 
 
 
 
 
 
 
 
 
5ec5ce2
ba37076
 
a857666
ba37076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yfinance as yf
import pandas as pd
import numpy as np
import plotly.express as px

# Page configuration
st.set_page_config(page_title="Portfolio Optimization Dashboard", layout="wide")

# Main Title

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")
# Sidebar for User Inputs
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"))

# Execution Button
if st.sidebar.button("Analyze Data"):
    # Clean and format the input tickers
    tickers = [t.strip().upper() for t in tickers_input.split(",")]
    
    with st.spinner('Fetching financial data and calculating returns...'):
        try:
            # Fetch adjusted closing prices from Yahoo Finance
            data = yf.download(tickers, start=start_date, end=end_date)['Close']
            
            # Verify if data is retrieved successfully
            if data.empty:
                st.error("No data found for these tickers. Please verify the symbols and try again.")
            else:
                # Calculate daily returns
                returns = data.pct_change().dropna()
                
                # 1. Plot Cumulative Returns
                st.subheader("πŸ“ˆ Cumulative Returns Performance")
                cumulative_returns = (1 + returns).cumprod()
                st.line_chart(cumulative_returns)

                # Create a two-column layout
                col1, col2 = st.columns(2)
                
                with col1:
                    # 2. Correlation Matrix
                    st.subheader("πŸ”— Correlation Matrix")
                    st.markdown("Analyzes how assets move together to ensure effective diversification.")
                    corr = returns.corr()
                    # Apply a heatmap style to the dataframe
                    st.dataframe(corr.style.background_gradient(cmap='Blues'), use_container_width=True)

                with col2:
                    # 3. Annualized Return and Risk Assessment
                    st.subheader("βš–οΈ Expected Return & Annualized Risk")
                    # Assuming 252 trading days in a year
                    annual_returns = returns.mean() * 252
                    annual_volatility = returns.std() * np.sqrt(252)
                    
                    # Compile the summary into a DataFrame
                    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:
    # Default instruction state
    st.info("πŸ‘ˆ Please enter the stock tickers in the sidebar and click 'Analyze Data' to begin.")