File size: 5,031 Bytes
f7323a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Stock Analysis Page - Comprehensive stock analysis with technical indicators."""

import streamlit as st
import sys
import os

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from components.styles import DARK_THEME_CSS
from components.data_sources import (
    load_stock_data,
    load_company_profile,
    load_income_statement,
    calculate_technical_indicators,
    get_price_metrics,
)
from components.chart import (
    create_price_chart,
    create_rsi_chart,
    create_financial_chart,
)
from components.ui import (
    display_price_metrics,
    display_company_info,
    display_financial_metrics,
    display_income_statement,
    display_profitability_metrics,
)


# ---- Page Configuration ----
st.set_page_config(
    page_title="Stocks - Financial Dashboard",
    page_icon="πŸ“ˆ",
    layout="wide",
    initial_sidebar_state="expanded",
)

# ---- Apply Dark Theme ----
st.markdown(DARK_THEME_CSS, unsafe_allow_html=True)

# ---- Header ----
st.markdown("# πŸ“ˆ Stock Analysis")
st.markdown("Real-time technical analysis with comprehensive financial metrics")

# ---- Sidebar Configuration ----
with st.sidebar:
    st.markdown("## βš™οΈ Settings")
    symbol = st.text_input("Stock Ticker", "AAPL", help="Enter a valid stock ticker symbol").upper()
    period = st.slider("Indicator Period", 5, 50, 20, help="Period for SMA, EMA, and RSI calculations")

    st.markdown("---")
    st.markdown("### About")
    st.info("Analyze stocks with technical indicators, financials, and TradingView charts.")


def main():
    """Main stock analysis logic."""
    if st.button("πŸ“Š Load Stock Data", key="load_btn", use_container_width=True):
        try:
            # Load data
            with st.spinner("Loading data..."):
                df = load_stock_data(symbol)
                profile_info = load_company_profile(symbol)
                income_stmt = load_income_statement(symbol)

            # Calculate technical indicators
            df = calculate_technical_indicators(df, period)

            # Display price metrics
            metrics = get_price_metrics(df)
            display_price_metrics(metrics)

            # Display company information
            display_company_info(profile_info)

            # Display financial metrics
            if not income_stmt.empty:
                display_financial_metrics(income_stmt)

                # Financial history chart
                st.markdown('<div class="section-title">πŸ“Š Revenue & Net Income Trend</div>', unsafe_allow_html=True)
                income_chart_data = income_stmt[['period_ending', 'total_revenue', 'net_income']].dropna()

                if len(income_chart_data) > 0:
                    fig_financial = create_financial_chart(income_chart_data)
                    st.plotly_chart(fig_financial, use_container_width=True)

            # ---- Tabs ----
            tab1, tab2, tab3, tab4 = st.tabs([
                "πŸ“ˆ Price & Moving Averages",
                "πŸ“Š RSI Indicator",
                "πŸ“‰ TradingView",
                "πŸ“‹ Financials"
            ])

            # Tab 1: Price & Moving Averages
            with tab1:
                fig_price = create_price_chart(df, symbol, period)
                st.plotly_chart(fig_price, use_container_width=True)

            # Tab 2: RSI Indicator
            with tab2:
                fig_rsi = create_rsi_chart(df, symbol)
                st.plotly_chart(fig_rsi, use_container_width=True)

            # Tab 3: TradingView
            with tab3:
                tradingview_html = f"""
                <div class="tradingview-widget-container">
                  <div id="tradingview_{symbol}"></div>
                  <script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
                  <script type="text/javascript">
                    new TradingView.widget({{
                      "width": "100%",
                      "height": 600,
                      "symbol": "{symbol}",
                      "interval": "D",
                      "timezone": "Etc/UTC",
                      "theme": "dark",
                      "style": "1",
                      "locale": "en",
                      "enable_publishing": false,
                      "allow_symbol_change": true,
                      "container_id": "tradingview_{symbol}"
                    }});
                  </script>
                </div>
                """
                st.components.v1.html(tradingview_html, height=650)

            # Tab 4: Detailed Financials
            with tab4:
                if not income_stmt.empty:
                    display_income_statement(income_stmt)
                    display_profitability_metrics(income_stmt)

        except Exception as e:
            st.error(f"Error loading data for {symbol}: {str(e)}")
            st.info("Please check the ticker symbol and try again.")


if __name__ == "__main__":
    main()