File size: 10,608 Bytes
15f101d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import streamlit as st
import plotly.graph_objects as go
import numpy as np
import pandas as pd
from indicators import (
    calculate_macd, calculate_atr, calculate_obv,
    calculate_stochastic, calculate_rsi, calculate_sma,
    calculate_bollinger_bands
)

def plot_stock_chart(df, ticker, tickers_companies_dict, indicator_params):
    """

    Generates and displays a stock chart with selected technical indicators.

    """
    title_str = f"{tickers_companies_dict[ticker]}'s stock price"

    try:
        fig = go.Figure()

        # Check if we have the necessary columns for a candlestick chart
        required_ohlc_cols = ['Open', 'High', 'Low', 'Close']
        has_ohlc = all(col in df.columns for col in required_ohlc_cols)

        if not has_ohlc:
            st.error(f"Missing required columns for candlestick chart: {', '.join([col for col in required_ohlc_cols if col not in df.columns])}")
            st.write("Falling back to a line chart for the available data")

            # Try to use Adj Close if available, then Close, then any numeric
            price_col = None
            for col_candidate in ['Close', 'Adj Close', 'Price', 'Last']:
                if col_candidate in df.columns:
                    price_col = col_candidate
                    break

            if price_col:
                fig.add_trace(go.Scatter(
                    x=df.index,
                    y=df[price_col],
                    name=price_col,
                    line=dict(color='blue')
                ))
            else:
                # Just use the first numeric column if no common price column is found
                numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
                if len(numeric_cols) > 0:
                    price_col = numeric_cols[0]
                    fig.add_trace(go.Scatter(
                        x=df.index,
                        y=df[price_col],
                        name=price_col,
                        line=dict(color='blue')
                    ))
                else:
                    st.error("No numeric columns found for plotting.")
                    return # Exit if no data to plot

        else:
            # Add candlestick trace
            fig.add_trace(go.Candlestick(
                x=df.index,
                open=df['Open'],
                high=df['High'],
                low=df['Low'],
                close=df['Close'],
                name='Price'
            ))

        # Add Volume if requested and available
        if indicator_params["volume_flag"] and 'Volume' in df.columns:
            fig.add_trace(go.Bar(
                x=df.index,
                y=df['Volume'],
                name='Volume',
                yaxis='y2'
            ))
            fig.update_layout(
                yaxis2=dict(
                    title="Volume",
                    overlaying="y",
                    side="right"
                )
            )

        # Add SMA if requested
        if indicator_params["sma_flag"] and 'Close' in df.columns:
            df = calculate_sma(df, indicator_params["sma_periods"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df[f'SMA_{indicator_params["sma_periods"]}'],
                name=f'SMA ({indicator_params["sma_periods"]})',
                line=dict(color='blue')
            ))

        # Add Bollinger Bands if requested
        if indicator_params["bb_flag"] and 'Close' in df.columns:
            df = calculate_bollinger_bands(df, indicator_params["bb_periods"], indicator_params["bb_std"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['Upper_Band'],
                name=f'Upper Band ({indicator_params["bb_periods"]}, {indicator_params["bb_std"]})',
                line=dict(color='rgba(250, 0, 0, 0.5)')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['Lower_Band'],
                name=f'Lower Band ({indicator_params["bb_periods"]}, {indicator_params["bb_std"]})',
                line=dict(color='rgba(250, 0, 0, 0.5)')
            ))

        # Add RSI if requested
        if indicator_params["rsi_flag"] and 'Close' in df.columns:
            df = calculate_rsi(df, indicator_params["rsi_periods"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['RSI'],
                name='RSI',
                yaxis='y3',
                line=dict(color='purple')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=[indicator_params["rsi_upper"]] * len(df),
                name=f'RSI Upper ({indicator_params["rsi_upper"]})',
                yaxis='y3',
                line=dict(color='rgba(250, 0, 0, 0.5)', dash='dash')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=[indicator_params["rsi_lower"]] * len(df),
                name=f'RSI Lower ({indicator_params["rsi_lower"]})',
                yaxis='y3',
                line=dict(color='rgba(0, 250, 0, 0.5)', dash='dash')
            ))
            fig.update_layout(
                yaxis3=dict(
                    title="RSI",
                    anchor="free",
                    overlaying="y",
                    side="right",
                    position=1.0,
                    range=[0, 100]
                )
            )

        # Add MACD if requested
        if indicator_params["macd_flag"] and 'Close' in df.columns:
            df = calculate_macd(df, indicator_params["macd_fast"], indicator_params["macd_slow"], indicator_params["macd_signal"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['MACD'],
                name='MACD',
                yaxis='y4',
                line=dict(color='blue')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['MACD_Signal'],
                name='MACD Signal',
                yaxis='y4',
                line=dict(color='red')
            ))
            fig.add_trace(go.Bar(
                x=df.index,
                y=df['MACD_Histogram'],
                name='MACD Histogram',
                yaxis='y4',
                marker=dict(
                    color=np.where(df['MACD_Histogram'] >= 0, 'green', 'red'),
                    opacity=0.7
                )
            ))
            fig.update_layout(
                yaxis4=dict(
                    title="MACD",
                    anchor="free",
                    overlaying="y",
                    side="right",
                    position=0.95
                )
            )

        # Add ATR if requested
        if indicator_params["atr_flag"] and all(col in df.columns for col in ['High', 'Low', 'Close']):
            df = calculate_atr(df, indicator_params["atr_period"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['ATR'],
                name=f'ATR ({indicator_params["atr_period"]})',
                yaxis='y5',
                line=dict(color='orange')
            ))
            fig.update_layout(
                yaxis5=dict(
                    title="ATR",
                    anchor="free",
                    overlaying="y",
                    side="right",
                    position=0.90
                )
            )

        # Add OBV if requested
        if indicator_params["obv_flag"] and all(col in df.columns for col in ['Close', 'Volume']):
            df = calculate_obv(df)
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['OBV'],
                name='OBV',
                yaxis='y6',
                line=dict(color='brown')
            ))
            fig.update_layout(
                yaxis6=dict(
                    title="OBV",
                    anchor="free",
                    overlaying="y",
                    side="right",
                    position=0.85
                )
            )

        # Add Stochastic Oscillator if requested
        if indicator_params["stoch_flag"] and all(col in df.columns for col in ['High', 'Low', 'Close']):
            df = calculate_stochastic(df, indicator_params["stoch_k"], indicator_params["stoch_d"])
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['%K'],
                name='%K',
                yaxis='y7',
                line=dict(color='blue')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=df['%D'],
                name='%D',
                yaxis='y7',
                line=dict(color='red')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=[80] * len(df),
                name='Overbought (80)',
                yaxis='y7',
                line=dict(color='rgba(250, 0, 0, 0.5)', dash='dash')
            ))
            fig.add_trace(go.Scatter(
                x=df.index,
                y=[20] * len(df),
                name='Oversold (20)',
                yaxis='y7',
                line=dict(color='rgba(0, 250, 0, 0.5)', dash='dash')
            ))
            fig.update_layout(
                yaxis7=dict(
                    title="Stochastic",
                    anchor="free",
                    overlaying="y",
                    side="right",
                    position=0.80,
                    range=[0, 100]
                )
            )

        # Update layout
        fig.update_layout(
            title=title_str,
            xaxis_title='Date',
            yaxis_title='Price',
            xaxis_rangeslider_visible=False,
            legend=dict(
                orientation="h",
                yanchor="bottom",
                y=1.02,
                xanchor="right",
                x=1
            ),
            height=800
        )

        st.plotly_chart(fig, use_container_width=True)

    except Exception as e:
        st.error(f"Error generating chart: {str(e)}")
        st.write("There was an error generating the chart. Please try different parameters or a different stock symbol.")
        st.write("DataFrame head:")
        st.write(df.head())
        st.write("Available columns:", ", ".join(df.columns))