# plots.py
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import streamlit as st
import numpy as np
from plotly.subplots import make_subplots

def plot_cagr_over_time(cum_returns, time_frames=['Weekly', 'Monthly', 'Quarterly', 'Annually'], label="Portfolio"):
    """
    Plot the Compound Annual Growth Rate (CAGR) over multiple time frames for a single portfolio.
    
    Parameters:
    - cum_returns (pd.Series): Cumulative returns of the portfolio.
    - time_frames (list): List of time frames to calculate CAGR.
    - label (str): Label for the portfolio.
    """
    try:
        frequency_map = {
            'Weekly': 'W',
            'Monthly': 'M',
            'Quarterly': 'Q',
            'Annually': 'Y'
        }
        fig = go.Figure()
        for tf in time_frames:
            freq = frequency_map.get(tf)
            if not freq:
                continue
            rolled = cum_returns.resample(freq).last()
            if rolled.empty:
                st.warning(f"Not enough data to calculate {tf} CAGR.")
                continue
            years = (rolled.index[-1] - rolled.index[0]).days / 365.25
            if years <= 0:
                st.warning(f"Not enough time elapsed to calculate {tf} CAGR.")
                continue
            cagr = (rolled / rolled.iloc[0]) ** (1/years) - 1
            fig.add_trace(go.Scatter(x=rolled.index, y=cagr, mode='lines', name=f'{tf} CAGR - {label}'))
        fig.update_layout(
            title=f'CAGR Over Multiple Time Frames - {label}',
            xaxis_title='Date',
            yaxis_title='CAGR',
            hovermode='x unified'
        )
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting CAGR over time: {e}")

def plot_rolling_metrics(returns, windows=[30, 90, 180, 252], label='Portfolio'):
    """
    Plot rolling metrics (Volatility and Sharpe Ratio) over specified rolling window periods.
    
    Parameters:
    - returns (pd.Series or pd.DataFrame): Daily returns of the portfolio.
    - windows (list): Rolling window sizes in days (e.g., [30, 90, 180, 252]).
    - label (str): Label for the portfolio.
    """
    try:
        # Ensure returns are in DataFrame format
        if isinstance(returns, pd.Series):
            returns = returns.to_frame(name='Return')
        elif isinstance(returns, pd.DataFrame):
            pass
        else:
            st.error("Returns should be a pandas DataFrame or Series.")
            return
        
        # Check if 'Return' column exists
        if 'Return' not in returns.columns:
            st.error("Returns DataFrame must contain a 'Return' column.")
            return
        
        # Initialize a DataFrame to store metrics
        metrics_df = pd.DataFrame(index=returns.index)
        
        for window in windows:
            if window <= 0:
                st.warning(f"Ignored non-positive rolling window: {window}")
                continue
            
            # Calculate rolling volatility (annualized)
            rolling_vol = returns['Return'].rolling(window=window).std() * np.sqrt(252)
            metrics_df[f'Rolling {window}-Day Volatility'] = rolling_vol
            
            # Calculate rolling Sharpe Ratio (assuming risk-free rate of 2%)
            risk_free_rate = 0.02
            rolling_return = returns['Return'].rolling(window=window).mean() * 252
            rolling_sharpe = (rolling_return - risk_free_rate) / rolling_vol
            metrics_df[f'Rolling {window}-Day Sharpe Ratio'] = rolling_sharpe
        
        # Drop rows with all NaN values
        metrics_df.dropna(how='all', inplace=True)
        
        if metrics_df.empty:
            st.warning("No data available to plot rolling metrics. Please check the rolling windows and data.")
            return
        
        # Create subplots: one for Volatility, one for Sharpe Ratio
        fig = make_subplots(
            rows=2, cols=1,
            subplot_titles=(f'Rolling Volatility - {label}', f'Rolling Sharpe Ratio - {label}'),
            shared_xaxes=True,
            vertical_spacing=0.1,
            row_heights=[0.5, 0.5]
        )
        
        # Plot Rolling Volatility
        for window in windows:
            col_name = f'Rolling {window}-Day Volatility'
            if col_name in metrics_df.columns:
                fig.add_trace(
                    go.Scatter(
                        x=metrics_df.index,
                        y=metrics_df[col_name],
                        mode='lines',
                        name=f'{window}-Day Volatility'
                    ),
                    row=1, col=1
                )
        
        # Plot Rolling Sharpe Ratio
        for window in windows:
            col_name = f'Rolling {window}-Day Sharpe Ratio'
            if col_name in metrics_df.columns:
                fig.add_trace(
                    go.Scatter(
                        x=metrics_df.index,
                        y=metrics_df[col_name],
                        mode='lines',
                        name=f'{window}-Day Sharpe'
                    ),
                    row=2, col=1
                )
        
        # Update layout
        fig.update_layout(
            title=f'Rolling Metrics Over Time - {label}',
            height=800,
            template='plotly_dark',
            hovermode='x unified'
        )
        
        fig.update_xaxes(title_text="Date")
        fig.update_yaxes(title_text="Volatility", row=1, col=1)
        fig.update_yaxes(title_text="Sharpe Ratio", row=2, col=1)
        
        st.plotly_chart(fig, use_container_width=True)

def plot_risk_return_attribution(returns, weights, label='Portfolio'):
    """
    Plot a Risk-Return Attribution analysis for the portfolio.
    
    Parameters:
    - returns (pd.DataFrame or pd.Series): Returns of the portfolio's assets.
    - weights (array-like): Allocation weights corresponding to each asset.
    - label (str): Label for the portfolio.
    """
    try:
        # Ensure returns are in DataFrame format
        if isinstance(returns, pd.Series):
            returns = returns.to_frame(name='Asset')
        elif isinstance(returns, pd.DataFrame):
            pass
        else:
            st.error("Returns should be a pandas DataFrame or Series.")
            return
        
        # Ensure weights are a numpy array
        weights = np.array(weights)
        
        # Check if the number of weights matches the number of assets
        if len(weights) != returns.shape[1]:
            st.error("Number of weights must match number of assets.")
            return
        
        # Calculate annualized returns and volatilities
        annual_returns = returns.mean() * 252
        annual_volatility = returns.std() * np.sqrt(252)
        
        # Calculate contribution to portfolio return and volatility
        portfolio_return = np.dot(weights, annual_returns)
        portfolio_volatility = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
        
        # Calculate contribution of each asset to portfolio return and volatility
        contribution_return = weights * annual_returns
        contribution_volatility = weights * annual_volatility
        
        # Create a DataFrame for plotting
        attribution_df = pd.DataFrame({
            'Asset': returns.columns,
            'Weight': weights,
            'Annual Return': annual_returns,
            'Contribution to Return': contribution_return,
            'Annual Volatility': annual_volatility,
            'Contribution to Volatility': contribution_volatility
        })
        
        # Plotting
        fig = make_subplots(
            rows=1, cols=2,
            subplot_titles=(f'Contribution to Return - {label}', f'Contribution to Volatility - {label}'),
            specs=[[{'type': 'bar'}, {'type': 'bar'}]]
        )
        
        # Contribution to Return
        fig.add_trace(
            go.Bar(
                x=attribution_df['Asset'],
                y=attribution_df['Contribution to Return'],
                marker_color=px.colors.qualitative.Set3,
                name='Return Contribution'
            ),
            row=1, col=1
        )
        
        # Contribution to Volatility
        fig.add_trace(
            go.Bar(
                x=attribution_df['Asset'],
                y=attribution_df['Contribution to Volatility'],
                marker_color=px.colors.qualitative.Set3,
                name='Volatility Contribution'
            ),
            row=1, col=2
        )
        
        fig.update_layout(
            title_text=f'Risk-Return Attribution Analysis - {label}',
            yaxis_title='Contribution',
            yaxis2_title='Contribution',
            template='plotly_dark',
            showlegend=False
        )
        
        st.plotly_chart(fig, use_container_width=True)
        
    except Exception as e:
        st.error(f"Error plotting risk-return attribution: {e}")

def plot_correlation_heatmap(returns):
    """
    Plot a correlation heatmap of the asset returns.
    
    Parameters:
    - returns (pd.DataFrame or pd.Series): Returns of the portfolio's assets.
    """
    try:
        # Ensure returns are in DataFrame format
        if isinstance(returns, pd.Series):
            returns = returns.to_frame(name='Asset')
        
        # Calculate correlation matrix
        corr_matrix = returns.corr()
        
        if corr_matrix.empty:
            st.warning("Correlation matrix is empty. Unable to plot heatmap.")
            return
        
        # Create heatmap using Plotly Express
        fig = px.imshow(
            corr_matrix,
            title='Correlation Heatmap of Assets',
            labels=dict(x="Assets", y="Assets", color="Correlation"),
            color_continuous_scale='Viridis',
            zmin=-1,
            zmax=1,
            text_auto=True,
            aspect="auto",
            template='plotly_dark'
        )
        
        # Improve layout
        fig.update_xaxes(side="top")
        fig.update_layout(
            xaxis_title="Assets",
            yaxis_title="Assets",
            coloraxis_colorbar=dict(title="Correlation"),
            margin=dict(l=50, r=50, t=80, b=50)
        )
        
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting correlation heatmap: {e}")

def plot_allocation_pie(weights, assets, title="Portfolio Allocation", hover_info="percent+name"):
    """
    Plot a pie chart showing the portfolio allocation.
    
    Parameters:
    - weights (array-like): Allocation weights (should sum to 1 or 100).
    - assets (list): List of asset names.
    - title (str): Title of the pie chart.
    - hover_info (str): Information to display on hover, e.g., "percent+name".
    """
    try:
        # Ensure weights sum to 1
        total = sum(weights)
        if not np.isclose(total, 1.0):
            weights = np.array(weights) / total
        
        fig = go.Figure(data=[go.Pie(
            labels=assets,
            values=weights,
            hoverinfo=hover_info,
            textinfo='percent',
            marker=dict(colors=px.colors.qualitative.Set3)
        )])
        
        fig.update_layout(
            title=title,
            template='plotly_dark'
        )
        
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting allocation pie chart: {e}")

def plot_growth_comparison(cum_returns_a, cum_returns_b, labels=None):
    """
    Plot the growth comparison between two portfolios.
    
    Parameters:
    - cum_returns_a (pd.Series): Cumulative returns of the first portfolio.
    - cum_returns_b (pd.Series): Cumulative returns of the second portfolio.
    - labels (list, optional): List containing labels for the two portfolios.
    """
    try:
        # Align the indices to ensure matching dates
        common_index = cum_returns_a.index.intersection(cum_returns_b.index)
        cum_returns_a = cum_returns_a.loc[common_index]
        cum_returns_b = cum_returns_b.loc[common_index]

        if cum_returns_a.empty or cum_returns_b.empty:
            st.warning("No overlapping data to plot growth comparison.")
            return

        df = pd.DataFrame({
            'Date': cum_returns_a.index,
            'Portfolio 1': cum_returns_a.values,
            'Portfolio 2': cum_returns_b.values
        })

        # Assign labels if provided
        if labels and isinstance(labels, list) and len(labels) == 2:
            df.rename(columns={'Portfolio 1': labels[0], 'Portfolio 2': labels[1]}, inplace=True)
        else:
            df.rename(columns={'Portfolio 1': 'Portfolio A', 'Portfolio 2': 'Portfolio B'}, inplace=True)

        fig = px.line(
            df,
            x='Date',
            y=df.columns[1:],  # Dynamically select the plotted columns
            title='Growth Comparison',
            labels={'value': 'Cumulative Returns', 'Date': 'Date'},
            hover_data={'Date': '|%B %d, %Y'},  # Enhanced hover format
            template='plotly_dark'  # Use dark template for better contrast
        )

        fig.update_layout(
            hovermode='x unified',
            xaxis=dict(rangeslider=dict(visible=True), type='date'),
        )
        fig.update_traces(line=dict(width=2))
        st.plotly_chart(fig, use_container_width=True, config={
            'scrollZoom': True
        })
    except Exception as e:
        st.error(f"Error plotting growth comparison: {e}")

def plot_drawdown_comparison(drawdown_a, drawdown_b, labels=None):
    """
    Plot the drawdown comparison between two portfolios.
    
    Parameters:
    - drawdown_a (pd.Series): Drawdown series of the first portfolio.
    - drawdown_b (pd.Series): Drawdown series of the second portfolio.
    - labels (list, optional): List containing labels for the two portfolios.
    """
    try:
        # Align the indices to ensure matching dates
        common_index = drawdown_a.index.intersection(drawdown_b.index)
        drawdown_a = drawdown_a.loc[common_index]
        drawdown_b = drawdown_b.loc[common_index]

        if drawdown_a.empty or drawdown_b.empty:
            st.warning("No overlapping data to plot drawdown comparison.")
            return

        df = pd.DataFrame({
            'Date': drawdown_a.index,
            'Drawdown 1': drawdown_a.values,
            'Drawdown 2': drawdown_b.values
        })

        # Assign labels if provided
        if labels and isinstance(labels, list) and len(labels) == 2:
            df.rename(columns={'Drawdown 1': labels[0], 'Drawdown 2': labels[1]}, inplace=True)
        else:
            df.rename(columns={'Drawdown 1': 'Portfolio A', 'Drawdown 2': 'Portfolio B'}, inplace=True)

        fig = px.line(
            df,
            x='Date',
            y=df.columns[1:],  # Dynamically select the plotted columns
            title='Drawdown Comparison',
            labels={'value': 'Drawdown (%)', 'Date': 'Date'},
            hover_data={'Date': '|%B %d, %Y'},  # Enhanced hover format
            template='plotly_dark'  # Use dark template for better contrast
        )

        fig.update_layout(
            hovermode='x unified',
            xaxis=dict(rangeslider=dict(visible=True), type='date'),
        )
        fig.update_traces(line=dict(width=2))
        st.plotly_chart(fig, use_container_width=True, config={
            'scrollZoom': True
        })
    except Exception as e:
        st.error(f"Error plotting drawdown comparison: {e}")

def plot_cagr_comparison(cum_returns_a, cum_returns_b, time_frames=['Weekly', 'Monthly', 'Quarterly', 'Annually']):
    """
    Plot the Compound Annual Growth Rate (CAGR) over multiple time frames for comparison.
    
    Parameters:
    - cum_returns_a (pd.Series): Cumulative returns of the first portfolio.
    - cum_returns_b (pd.Series): Cumulative returns of the second portfolio.
    - time_frames (list): List of time frames to calculate CAGR.
    """
    try:
        frequency_map = {
            'Weekly': 'W',
            'Monthly': 'M',
            'Quarterly': 'Q',
            'Annually': 'Y'
        }
        fig = go.Figure()
        for tf in time_frames:
            freq = frequency_map.get(tf)
            if not freq:
                continue
            rolled_a = cum_returns_a.resample(freq).last()
            rolled_b = cum_returns_b.resample(freq).last()
            if rolled_a.empty or rolled_b.empty:
                st.warning(f"Not enough data to calculate {tf} CAGR.")
                continue
            years_a = (rolled_a.index[-1] - rolled_a.index[0]).days / 365.25
            years_b = (rolled_b.index[-1] - rolled_b.index[0]).days / 365.25
            if years_a <= 0 or years_b <= 0:
                st.warning(f"Not enough time elapsed to calculate {tf} CAGR.")
                continue
            cagr_a = (rolled_a / rolled_a.iloc[0]) ** (1/years_a) - 1
            cagr_b = (rolled_b / rolled_b.iloc[0]) ** (1/years_b) - 1
            fig.add_trace(go.Scatter(x=rolled_a.index, y=cagr_a, mode='lines', name=f'{tf} CAGR - Portfolio A'))
            fig.add_trace(go.Scatter(x=rolled_b.index, y=cagr_b, mode='lines', name=f'{tf} CAGR - Portfolio B'))
        fig.update_layout(
            title='CAGR Over Multiple Time Frames - Comparison',
            xaxis_title='Date',
            yaxis_title='CAGR',
            hovermode='x unified'
        )
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting CAGR comparison: {e}")

def plot_allocation_comparison(weights_a, assets_a, weights_b, assets_b, title='Portfolio Allocation Comparison'):
    """
    Plot side-by-side pie charts to compare portfolio allocations.
    
    Parameters:
    - weights_a (array-like): Allocation weights for the first portfolio.
    - assets_a (list): List of asset names for the first portfolio.
    - weights_b (array-like): Allocation weights for the second portfolio.
    - assets_b (list): List of asset names for the second portfolio.
    - title (str): Title of the plot.
    """
    try:
        allocation_df_a = pd.DataFrame({
            'Asset': assets_a,
            'Weight': weights_a
        })

        allocation_df_b = pd.DataFrame({
            'Asset': assets_b,
            'Weight': weights_b
        })

        # Create subplots for side-by-side pie charts
        fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]],
                            subplot_titles=('Portfolio A', 'Portfolio B'))

        fig.add_trace(go.Pie(labels=allocation_df_a['Asset'], values=allocation_df_a['Weight'],
                             name='Portfolio A',
                             hoverinfo='label+percent',
                             textinfo='percent',
                             marker=dict(colors=px.colors.qualitative.Set3)),
                      row=1, col=1)

        fig.add_trace(go.Pie(labels=allocation_df_b['Asset'], values=allocation_df_b['Weight'],
                             name='Portfolio B',
                             hoverinfo='label+percent',
                             textinfo='percent',
                             marker=dict(colors=px.colors.qualitative.Set3)),
                      row=1, col=2)

        fig.update_traces(textposition='inside', showlegend=False)

        fig.update_layout(
            title_text=title,
            annotations=[dict(text='Portfolio A', x=0.18, y=0.5, font_size=12, showarrow=False),
                         dict(text='Portfolio B', x=0.82, y=0.5, font_size=12, showarrow=False)],
            template='plotly_dark'
        )

        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting allocation comparison: {e}")

def plot_rolling_metrics_comparison(returns_a, weights_a, returns_b, weights_b, windows=[30, 90, 180, 252]):
    """
    Plot rolling metrics (volatility and Sharpe ratio) over specified windows for comparison.
    
    Parameters:
    - returns_a (pd.Series): Returns of the first portfolio.
    - weights_a (array-like): Allocation weights of the first portfolio.
    - returns_b (pd.Series): Returns of the second portfolio.
    - weights_b (array-like): Allocation weights of the second portfolio.
    - windows (list): List of rolling window sizes in days.
    """
    try:
        metrics_a = {}
        metrics_b = {}
        for window in windows:
            rolling_return_a = returns_a.rolling(window).mean() * 252
            rolling_vol_a = returns_a.rolling(window).std() * np.sqrt(252)
            rolling_sharpe_a = (rolling_return_a - 0.02) / rolling_vol_a  # Assuming rf rate of 2%

            rolling_return_b = returns_b.rolling(window).mean() * 252
            rolling_vol_b = returns_b.rolling(window).std() * np.sqrt(252)
            rolling_sharpe_b = (rolling_return_b - 0.02) / rolling_vol_b  # Assuming rf rate of 2%

            metrics_a[f'Rolling {window}-Day Volatility - Portfolio A'] = rolling_vol_a
            metrics_a[f'Rolling {window}-Day Sharpe Ratio - Portfolio A'] = rolling_sharpe_a

            metrics_b[f'Rolling {window}-Day Volatility - Portfolio B'] = rolling_vol_b
            metrics_b[f'Rolling {window}-Day Sharpe Ratio - Portfolio B'] = rolling_sharpe_b

        fig = go.Figure()

        for metric_name, metric_series in metrics_a.items():
            fig.add_trace(go.Scatter(x=metric_series.index, y=metric_series, mode='lines', name=metric_name))

        for metric_name, metric_series in metrics_b.items():
            fig.add_trace(go.Scatter(x=metric_series.index, y=metric_series, mode='lines', name=metric_name))

        fig.update_layout(
            title='Rolling Metrics Over User-Defined Periods - Comparison',
            xaxis_title='Date',
            yaxis_title='Value',
            hovermode='x unified',
            template='plotly_dark'
        )
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting rolling metrics comparison: {e}")

def plot_correlation_heatmap_comparison(returns_a, returns_b):
    """
    Plot a heatmap of the correlation matrix for both portfolios combined.
    
    Parameters:
    - returns_a (pd.DataFrame or pd.Series): Returns of the first portfolio.
    - returns_b (pd.DataFrame or pd.Series): Returns of the second portfolio.
    """
    try:
        combined_returns = pd.concat([returns_a, returns_b], axis=1)
        combined_returns.columns = ['Portfolio A'] + (returns_b.columns.tolist() if isinstance(returns_b, pd.DataFrame) else ['Portfolio B'])

        if combined_returns.shape[1] < 2:
            st.warning("Correlation heatmap requires at least two assets with variability.")
            return

        corr_matrix = combined_returns.corr()

        # Check if the correlation matrix is not empty
        if corr_matrix.empty:
            st.warning("Correlation matrix is empty. Unable to plot heatmap.")
            return

        fig = px.imshow(
            corr_matrix,
            title='Correlation Heatmap - Comparison',
            labels={'x': 'Portfolio', 'y': 'Portfolio', 'color': 'Correlation'},
            color_continuous_scale='Portland',
            zmin=-1,
            zmax=1,
            text_auto=True,
            aspect="auto",
            template='plotly_dark'
        )
        fig.update_xaxes(side="top")  # Move x-axis labels to the top for better readability
        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting correlation heatmap: {e}")

def plot_risk_return_attribution_comparison(returns_a, weights_a, returns_b, weights_b, label_a='Portfolio A', label_b='Portfolio B'):
    """
    Plot the risk-return attribution comparison between two portfolios.
    
    Parameters:
    - returns_a (pd.Series or pd.DataFrame): Returns of the first portfolio.
    - weights_a (array-like): Allocation weights of the first portfolio.
    - returns_b (pd.Series or pd.DataFrame): Returns of the second portfolio.
    - weights_b (array-like): Allocation weights of the second portfolio.
    - label_a (str): Label for the first portfolio.
    - label_b (str): Label for the second portfolio.
    """
    try:
        # Portfolio A
        asset_returns_a = returns_a.mean() * 252  # Annualized return
        asset_volatility_a = returns_a.std() * np.sqrt(252)  # Annualized volatility
        asset_contribution_a = weights_a * asset_volatility_a

        # Portfolio B
        asset_returns_b = returns_b.mean() * 252  # Annualized return
        asset_volatility_b = returns_b.std() * np.sqrt(252)  # Annualized volatility
        asset_contribution_b = weights_b * asset_volatility_b

        # Create DataFrames for plotting
        df_a = pd.DataFrame({
            'Asset': returns_a.columns if isinstance(returns_a, pd.DataFrame) else ['Asset A'],
            'Volatility': asset_volatility_a if isinstance(asset_volatility_a, np.ndarray) else [asset_volatility_a],
            'Return': asset_returns_a if isinstance(asset_returns_a, np.ndarray) else [asset_returns_a],
            'Contribution': asset_contribution_a if isinstance(asset_contribution_a, np.ndarray) else [asset_contribution_a]
        })

        df_b = pd.DataFrame({
            'Asset': returns_b.columns if isinstance(returns_b, pd.DataFrame) else ['Asset B'],
            'Volatility': asset_volatility_b if isinstance(asset_volatility_b, np.ndarray) else [asset_volatility_b],
            'Return': asset_returns_b if isinstance(asset_returns_b, np.ndarray) else [asset_returns_b],
            'Contribution': asset_contribution_b if isinstance(asset_contribution_b, np.ndarray) else [asset_contribution_b]
        })

        # Combine for plotting
        df_a['Portfolio'] = label_a
        df_b['Portfolio'] = label_b
        combined_df = pd.concat([df_a, df_b], ignore_index=True)

        fig = px.scatter(
            combined_df,
            x='Volatility',
            y='Return',
            size='Contribution',
            color='Portfolio',
            hover_name='Asset',
            title='Risk-Return Attribution Comparison',
            labels={'Volatility': 'Annualized Volatility', 'Return': 'Annualized Return'},
            template='plotly_dark',
            size_max=60
        )

        st.plotly_chart(fig, use_container_width=True)
    except Exception as e:
        st.error(f"Error plotting risk-return attribution comparison: {e}")

def plot_efficient_frontier_comparison(returns_a, returns_b, num_portfolios=1000, rf=0.0):
    """
    Plot the Efficient Frontier comparison between two portfolios.
    
    Parameters:
    - returns_a (pd.DataFrame or pd.Series): Returns of the first portfolio.
    - returns_b (pd.DataFrame or pd.Series): Returns of the second portfolio.
    - num_portfolios (int): Number of portfolios to simulate.
    - rf (float): Risk-free rate.
    """
    try:
        num_assets_a = returns_a.shape[1] if isinstance(returns_a, pd.DataFrame) else 1
        num_assets_b = returns_b.shape[1] if isinstance(returns_b, pd.DataFrame) else 1

        def generate_portfolio(returns, rf):
            weights = np.random.random(num_assets_a)
            weights /= np.sum(weights)
            portfolio_return = np.dot(returns.mean(), weights) * 252
            portfolio_std_dev = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
            sharpe_ratio = (portfolio_return - rf) / portfolio_std_dev if portfolio_std_dev > 0 else 0
            return portfolio_std_dev, portfolio_return, sharpe_ratio

        # Generate portfolios for Portfolio A
        results_a = []
        for _ in range(num_portfolios):
            s, r, sr = generate_portfolio(returns_a, rf)
            results_a.append([s, r, sr])
        results_a = np.array(results_a).T

        # Generate portfolios for Portfolio B
        def generate_portfolio_b(returns, rf):
            weights = np.random.random(num_assets_b)
            weights /= np.sum(weights)
            portfolio_return = np.dot(returns.mean(), weights) * 252
            portfolio_std_dev = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
            sharpe_ratio = (portfolio_return - rf) / portfolio_std_dev if portfolio_std_dev > 0 else 0
            return portfolio_std_dev, portfolio_return, sharpe_ratio

        results_b = []
        for _ in range(num_portfolios):
            s, r, sr = generate_portfolio_b(returns_b, rf)
            results_b.append([s, r, sr])
        results_b = np.array(results_b).T

        # Create DataFrame
        ef_df_a = pd.DataFrame({
            'Std Dev': results_a[0],
            'Return': results_a[1],
            'Sharpe Ratio': results_a[2],
            'Portfolio': 'Portfolio A'
        })

        ef_df_b = pd.DataFrame({
            'Std Dev': results_b[0],
            'Return': results_b[1],
            'Sharpe Ratio': results_b[2],
            'Portfolio': 'Portfolio B'
        })

        ef_df = pd.concat([ef_df_a, ef_df_b], ignore_index=True)

        fig = px.scatter(
            ef_df,
            x='Std Dev',
            y='Return',
            color='Sharpe Ratio',
            facet_col='Portfolio',
            title='Efficient Frontier Comparison',
            labels={
                'Std Dev': 'Annualized Volatility (Std Dev)',
                'Return': 'Annualized Return',
                'Sharpe Ratio': 'Sharpe Ratio'
            },
            hover_data={
                'Sharpe Ratio': ':.2f',
                'Std Dev': ':.2f',
                'Return': ':.2f'
            },
            template='plotly_dark'
        )

        # Highlight maximum Sharpe ratio portfolio for each
        for portfolio in ['Portfolio A', 'Portfolio B']:
            subset = ef_df[ef_df['Portfolio'] == portfolio]
            if subset.empty:
                continue
            max_sharpe_idx = subset['Sharpe Ratio'].idxmax()
            if pd.isna(max_sharpe_idx):
                continue
            max_sharpe = subset.loc[max_sharpe_idx]
            fig.add_trace(go.Scatter(
                x=[max_sharpe['Std Dev']],
                y=[max_sharpe['Return']],
                mode='markers+text',
                marker=dict(color='red', size=14, symbol='star'),
                name=f'Max Sharpe {portfolio}',
                text=["Max Sharpe"],
                textposition="top center",
                hoverinfo='text',
                showlegend=False
            ))

        fig.update_layout(
            hovermode='closest',
            coloraxis_colorbar=dict(title='Sharpe Ratio'),
            template='plotly_dark'
        )
        st.plotly_chart(fig, use_container_width=True, config={
            'responsive': True,
            'scrollZoom': True,
            'displayModeBar': True
        })
    except Exception as e:
        st.error(f"Error plotting efficient frontier comparison: {e}")

SyntaxError: File "/www/wwwroot/portfolio_optimizer/plots.py", line 150 def plot_risk_return_attribution(returns, weights, label='Portfolio'): ^ SyntaxError: expected 'except' or 'finally' block
Traceback:
File "/www/server/pyporject_evn/portfolio_optimizer_venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
File "/www/server/pyporject_evn/portfolio_optimizer_venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
File "/www/wwwroot/portfolio_optimizer/pages/2_Run_Backtest.py", line 3, in <module>
    from components.run_backtest import run_backtest_ui
File "/www/wwwroot/portfolio_optimizer/components/run_backtest.py", line 22, in <module>
    from plots import (

fix it and give me full code

# components/run_backtest.py
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px

# Importing all necessary functions from utils, metrics, and plots
from utils import (
    download_data, 
    generate_recommendations, 
    backtest,  
    fetch_fama_french_factors, 
)

from metrics import (
    calculate_portfolio_metrics, 
    calculate_final_score, 
    calculate_risk_factor_attribution, 
    get_drawdown_details
)

from plots import (
    plot_growth_comparison, 
    plot_drawdown_comparison, 
    plot_cagr_over_time, 
    plot_allocation_pie, 
    plot_correlation_heatmap, 
    plot_risk_return_attribution, 
    plot_rolling_metrics
)

def run_backtest_ui():
    # Initialize portfolios in session state if not already initialized
    if 'portfolios' not in st.session_state:
        st.session_state.portfolios = []
    
    if st.session_state.portfolios:
        with st.form("compare_form"):
            st.subheader("🔍 Select Portfolios to Compare")
            comparison_type = st.selectbox(
                "Comparison Type",
                ["Portfolio vs Benchmark", "Portfolio vs Portfolio"]
            )
            portfolio_names = [p['name'] for p in st.session_state.portfolios]
            if comparison_type == "Portfolio vs Benchmark":
                portfolio1 = st.selectbox("Select Portfolio", portfolio_names)
                portfolio2 = None
            else:
                portfolio1 = st.selectbox("Select First Portfolio", portfolio_names, key="p1")
                portfolio2 = st.selectbox("Select Second Portfolio", portfolio_names, key="p2")
                if portfolio1 == portfolio2:
                    st.warning("Please select two different portfolios for comparison.")
            
            st.subheader("⚙️ Visualizations Settings")
            selected_time_frames = st.multiselect(
                "Select Time Frames for CAGR",
                options=['Weekly', 'Monthly', 'Quarterly', 'Annually'],
                default=['Annually'],
                key="selected_time_frames",
                help="Choose one or more time frames to view CAGR over different horizons."
            )
            selected_rolling_periods = st.multiselect(
                "Select Rolling Periods (Days)",
                options=[30, 90, 180, 252],
                default=[252],
                key="selected_rolling_periods",
                help="Choose one or more periods to view rolling metrics."
            )

            run = st.form_submit_button("Run Backtest")
        
        if run:
            with st.spinner("Running backtest..."):
                if comparison_type == "Portfolio vs Benchmark":
                    # Handle Portfolio vs Benchmark
                    try:
                        portfolio = next(p for p in st.session_state.portfolios if p['name'] == portfolio1)
                    except StopIteration:
                        st.error("Selected portfolio not found.")
                        return

                    price_data = download_data(
                        portfolio['selected'],
                        portfolio['start_date'],
                        portfolio['end_date']
                    )
                    if price_data.empty:
                        st.error("No price data available for the selected portfolio.")
                    else:
                        available_selected = [ticker for ticker in portfolio['selected'] if ticker in price_data.columns]
                        missing_selected = list(set(portfolio['selected']) - set(available_selected))
                        if missing_selected:
                            st.warning(f"Excluded tickers with no data: {', '.join(missing_selected)}")
                        if not available_selected:
                            st.error(
                                "No selected tickers have available data for the chosen date range. "
                                "Please ensure that the ticker symbols are correct and data is available for the specified period."
                            )
                        else:
                            allocations = [portfolio.get('allocations', [0.0]*len(portfolio.get('selected', [])))[i]
                                          for i, ticker in enumerate(portfolio.get('selected', [])) if ticker in available_selected]
                            if not np.isclose(sum(allocations), 100.0, atol=1e-4):
                                st.warning("Allocations do not sum to 100%. Adjusting allocations proportionally.")
                                allocations = [a / sum(allocations) * 100 for a in allocations]
                            weights = np.array(allocations) / 100
                            returns, cum_returns = backtest(weights, price_data[available_selected],
                                                            portfolio['rebalance_freq'], portfolio['broker_fee'], debug=False)
                            
                            # **Fix: Ensure returns have a DateTime index with matching length**
                            if not isinstance(returns.index, pd.DatetimeIndex):
                                try:
                                    returns.index = pd.to_datetime(price_data.index[:len(returns)])
                                    cum_returns.index = pd.to_datetime(price_data.index[:len(cum_returns)])
                                except Exception as e:
                                    st.error(f"Error converting returns index to DateTimeIndex: {e}")
                                    return
                            
                            if cum_returns.empty:
                                st.error("Cumulative returns are empty. Check the data and allocations.")
                            else:
                                benchmark_symbol = portfolio['benchmark_symbol']
                                benchmark_data = download_data([benchmark_symbol], portfolio['start_date'], portfolio['end_date'])

                                if benchmark_symbol not in benchmark_data.columns:
                                    st.error(f"The selected benchmark symbol '{benchmark_symbol}' does not have available data for the chosen period.")
                                    st.stop()
                                else:
                                    st.success(f"Benchmark '{benchmark_symbol}' data successfully downloaded and will be used for comparison.")
                                    if not benchmark_data.empty:
                                        benchmark_returns = benchmark_data[benchmark_symbol].pct_change().dropna()
                                        benchmark_returns = benchmark_returns.reindex(returns.index, method='ffill').dropna()
                                        common_index = returns.index.intersection(benchmark_returns.index)
                                        if common_index.empty:
                                            st.error("No overlapping dates between portfolio returns and benchmark returns after alignment.")
                                            st.stop()
                                        returns = returns.loc[common_index]
                                        cum_returns = cum_returns.loc[common_index]
                                        # Align the data using an inner join
                                        aligned_data = pd.merge(returns.to_frame('Portfolio'), benchmark_returns.to_frame('Benchmark'), left_index=True, right_index=True, how='inner')

                                        # Check that we have data after merging
                                        if aligned_data.empty:
                                            st.error("No overlapping dates between portfolio returns and benchmark returns after alignment.")
                                            st.stop()

                                        # Assign aligned returns
                                        returns = aligned_data['Portfolio'].rename("Portfolio_Returns")
                                        benchmark_returns = aligned_data['Benchmark']
                                        benchmark_cum_returns = (1 + benchmark_returns).cumprod()
                                        benchmark_metrics = calculate_portfolio_metrics(
                                            benchmark_returns, benchmark_cum_returns, portfolio['rf_rate'], None
                                        )
                                    else:
                                        benchmark_returns = None
                                        benchmark_cum_returns = pd.Series(dtype=float)
                                        benchmark_metrics = {
                                            'Start Balance': "N/A",
                                            'End Balance': "N/A",
                                            'Annualized Return (CAGR)': "N/A",
                                            'Best Year': "N/A",
                                            'Worst Year': "N/A",
                                            'Arithmetic Mean (Monthly)': "N/A",
                                            'Arithmetic Mean (Annualized)': "N/A",
                                            'Geometric Mean (Monthly)': "N/A",
                                            'Geometric Mean (Annualized)': "N/A",
                                            'Standard Deviation (Monthly)': "N/A",
                                            'Standard Deviation (Annualized)': "N/A",
                                            'Downside Deviation (Monthly)': "N/A",
                                            'Maximum Drawdown': "N/A",
                                            'Sharpe Ratio': "N/A",
                                            'Sortino Ratio': "N/A",
                                            'Gain/Loss Ratio': "N/A",
                                            'Skewness': "N/A",
                                            'Excess Kurtosis': "N/A",
                                            'Safe Withdrawal Rate': "N/A",
                                            'Perpetual Withdrawal Rate': "N/A",
                                            'Positive Periods': "N/A",
                                            'Benchmark Correlation': "N/A",
                                            'Beta': "N/A",
                                            'Alpha (annualized)': "N/A",
                                            'R2': "N/A",
                                            'Treynor Ratio': "N/A",
                                            'Calmar Ratio': "N/A",
                                            'Modigliani–Modigliani Measure': "N/A",
                                            'Information Ratio': "N/A",
                                            'Tracking Error': "N/A",
                                            'Active Return': "N/A",
                                            'Upside Capture Ratio': "N/A",
                                            'Downside Capture Ratio': "N/A"
                                        }

                                if not cum_returns.empty:
                                    portfolio_metrics = calculate_portfolio_metrics(
                                        returns, cum_returns, portfolio['rf_rate'], benchmark_returns
                                    )

                                    # Dashboard Header with Key Stats
                                    with st.container():
                                        st.markdown("### 🔑 Key Metrics")
                                        key_metrics = {
                                            'Annualized Return (CAGR)': portfolio_metrics.get('Annualized Return (CAGR)', "N/A"),
                                            'Sharpe Ratio': portfolio_metrics.get('Sharpe Ratio', "N/A"),
                                            'Maximum Drawdown': portfolio_metrics.get('Maximum Drawdown', "N/A")
                                        }
                                        cols = st.columns(len(key_metrics))
                                        for col, (metric, value) in zip(cols, key_metrics.items()):
                                            with col:
                                                st.metric(label=metric, value=value)

                                    # Create Tabs for Organized Sections
                                    tabs = st.tabs(["Overview", "Performance Statistics", "Advanced Metrics", "Drawdowns", "Visualizations"])

                                    with tabs[0]:
                                        st.header("📈 Portfolio Performance Overview")
                                        st.write(f"**Portfolio Name:** {portfolio['name']}")
                                        st.write(f"**Start Date:** {portfolio['start_date'].strftime('%Y-%m-%d')}")
                                        st.write(f"**End Date:** {portfolio['end_date'].strftime('%Y-%m-%d')}")
                                        st.write(f"**Benchmark:** {portfolio['benchmark_symbol']}")

                                    with tabs[1]:
                                        st.header("📊 Performance Statistics")
                                        
                                        with st.container():
                                            st.subheader("🛠️ Basic Metrics")
                                            performance_data_stats = {
                                                'Metric': [
                                                    'Start Balance',
                                                    'End Balance',
                                                    'Annualized Return (CAGR)',
                                                    'Standard Deviation (Annualized)',
                                                    'Best Year',
                                                    'Worst Year',
                                                    'Maximum Drawdown',
                                                    'Sharpe Ratio',
                                                    'Sortino Ratio',
                                                    'Benchmark Correlation'
                                                ],
                                                'Portfolio': [
                                                    portfolio_metrics['Start Balance'],
                                                    portfolio_metrics['End Balance'],
                                                    portfolio_metrics['Annualized Return (CAGR)'],
                                                    portfolio_metrics['Standard Deviation (Annualized)'],
                                                    portfolio_metrics['Best Year'],
                                                    portfolio_metrics['Worst Year'],
                                                    portfolio_metrics['Maximum Drawdown'],
                                                    portfolio_metrics['Sharpe Ratio'],
                                                    portfolio_metrics['Sortino Ratio'],
                                                    portfolio_metrics['Benchmark Correlation']
                                                ],
                                                'Benchmark': [
                                                    benchmark_metrics['Start Balance'],
                                                    benchmark_metrics['End Balance'],
                                                    benchmark_metrics['Annualized Return (CAGR)'],
                                                    benchmark_metrics['Standard Deviation (Annualized)'],
                                                    benchmark_metrics['Best Year'],
                                                    benchmark_metrics['Worst Year'],
                                                    benchmark_metrics['Maximum Drawdown'],
                                                    benchmark_metrics['Sharpe Ratio'],
                                                    benchmark_metrics['Sortino Ratio'],
                                                    benchmark_metrics['Benchmark Correlation']
                                                ]
                                            }
                                            performance_df_stats = pd.DataFrame(performance_data_stats).set_index('Metric')
                                            st.table(performance_df_stats)

                                    with tabs[2]:
                                        st.header("📋 Detail Comparisons")
                                        with st.container():
                                            st.subheader("🧮 Advanced Metrics")
                                            performance_data_advanced = {
                                                'Metric': list(portfolio_metrics.keys()),
                                                'Portfolio': list(portfolio_metrics.values()),
                                                'Benchmark': list(benchmark_metrics.values())
                                            }
                                            performance_df_advanced = pd.DataFrame(performance_data_advanced).set_index('Metric')

                                            # Replace NaN with empty strings for better visualization
                                            performance_df_advanced = performance_df_advanced.replace(np.nan, "")
                                            # Drop rows where both Portfolio and Benchmark values are NaN
                                            performance_df_advanced_clean = performance_df_advanced.dropna(how='all')

                                            st.table(performance_df_advanced_clean)
                                            st.markdown("---")
                                            st.write("**Note:** Some metrics in the benchmark column are empty because they are portfolio-specific measurements that can't be calculated for the benchmark alone. These include:\n\n"
                             "1. Alpha (annualized)\n"
                             "2. Information Ratio\n"
                             "3. Tracking Error\n"
                             "4. Active Return\n"
                             "5. Upside/Downside Capture Ratios\n\n"
                             "These metrics specifically measure how a portfolio performs relative to its benchmark, so they only make sense when calculated for the portfolio itself. For example, tracking error measures how closely a portfolio follows its benchmark, which isn't applicable to the benchmark itself. Similarly, alpha measures excess return relative to the benchmark, which wouldn't be meaningful to calculate for the benchmark itself.")
                                        st.subheader("🔍 Risk Factor Attribution Analysis")
                                        
                                        # Ensure 'returns' is defined and is a pd.Series
                                        if 'returns' not in locals():
                                            st.error("Portfolio returns data ('returns') is not defined.")
                                        else:
                                            # Define date range based on portfolio returns index
                                            start_date = returns.index.min().strftime('%Y-%m-%d')
                                            end_date = returns.index.max().strftime('%Y-%m-%d')
                                            # Fetch actual factor data
                                            factors = fetch_fama_french_factors(start_date, end_date)
                                            # Ensure factors are fetched
                                            if factors.empty:
                                                st.write("Failed to retrieve factor data.")       
                                            else:
                                                if isinstance(returns.index, pd.DatetimeIndex):
                                                    if returns.index.freq is None:
                                                        inferred_freq = pd.infer_freq(returns.index)
                                                        if inferred_freq is None:
                                                            inferred_freq = 'D'  # Set default frequency to daily
                                                    else:
                                                        inferred_freq = returns.index.freq
                                                    
                                                    try:
                                                        factors = factors.asfreq(inferred_freq, method='ffill')
                                                    except TypeError as e:
                                                        st.error(f"Error setting frequency for factors: {e}")
                                                        st.stop()
                                                else:
                                                    st.error("Returns index is not a DatetimeIndex.")
                                                    st.stop()
                                                # Calculate Attribution with aligned dates
                                                attribution, error = calculate_risk_factor_attribution(returns, factors)
                                                
                                                if error:
                                                    st.error(error)
                                                    st.write("Risk factor attribution analysis is unavailable.")
                                                elif attribution.empty:
                                                    st.write("Risk factor attribution analysis is unavailable.")
                                                else:
                                                    st.write("Risk Factor Attribution:")
                                                    st.table(attribution)
                                                    # Optional: Visualize Attribution
                                                    fig_attribution = px.bar(
                                                        attribution,
                                                        x='Factor',
                                                        y='Contribution (%)',
                                                        title='Risk Factor Attribution',
                                                        labels={'Contribution (%)': 'Contribution (%)'},
                                                        template='plotly_dark'
                                                    )
                                                    st.plotly_chart(fig_attribution, use_container_width=True)

                                    with tabs[3]:
                                        st.header("📉 Detailed Drawdowns")
                                        st.markdown("### 📈 Drawdowns for Portfolio")
                                        portfolio_drawdowns = get_drawdown_details(cum_returns)
                                        if portfolio_drawdowns:
                                            portfolio_drawdowns_df = pd.DataFrame(portfolio_drawdowns)
                                            st.table(portfolio_drawdowns_df)
                                        else:
                                            st.write("No drawdowns detected for the portfolio.")

                                        if benchmark_returns is not None and not benchmark_returns.empty:
                                            st.markdown("### 📈 Drawdowns for Benchmark")
                                            benchmark_drawdowns = get_drawdown_details(benchmark_cum_returns)
                                            if benchmark_drawdowns:
                                                benchmark_drawdowns_df = pd.DataFrame(benchmark_drawdowns)
                                                st.table(benchmark_drawdowns_df)
                                            else:
                                                st.write("No drawdowns detected for the benchmark.")
                                        else:
                                            st.write("Benchmark data not available for drawdown analysis.")

                                    with tabs[4]:
                                        st.header("📊 Visualizations")
                                        if not cum_returns.empty and not benchmark_cum_returns.empty:
                                            plot_growth_comparison(cum_returns, benchmark_cum_returns, labels=["Portfolio", "Benchmark"])
                                        else:
                                            st.warning("Insufficient data to display Growth Comparison. Ensure both portfolio and benchmark have data.")
                                        
                                        portfolio_drawdown_series = (cum_returns / cum_returns.expanding().max() - 1) * 100
                                        benchmark_drawdown_series = (benchmark_cum_returns / benchmark_cum_returns.expanding().max() - 1) * 100
                                        plot_drawdown_comparison(portfolio_drawdown_series, benchmark_drawdown_series, labels=["Portfolio", "Benchmark"])

                                        st.subheader("📈 Compound Annual Growth Rate (CAGR) Over Time")
                                        if selected_time_frames:
                                            plot_cagr_over_time(cum_returns, time_frames=selected_time_frames, label="Portfolio")
                                        else:
                                            st.warning("Please select at least one time frame for CAGR.")

                                        # Box Plot for Returns Distribution
                                        st.subheader("📦 Returns Distribution Box Plot")
                                        if isinstance(returns, pd.DataFrame):
                                            melted_returns = returns.reset_index().melt(id_vars='Date', var_name='Asset', value_name='Return')
                                            fig_box_plot = px.box(
                                                melted_returns,
                                                x='Asset',
                                                y='Return',
                                                title='Returns Distribution Box Plot',
                                                labels={'Return': 'Returns', 'Asset': 'Asset'},
                                                points='all',
                                                hover_data=['Return'],
                                                template='plotly_dark',
                                                color='Asset',
                                                color_discrete_sequence=px.colors.qualitative.Set3
                                            )
                                            st.plotly_chart(fig_box_plot, use_container_width=True)
                                            st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for each asset in the portfolio. The boxes represent the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                                        elif isinstance(returns, pd.Series):
                                            fig_box_plot = px.box(
                                                returns.to_frame(name='Return'),
                                                y='Return',
                                                title='Returns Distribution Box Plot',
                                                labels={'Return': 'Returns'},
                                                points='all',
                                                hover_data=['Return'],
                                                template='plotly_dark'
                                            )
                                            st.plotly_chart(fig_box_plot, use_container_width=True)
                                            st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for the portfolio. The box represents the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                                        else:
                                            st.warning("Returns data is neither a DataFrame nor a Series.")

                                        # Heatmap of Correlations Between Assets
                                        st.subheader("🔥 Correlation Heatmap of Assets")
                                        plot_correlation_heatmap(returns)

                                        # Add Risk-Return Attribution Analysis
                                        st.subheader("🔍 Risk-Return Attribution Analysis")
                                        plot_risk_return_attribution(returns, weights, label='Portfolio')

                                        # Cumulative Returns Heatmap
                                        st.subheader("🔥 Cumulative Returns Heatmap")
                                        try:
                                            cum_returns_normalized = cum_returns / cum_returns.max()
                                            fig_cum_heatmap = px.imshow(
                                                cum_returns_normalized.to_frame().T,
                                                labels=dict(x="Date", y="Portfolio", color="Normalized Cumulative Return"),
                                                title="Cumulative Returns Heatmap",
                                                aspect="auto",
                                                color_continuous_scale='Viridis',
                                                template='plotly_dark'
                                            )
                                            st.plotly_chart(fig_cum_heatmap, use_container_width=True)
                                            st.markdown("**Interpretation:** The heatmap visualizes the normalized cumulative returns over time, allowing for an intuitive comparison of portfolio performance across different periods.")
                                        except Exception as e:
                                            st.error(f"Error plotting cumulative returns heatmap: {e}")

                                        # Portfolio Allocation Pie Chart
                                        st.subheader("🥧 Portfolio Allocation Pie Chart")
                                        plot_allocation_pie(weights, available_selected, title="Portfolio Allocation", hover_info="percent+name")

                                        # Rolling Metrics Chart with User-Defined Periods
                                        st.subheader("📉 Rolling Metrics")
                                        if selected_rolling_periods:
                                            plot_rolling_metrics(returns, windows=selected_rolling_periods, label='Portfolio')
                                        else:
                                            st.warning("Please select at least one rolling period to display metrics.")

                                        # Recommendations
                                        st.markdown("### 💡 Recommendations")
                                        recommendations = generate_recommendations(
                                            allocations=[alloc for alloc in portfolio['allocations'] if alloc > 0],
                                            available_selected=[ticker for ticker in available_selected if portfolio['allocations'][available_selected.index(ticker)] > 0]
                                        )
                                        
                                        if recommendations:
                                            for rec in recommendations:
                                                st.write(rec)
                                        else:
                                            st.success("Your portfolio allocations are well-balanced!")
                                            
                                        # Final Score
                                        st.markdown("### 📝 Final Score")
                                        portfolio_score = calculate_final_score(portfolio_metrics, benchmark_metrics)
                                        benchmark_score = calculate_final_score(benchmark_metrics, portfolio_metrics)
                                        st.write(f"**Portfolio:** {portfolio_score:.2f} / 100")
                                        st.write(f"**Benchmark:** {benchmark_score:.2f} / 100")

                                        # Store results
                                        st.session_state.backtest_results = {
                                            'returns': returns,
                                            'cum_returns': cum_returns,
                                            'weights_a': weights,
                                            'price_data': price_data[available_selected],
                                            'metrics': portfolio_metrics,
                                            'benchmark_metrics': benchmark_metrics,
                                            'performance_df_stats': performance_df_stats,
                                            'performance_df_advanced': performance_df_advanced_clean,  # Corrected to use the cleaned DataFrame
                                            'benchmark_cum_returns': benchmark_cum_returns
                                        }
                elif comparison_type == "Portfolio vs Portfolio" and portfolio1 and portfolio2 and portfolio1 != portfolio2:
                    # Handle Portfolio vs Portfolio
                    try:
                        portfolio_a = next(p for p in st.session_state.portfolios if p['name'] == portfolio1)
                        portfolio_b = next(p for p in st.session_state.portfolios if p['name'] == portfolio2)
                    except StopIteration:
                        st.error("One or both selected portfolios were not found.")
                        return

                    # Backtest Portfolio A
                    price_data_a = download_data(
                        portfolio_a['selected'],
                        portfolio_a['start_date'],
                        portfolio_a['end_date']
                    )
                    # Backtest Portfolio B
                    price_data_b = download_data(
                        portfolio_b['selected'],
                        portfolio_b['start_date'],
                        portfolio_b['end_date']
                    )

                    if price_data_a.empty or price_data_b.empty:
                        st.error("No price data available for one or both selected portfolios.")
                    else:
                        # Process Portfolio A
                        available_a = [ticker for ticker in portfolio_a['selected'] if ticker in price_data_a.columns]
                        missing_a = list(set(portfolio_a['selected']) - set(available_a))
                        if missing_a:
                            st.warning(f"Excluded tickers from {portfolio_a['name']} with no data: {', '.join(missing_a)}")
                        if not available_a:
                            st.error(f"No selected tickers have available data for the portfolio '{portfolio_a['name']}'.")
                            return
                        allocations_a = [portfolio_a.get('allocations', [0.0]*len(portfolio_a.get('selected', [])))[i]
                                         for i, ticker in enumerate(portfolio_a.get('selected', [])) if ticker in available_a]
                        if not np.isclose(sum(allocations_a), 100.0, atol=1e-4):
                            st.warning(f"Allocations for {portfolio_a['name']} do not sum to 100%. Adjusting allocations proportionally.")
                            allocations_a = [a / sum(allocations_a) * 100 for a in allocations_a]
                        weights_a = np.array(allocations_a) / 100
                        returns_a, cum_returns_a = backtest(weights_a, price_data_a[available_a],
                                                           portfolio_a['rebalance_freq'], portfolio_a['broker_fee'], debug=False)
                        
                        # Process Portfolio B
                        available_b = [ticker for ticker in portfolio_b['selected'] if ticker in price_data_b.columns]
                        missing_b = list(set(portfolio_b['selected']) - set(available_b))
                        if missing_b:
                            st.warning(f"Excluded tickers from {portfolio_b['name']} with no data: {', '.join(missing_b)}")
                        if not available_b:
                            st.error(f"No selected tickers have available data for the portfolio '{portfolio_b['name']}'.")
                            return
                        allocations_b = [portfolio_b.get('allocations', [0.0]*len(portfolio_b.get('selected', [])))[i]
                                         for i, ticker in enumerate(portfolio_b.get('selected', [])) if ticker in available_b]
                        if not np.isclose(sum(allocations_b), 100.0, atol=1e-4):
                            st.warning(f"Allocations for {portfolio_b['name']} do not sum to 100%. Adjusting allocations proportionally.")
                            allocations_b = [a / sum(allocations_b) * 100 for a in allocations_b]
                        weights_b = np.array(allocations_b) / 100
                        returns_b, cum_returns_b = backtest(weights_b, price_data_b[available_b],
                                                           portfolio_b['rebalance_freq'], portfolio_b['broker_fee'], debug=False)
                        
                        # **Fix: Ensure returns have a DateTime index with matching length**
                        try:
                            returns_a.index = pd.to_datetime(price_data_a.index[:len(returns_a)])
                            cum_returns_a.index = pd.to_datetime(price_data_a.index[:len(cum_returns_a)])
                            returns_b.index = pd.to_datetime(price_data_b.index[:len(returns_b)])
                            cum_returns_b.index = pd.to_datetime(price_data_b.index[:len(cum_returns_b)])
                        except Exception as e:
                            st.error(f"Error converting returns index to DateTimeIndex: {e}")
                            return
                        
                        if cum_returns_a.empty or cum_returns_b.empty:
                            st.error("Cumulative returns are empty for one or both portfolios. Check the data and allocations.")
                            return
                        
                        # Align the data for comparison
                        common_dates = cum_returns_a.index.intersection(cum_returns_b.index)
                        if common_dates.empty:
                            st.error("No overlapping dates between the two portfolios after alignment.")
                            return
                        cum_returns_a = cum_returns_a.loc[common_dates]
                        cum_returns_b = cum_returns_b.loc[common_dates]
                        returns_a = returns_a.loc[common_dates]
                        returns_b = returns_b.loc[common_dates]
                        
                        # Calculate Metrics
                        metrics_a = calculate_portfolio_metrics(
                            returns_a, cum_returns_a, portfolio_a['rf_rate'], None
                        )
                        metrics_b = calculate_portfolio_metrics(
                            returns_b, cum_returns_b, portfolio_b['rf_rate'], None
                        )
                        
                        # Dashboard Header with Key Stats
                        with st.container():
                            st.markdown("### 🔑 Key Metrics")
                            key_metrics = {
                                'Annualized Return (CAGR)': ('CAGR', metrics_a.get('Annualized Return (CAGR)', "N/A"), metrics_b.get('Annualized Return (CAGR)', "N/A")),
                                'Sharpe Ratio': ('Sharpe Ratio', metrics_a.get('Sharpe Ratio', "N/A"), metrics_b.get('Sharpe Ratio', "N/A")),
                                'Maximum Drawdown': ('Maximum Drawdown', metrics_a.get('Maximum Drawdown', "N/A"), metrics_b.get('Maximum Drawdown', "N/A"))
                            }
                            columns = st.columns(len(key_metrics))
                            for (metric, (label, value_a, value_b)) in key_metrics.items():
                                with columns[list(key_metrics.keys()).index(metric)]:
                                    st.metric(label=f"{label} ({portfolio_a['name']})", value=value_a)
                                    st.metric(label=f"{label} ({portfolio_b['name']})", value=value_b)

                        # Create Tabs for Organized Sections
                        tabs = st.tabs(["Overview", "Performance Statistics", "Advanced Metrics", "Drawdowns", "Visualizations"])

                        with tabs[0]:
                            st.header("📈 Portfolios Performance Overview")
                            # Determine the aligned start and end dates
                            aligned_start_date = common_dates.min().strftime('%Y-%m-%d')
                            aligned_end_date = common_dates.max().strftime('%Y-%m-%d')
                            
                            st.write(f"**Aligned Date Range:** {aligned_start_date} to {aligned_end_date}")
                            st.write(f"**Portfolio A:** {portfolio_a['name']}")
                            st.write(f"**Original Start Date:** {portfolio_a['start_date'].strftime('%Y-%m-%d')}")
                            st.write(f"**Portfolio B:** {portfolio_b['name']}")
                            st.write(f"**Original Start Date:** {portfolio_b['start_date'].strftime('%Y-%m-%d')}")

                        with tabs[1]:
                            st.header("📊 Performance Statistics")
                            
                            with st.container():
                                st.subheader("🛠️ Basic Metrics")
                                performance_data_stats = {
                                    'Metric': [
                                        'Start Balance',
                                        'End Balance',
                                        'Annualized Return (CAGR)',
                                        'Standard Deviation (Annualized)',
                                        'Best Year',
                                        'Worst Year',
                                        'Maximum Drawdown',
                                        'Sharpe Ratio',
                                        'Sortino Ratio',
                                        'Benchmark Correlation'
                                    ],
                                    f'{portfolio_a["name"]}': [
                                        metrics_a['Start Balance'],
                                        metrics_a['End Balance'],
                                        metrics_a['Annualized Return (CAGR)'],
                                        metrics_a['Standard Deviation (Annualized)'],
                                        metrics_a['Best Year'],
                                        metrics_a['Worst Year'],
                                        metrics_a['Maximum Drawdown'],
                                        metrics_a['Sharpe Ratio'],
                                        metrics_a['Sortino Ratio'],
                                        metrics_a['Benchmark Correlation']
                                    ],
                                    f'{portfolio_b["name"]}': [
                                        metrics_b['Start Balance'],
                                        metrics_b['End Balance'],
                                        metrics_b['Annualized Return (CAGR)'],
                                        metrics_b['Standard Deviation (Annualized)'],
                                        metrics_b['Best Year'],
                                        metrics_b['Worst Year'],
                                        metrics_b['Maximum Drawdown'],
                                        metrics_b['Sharpe Ratio'],
                                        metrics_b['Sortino Ratio'],
                                        metrics_b['Benchmark Correlation']
                                    ]
                                }
                                performance_df_stats = pd.DataFrame(performance_data_stats).set_index('Metric')
                                st.table(performance_df_stats)

                        with tabs[2]:
                            st.header("📋 Detail Comparisons")
                            with st.container():
                                st.subheader("🧮 Advanced Metrics")
                                performance_data_advanced = {
                                    'Metric': list(metrics_a.keys()),
                                    f'{portfolio_a["name"]}': list(metrics_a.values()),
                                    f'{portfolio_b["name"]}': list(metrics_b.values())
                                }
                                performance_df_advanced = pd.DataFrame(performance_data_advanced).set_index('Metric')

                                # Replace NaN with empty strings for better visualization
                                performance_df_advanced = performance_df_advanced.replace(np.nan, "")
                                # Drop rows where both Portfolio A and Portfolio B values are NaN
                                performance_df_advanced_clean = performance_df_advanced.dropna(how='all', subset=[f'{portfolio_a["name"]}', f'{portfolio_b["name"]}'])

                                st.table(performance_df_advanced_clean)
                                st.markdown("---")
                                st.write("**Note:** Some metrics may be specific to certain portfolios or may not be directly comparable. Ensure to interpret the metrics in the context of each portfolio's strategy and objectives.")
                            st.subheader("🔍 Risk Factor Attribution Analysis")
                            
                            # Define date range based on the overlapping index
                            start_date = common_dates.min().strftime('%Y-%m-%d')
                            end_date = common_dates.max().strftime('%Y-%m-%d')
                            # Fetch actual factor data
                            factors = fetch_fama_french_factors(start_date, end_date)
                            # Ensure factors are fetched
                            if factors.empty:
                                st.write("Failed to retrieve factor data.")       
                            else:
                                # Align factors frequency with portfolios
                                if isinstance(returns_a.index, pd.DatetimeIndex):
                                    if returns_a.index.freq is None:
                                        inferred_freq = pd.infer_freq(returns_a.index)
                                        if inferred_freq is None:
                                            inferred_freq = 'D'  # Set default frequency to daily
                                    else:
                                        inferred_freq = returns_a.index.freq
                                    
                                    try:
                                        factors = factors.asfreq(inferred_freq, method='ffill')
                                    except TypeError as e:
                                        st.error(f"Error setting frequency for factors: {e}")
                                        st.stop()
                                else:
                                    st.error("Returns index is not a DatetimeIndex.")
                                    st.stop()
                                
                                # Calculate Attribution for Portfolio A
                                attribution_a, error_a = calculate_risk_factor_attribution(returns_a, factors)
                                
                                # Calculate Attribution for Portfolio B
                                attribution_b, error_b = calculate_risk_factor_attribution(returns_b, factors)
                                
                                if error_a or error_b:
                                    st.error("Risk factor attribution analysis is unavailable for one or both portfolios.")
                                else:
                                    if attribution_a.empty or attribution_b.empty:
                                        st.write("Risk factor attribution analysis is unavailable for one or both portfolios.")
                                    else:
                                        st.write(f"**Risk Factor Attribution for {portfolio_a['name']}:**")
                                        st.table(attribution_a)
                                        fig_attribution_a = px.bar(
                                            attribution_a,
                                            x='Factor',
                                            y='Contribution (%)',
                                            title=f'Risk Factor Attribution for {portfolio_a["name"]}',
                                            labels={'Contribution (%)': 'Contribution (%)'},
                                            template='plotly_dark'
                                        )
                                        st.plotly_chart(fig_attribution_a, use_container_width=True)

                                        st.write(f"**Risk Factor Attribution for {portfolio_b['name']}:**")
                                        st.table(attribution_b)
                                        fig_attribution_b = px.bar(
                                            attribution_b,
                                            x='Factor',
                                            y='Contribution (%)',
                                            title=f'Risk Factor Attribution for {portfolio_b["name"]}',
                                            labels={'Contribution (%)': 'Contribution (%)'},
                                            template='plotly_dark'
                                        )
                                        st.plotly_chart(fig_attribution_b, use_container_width=True)

                                        # Combined Bar Chart for Comparison
                                        combined_attribution = pd.concat([attribution_a.set_index('Factor')['Contribution (%)'],
                                                                        attribution_b.set_index('Factor')['Contribution (%)']], axis=1)
                                        combined_attribution.columns = [portfolio_a['name'], portfolio_b['name']]
                                        combined_attribution = combined_attribution.fillna(0)

                                        st.markdown(f"### 📊 Risk Factor Attribution Comparison")
                                        fig_combined_attribution = px.bar(
                                            combined_attribution,
                                            x=combined_attribution.index,
                                            y=combined_attribution.columns,
                                            title='Risk Factor Attribution Comparison',
                                            labels={'value': 'Contribution (%)', 'Factor': 'Risk Factor'},
                                            template='plotly_dark',
                                            barmode='group'
                                        )
                                        st.plotly_chart(fig_combined_attribution, use_container_width=True)

                        with tabs[3]:
                            st.header("📉 Detailed Drawdowns")
                            st.markdown("### 📈 Drawdowns for Portfolio A")
                            portfolio_drawdowns = get_drawdown_details(cum_returns_a)
                            if portfolio_drawdowns:
                                portfolio_drawdowns_df = pd.DataFrame(portfolio_drawdowns)
                                st.table(portfolio_drawdowns_df)
                            else:
                                st.write("No drawdowns detected for the portfolio A.")

                            st.markdown("### 📈 Drawdowns for Portfolio B")
                            portfolio_b_drawdowns = get_drawdown_details(cum_returns_b)
                            if portfolio_b_drawdowns:
                                portfolio_b_drawdowns_df = pd.DataFrame(portfolio_b_drawdowns)
                                st.table(portfolio_b_drawdowns_df)
                            else:
                                st.write("No drawdowns detected for the portfolio B.")

                        with tabs[4]:
                            st.header("📊 Visualizations")
                            if not cum_returns_a.empty and not cum_returns_b.empty:
                                plot_growth_comparison(cum_returns_a, cum_returns_b, labels=[portfolio_a['name'], portfolio_b['name']])
                            else:
                                st.warning("Insufficient data to display Growth Comparison. Ensure both portfolios have data.")
                        
                            portfolio_a_drawdown_series = (cum_returns_a / cum_returns_a.expanding().max() - 1) * 100
                            portfolio_b_drawdown_series = (cum_returns_b / cum_returns_b.expanding().max() - 1) * 100
                            plot_drawdown_comparison(portfolio_a_drawdown_series, portfolio_b_drawdown_series, labels=[portfolio_a['name'], portfolio_b['name']])
                        
                            st.subheader("📈 Compound Annual Growth Rate (CAGR) Over Time")
                            if selected_time_frames:
                                plot_cagr_comparison(cum_returns_a, cum_returns_b, time_frames=selected_time_frames)
                            else:
                                st.warning("Please select at least one time frame for CAGR.")

                            # Box Plot for Returns Distribution
                            st.subheader("📦 Returns Distribution Box Plot")
                            # For portfolio A
                            if isinstance(returns_a, pd.DataFrame):
                                melted_returns_a = returns_a.reset_index().melt(id_vars='Date', var_name='Asset', value_name='Return')
                                fig_box_plot_a = px.box(
                                    melted_returns_a,
                                    x='Asset',
                                    y='Return',
                                    title='Returns Distribution Box Plot - Portfolio A',
                                    labels={'Return': 'Returns', 'Asset': 'Asset'},
                                    points='all',
                                    hover_data=['Return'],
                                    template='plotly_dark',
                                    color='Asset',
                                    color_discrete_sequence=px.colors.qualitative.Set3
                                )
                                st.plotly_chart(fig_box_plot_a, use_container_width=True)
                                st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for each asset in Portfolio A. The boxes represent the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                            elif isinstance(returns_a, pd.Series):
                                fig_box_plot_a = px.box(
                                    returns_a.to_frame(name='Return'),
                                    y='Return',
                                    title='Returns Distribution Box Plot - Portfolio A',
                                    labels={'Return': 'Returns'},
                                    points='all',
                                    hover_data=['Return'],
                                    template='plotly_dark'
                                )
                                st.plotly_chart(fig_box_plot_a, use_container_width=True)
                                st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for Portfolio A. The box represents the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                            else:
                                st.warning("Returns data for Portfolio A is neither a DataFrame nor a Series.")

                            # For portfolio B
                            if isinstance(returns_b, pd.DataFrame):
                                melted_returns_b = returns_b.reset_index().melt(id_vars='Date', var_name='Asset', value_name='Return')
                                fig_box_plot_b = px.box(
                                    melted_returns_b,
                                    x='Asset',
                                    y='Return',
                                    title='Returns Distribution Box Plot - Portfolio B',
                                    labels={'Return': 'Returns', 'Asset': 'Asset'},
                                    points='all',
                                    hover_data=['Return'],
                                    template='plotly_dark',
                                    color='Asset',
                                    color_discrete_sequence=px.colors.qualitative.Set3
                                )
                                st.plotly_chart(fig_box_plot_b, use_container_width=True)
                                st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for each asset in Portfolio B. The boxes represent the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                            elif isinstance(returns_b, pd.Series):
                                fig_box_plot_b = px.box(
                                    returns_b.to_frame(name='Return'),
                                    y='Return',
                                    title='Returns Distribution Box Plot - Portfolio B',
                                    labels={'Return': 'Returns'},
                                    points='all',
                                    hover_data=['Return'],
                                    template='plotly_dark'
                                )
                                st.plotly_chart(fig_box_plot_b, use_container_width=True)
                                st.markdown("**Interpretation:** The box plot visualizes the distribution of returns for Portfolio B. The box represents the interquartile range (IQR), the line inside the box indicates the median, and the whiskers show the range of the data. Outliers are displayed as individual points.")
                            else:
                                st.warning("ReturnCompound Annual Growth Rate (CAGR) Over Times data for Portfolio B is neither a DataFrame nor a Series.")

                            # Correlation Heatmap Comparison
                            st.subheader("🔥 Correlation Heatmap Comparison")
                            plot_correlation_heatmap_comparison(returns_a, returns_b)
                            
                            # Efficient Frontier Comparison
                            st.subheader("📈 Efficient Frontier Comparison")
                            plot_efficient_frontier_comparison(returns_a, returns_b, num_portfolios=1000, rf=portfolio_a['rf_rate'])

                            # Risk-Return Attribution Comparison
                            st.subheader("🔍 Risk-Return Attribution Comparison")
                            plot_risk_return_attribution_comparison(returns_a, weights_a, returns_b, weights_b, label_a=portfolio_a['name'], label_b=portfolio_b['name'])

                            # Cumulative Returns Heatmap
                            st.subheader("🔥 Cumulative Returns Heatmap")
                            try:
                                cum_returns_normalized_a = cum_returns_a / cum_returns_a.max()
                                cum_returns_normalized_b = cum_returns_b / cum_returns_b.max()
                                cum_heatmap = pd.concat([cum_returns_normalized_a, cum_returns_normalized_b], axis=1)
                                cum_heatmap.columns = [f"{portfolio_a['name']} Normalized", f"{portfolio_b['name']} Normalized"]

                                fig_cum_heatmap = px.imshow(
                                    cum_heatmap.T,
                                    labels=dict(x="Date", y="Portfolio", color="Normalized Cumulative Return"),
                                    title="Cumulative Returns Heatmap",
                                    aspect="auto",
                                    color_continuous_scale='Viridis',
                                    template='plotly_dark'
                                )
                                st.plotly_chart(fig_cum_heatmap, use_container_width=True)
                                st.markdown("**Interpretation:** The heatmap visualizes the normalized cumulative returns over time for both portfolios, allowing for an intuitive comparison of portfolio performance across different periods.")
                            except Exception as e:
                                st.error(f"Error plotting cumulative returns heatmap: {e}")

                            # Allocation Comparison
                            st.subheader("🥧 Allocation Comparison")
                            plot_allocation_comparison(weights_a, available_a, weights_b, available_b, title="Portfolio Allocation Comparison")

                            # Rolling Metrics Comparison
                            st.subheader("📉 Rolling Metrics Comparison")
                            plot_rolling_metrics_comparison(returns_a, weights_a, returns_b, weights_b, windows=selected_rolling_periods)

                            # Recommendations
                            st.markdown("### 💡 Recommendations")
                            recommendations_a = generate_recommendations(
                                allocations=[alloc for alloc in portfolio_a['allocations'] if alloc > 0],
                                available_selected=[ticker for ticker in available_a if portfolio_a['allocations'][available_a.index(ticker)] > 0]
                            )
                            recommendations_b = generate_recommendations(
                                allocations=[alloc for alloc in portfolio_b['allocations'] if alloc > 0],
                                available_selected=[ticker for ticker in available_b if portfolio_b['allocations'][available_b.index(ticker)] > 0]
                            )
                            
                            if recommendations_a:
                                st.write(f"**{portfolio_a['name']} Recommendations:**")
                                for rec in recommendations_a:
                                    st.write(rec)
                            else:
                                st.success(f"{portfolio_a['name']} allocations are well-balanced!")
                            
                            if recommendations_b:
                                st.write(f"**{portfolio_b['name']} Recommendations:**")
                                for rec in recommendations_b:
                                    st.write(rec)
                            else:
                                st.success(f"{portfolio_b['name']} allocations are well-balanced!")
                                
                            # Final Score
                            st.markdown("### 📝 Final Score")
                            portfolio_score_a = calculate_final_score(metrics_a, metrics_b)
                            portfolio_score_b = calculate_final_score(metrics_b, metrics_a)
                            st.write(f"**{portfolio_a['name']}:** {portfolio_score_a:.2f} / 100")
                            st.write(f"**{portfolio_b['name']}:** {portfolio_score_b:.2f} / 100")

                            # Store results
                            st.session_state.backtest_results = {
                                'returns_a': returns_a,
                                'cum_returns_a': cum_returns_a,
                                'weights_a': weights_a,
                                'price_data_a': price_data_a[available_a],
                                'metrics_a': metrics_a,
                                'returns_b': returns_b,
                                'cum_returns_b': cum_returns_b,
                                'weights_b': weights_b,
                                'price_data_b': price_data_b[available_b],
                                'metrics_b': metrics_b,
                                'performance_df_stats_a': performance_df_stats,
                                'performance_df_advanced_a': performance_df_advanced_clean,
                                'performance_df_stats_b': performance_df_stats,
                                'performance_df_advanced_b': performance_df_advanced_clean
                            }
                st.success("Backtest completed successfully!")
    else:
        st.warning("Please add at least one portfolio to run backtest.")