pvyas96's picture
Update Main_Page.py
ec6a832 verified
"""
NSE Portfolio Optimizer Pro - Main Entry Page
==============================================
"""
import streamlit as st
from datetime import datetime
import pandas as pd
# Import utilities for live data AND Theme
try:
from src.utils import get_nifty_data, render_header, get_theme_colors
except ImportError:
get_nifty_data = None
# Fallback dummies if import fails
def render_header(): pass
def get_theme_colors(): return {"card_bg": "#ffffff", "border": "#e2e8f0"}
# ============ PAGE CONFIGURATION ============
st.set_page_config(
page_title="NSE Portfolio Optimizer Pro",
page_icon="πŸ“Š",
layout="wide",
initial_sidebar_state="expanded",
menu_items={'About': 'NSE Portfolio Optimizer Pro v2.0'}
)
# ============ MAIN CONTENT ============
def main():
# 1. Render Header (with Toggle)
render_header()
# 2. Get Dynamic Colors from the Toggle State
colors = get_theme_colors()
# 3. Apply CSS dynamically based on the Toggle
st.markdown(f"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
:root {{ --primary: #2563eb; --font-main: 'Inter', sans-serif; }}
* {{ font-family: var(--font-main); }}
/* Metric Cards - controlled by Python variable, NOT system setting */
.stMetric {{
background: {colors['card_bg']} !important;
border: 1px solid {colors['border']} !important;
border-radius: 10px;
padding: 1rem;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}}
.badge-live {{
background-color: #dcfce7; color: #166534; padding: 2px 8px;
border-radius: 12px; font-size: 0.75rem; font-weight: 600;
border: 1px solid #bbf7d0;
}}
.badge-static {{
background-color: #f1f5f9; color: #475569; padding: 2px 8px;
border-radius: 12px; font-size: 0.75rem; font-weight: 600;
border: 1px solid #e2e8f0;
}}
</style>
""", unsafe_allow_html=True)
# ============ SESSION STATE ============
if 'user_preferences' not in st.session_state:
st.session_state.user_preferences = {
'risk_free_rate': 0.0654,
'default_years': 2,
'default_simulations': 1000,
'brokerage_rate': 0.0003,
'confidence_level': 0.95
}
if 'portfolio_history' not in st.session_state:
st.session_state.portfolio_history = []
if 'current_portfolio' not in st.session_state:
st.session_state.current_portfolio = None
st.title("πŸ“Š NSE Portfolio Optimizer Pro")
# --- TRANSPARENCY & ONBOARDING SECTION ---
with st.expander("πŸ‘‹ New here? Read about the data sources", expanded=False):
st.markdown("""
### 🧐 Data Transparency Statement
We believe in being 100% clear about where our numbers come from:
| Indicator | Source | Real-Time? | Why is it here? |
|-----------|--------|------------|-----------------|
| **NIFTY 50** | Yahoo Finance API | 🟒 **Yes (Delayed ~15m)** | Shows overall market health. |
| **Risk-Free Rate** | User Settings | πŸ”΄ **No (Fixed)** | Used as a benchmark. Defaults to India 10Y Bond Yield (6.54%). |
| **RBI Repo Rate** | Reference Value | πŸ”΄ **No (Fixed)** | Context only. Does not affect calculations. |
| **Stock Prices** | Yahoo Finance API | 🟒 **Yes (Delayed ~15m)** | Used for all portfolio optimization. |
""")
st.markdown("---")
# --- METRICS DASHBOARD ---
col1, col2, col3, col4 = st.columns(4)
# 1. Risk Free Rate (Static/Configurable)
with col1:
rf_rate = st.session_state.user_preferences['risk_free_rate'] * 100
st.metric(
"India Risk-Free Rate",
f"{rf_rate:.2f}%",
"Reference Setting",
help="ℹ️ SOURCE: Your Settings.\n\nThis is the 'safe' return (like a FD or Bond) used to calculate Sharpe Ratio."
)
st.caption("πŸ”΄ Manual Setting")
# 2. RBI Repo Rate (Static)
with col2:
st.metric(
"RBI Repo Rate",
"6.50%",
"Neutral Stance",
help="ℹ️ SOURCE: Fixed Reference.\n\nThis is the rate at which RBI lends money. Hardcoded context."
)
st.caption("πŸ”΄ Reference Only")
# 3. NIFTY 50 (Live)
with col3:
nifty_price = "Loading..."
nifty_delta = ""
is_live = False
if get_nifty_data:
df, _ = get_nifty_data()
if not df.empty:
current = df['Close'].iloc[-1]
prev = df['Close'].iloc[-2] if len(df) > 1 else current
change = current - prev
pct_change = (change / prev) * 100
nifty_price = f"{current:,.0f}"
nifty_delta = f"{change:+,.0f} ({pct_change:+.2f}%)"
is_live = True
st.metric(
"NIFTY 50",
nifty_price,
nifty_delta,
help="ℹ️ SOURCE: Yahoo Finance (Live).\n\nThis shows the current level of the NIFTY 50 index."
)
if is_live:
st.caption(f"🟒 Live Data ({datetime.now().strftime('%H:%M')})")
else:
st.caption("πŸ”΄ Data Unavailable")
# 4. Portfolios
with col4:
st.metric(
"Your Portfolios",
len(st.session_state.portfolio_history),
help="Number of portfolios you have created in this session."
)
st.caption("πŸ”΅ Session Data")
st.markdown("---")
# --- BEGINNER GUIDE ---
st.markdown("## 🧭 How to Start")
tab1, tab2 = st.tabs(["πŸš€ Quick Start", "πŸ’‘ What do these terms mean?"])
with tab1:
col_new, col_exist = st.columns(2)
with col_new:
st.info("### 1. Create a New Portfolio\nGo here if you want to build a portfolio from scratch.")
if st.button("Go to New Portfolio β†’", use_container_width=True):
st.switch_page("pages/1_New_Portfolio.py")
with col_exist:
st.success("### 2. Optimize Existing\nGo here if you already own stocks and want to know how to rebalance them.")
if st.button("Go to Rebalance β†’", use_container_width=True):
st.switch_page("pages/2_Rebalance.py")
with tab2:
st.markdown("""
**Don't let the jargon scare you! Here is what matters:**
* **Sharpe Ratio:** Think of this as "Bang for your Buck." Higher is better.
* **Volatility:** The "Rollercoaster Factor." High volatility means the price jumps up and down a lot.
* **VaR (Value at Risk):** The "Worst Day" predictor.
""")
st.markdown("---")
st.caption("Β© 2025 NSE Portfolio Optimizer Pro | Data provided for educational purposes.")
if __name__ == "__main__":
main()