File size: 5,095 Bytes
47551b9
b7c6830
 
47551b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7c6830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47551b9
 
 
 
 
 
 
 
b7c6830
 
 
 
 
 
 
 
 
 
 
47551b9
 
 
 
b7c6830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47551b9
 
 
b7c6830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47551b9
 
 
 
b7c6830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47551b9
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import streamlit as st
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
from utils.data_loader import load_nifty50_symbols, fetch_stock_data
from utils.technical_indicators import (
    calculate_rsi,
    calculate_macd,
    calculate_bollinger_bands,
    calculate_support_resistance
)

st.title("Technical Analysis")

# Stock selection
symbol = st.selectbox("Select Stock", load_nifty50_symbols())
timeframe = st.selectbox(
    "Select Timeframe",
    options=["1w", "1mo", "3mo", "6mo", "1y", "3y", "5y"],
    format_func=lambda x: {
        "1w": "1 Week",
        "1mo": "1 Month",
        "3mo": "1 Quarter",
        "6mo": "6 Months",
        "1y": "1 Year",
        "3y": "3 Years",
        "5y": "5 Years"
    }[x]
)

# Fetch data
data = fetch_stock_data(symbol, period=timeframe)

if data is not None:
    # Create tabs for different analysis
    tab1, tab2, tab3 = st.tabs(["Price & Volume", "Technical Indicators", "Support & Resistance"])

    with tab1:
        # Candlestick chart with volume
        fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                           vertical_spacing=0.03, 
                           row_heights=[0.7, 0.3])

        # Candlestick
        fig.add_trace(go.Candlestick(
            x=data.index,
            open=data['Open'],
            high=data['High'],
            low=data['Low'],
            close=data['Close'],
            name='OHLC'
        ), row=1, col=1)

        # Volume
        fig.add_trace(go.Bar(
            x=data.index,
            y=data['Volume'],
            name='Volume',
            marker_color='rgba(0,184,148,0.3)'
        ), row=2, col=1)

        fig.update_layout(
            title=f"{symbol} Price and Volume",
            yaxis_title="Price",
            yaxis2_title="Volume",
            xaxis_rangeslider_visible=False,
            height=800
        )

        st.plotly_chart(fig, use_container_width=True)

    with tab2:
        # Technical indicators
        col1, col2 = st.columns(2)

        with col1:
            # RSI
            rsi = calculate_rsi(data)
            fig_rsi = go.Figure()
            fig_rsi.add_trace(go.Scatter(
                x=data.index,
                y=rsi,
                name='RSI',
                line=dict(color='#00B894')
            ))
            fig_rsi.add_hline(y=70, line_dash="dash", line_color="red")
            fig_rsi.add_hline(y=30, line_dash="dash", line_color="green")
            fig_rsi.update_layout(title="RSI (14)", height=400)
            st.plotly_chart(fig_rsi, use_container_width=True)

        with col2:
            # MACD
            macd, signal = calculate_macd(data)
            fig_macd = go.Figure()
            fig_macd.add_trace(go.Scatter(
                x=data.index,
                y=macd,
                name='MACD',
                line=dict(color='#00B894')
            ))
            fig_macd.add_trace(go.Scatter(
                x=data.index,
                y=signal,
                name='Signal',
                line=dict(color='#FFA500')
            ))
            fig_macd.update_layout(title="MACD", height=400)
            st.plotly_chart(fig_macd, use_container_width=True)

        # Bollinger Bands
        upper_band, middle_band, lower_band = calculate_bollinger_bands(data)
        fig_bb = go.Figure()
        fig_bb.add_trace(go.Scatter(
            x=data.index,
            y=upper_band,
            name='Upper Band',
            line=dict(color='gray', dash='dash')
        ))
        fig_bb.add_trace(go.Scatter(
            x=data.index,
            y=middle_band,
            name='Middle Band',
            line=dict(color='blue')
        ))
        fig_bb.add_trace(go.Scatter(
            x=data.index,
            y=lower_band,
            name='Lower Band',
            line=dict(color='gray', dash='dash')
        ))
        fig_bb.add_trace(go.Scatter(
            x=data.index,
            y=data['Close'],
            name='Close Price',
            line=dict(color='#00B894')
        ))
        fig_bb.update_layout(title="Bollinger Bands", height=500)
        st.plotly_chart(fig_bb, use_container_width=True)

    with tab3:
        # Support and Resistance
        support, resistance = calculate_support_resistance(data)
        fig_sr = go.Figure()

        fig_sr.add_trace(go.Scatter(
            x=data.index,
            y=data['Close'],
            name='Price',
            line=dict(color='#00B894')
        ))
        fig_sr.add_trace(go.Scatter(
            x=data.index,
            y=support,
            name='Support',
            line=dict(color='green', dash='dash')
        ))
        fig_sr.add_trace(go.Scatter(
            x=data.index,
            y=resistance,
            name='Resistance',
            line=dict(color='red', dash='dash')
        ))

        fig_sr.update_layout(
            title="Support and Resistance Levels",
            height=600
        )
        st.plotly_chart(fig_sr, use_container_width=True)

else:
    st.error("Unable to fetch data. Please try again later.")