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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# FB Page: https://www.facebook.com/AIsparking

import streamlit as st
import yfinance as yf
import pandas as pd
import numpy as np
from ta.trend import MACD
from ta.momentum import RSIIndicator, StochasticOscillator
from ta.volatility import AverageTrueRange, BollingerBands
from ta.volume import VolumeWeightedAveragePrice
import plotly.graph_objects as go
from datetime import datetime, timedelta
import plotly.subplots as sp
from ta.trend import IchimokuIndicator

# Set page config
st.set_page_config(layout="wide", page_title="Stock Technical Analysis")

# Functions from previous implementations remain the same
# Add error handling wrapper
def safe_execute(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            st.error(f"Error: {str(e)}")
            return None
    return wrapper

@safe_execute
def get_stock_data(symbol, market, nYear):
    if market == 'HK':
        symbol = f'{symbol}.HK'
    
    end_date = datetime.now()
    start_date = end_date - timedelta(days=nYear*365)
    
    df = yf.download(symbol, start=start_date, end=end_date)
    if df.empty:
        raise ValueError("No data found for this symbol")
    return df

# Combine all technical indicators
@safe_execute

def generate_recommendation(df):
    last_row = df.iloc[-1]
    signals = []
    
    # RSI signals
    if last_row['RSI'] < 30:
        signals.append(('BUY', 'RSI oversold'))
    elif last_row['RSI'] > 70:
        signals.append(('SELL', 'RSI overbought'))
    
    # Stochastic signals
    if last_row['%K'] < 20 and last_row['%D'] < 20:
        signals.append(('BUY', 'Stochastic oversold'))
    elif last_row['%K'] > 80 and last_row['%D'] > 80:
        signals.append(('SELL', 'Stochastic overbought'))
    
    # MACD signals
    if last_row['MACD'] > last_row['MACD_Signal']:
        signals.append(('BUY', 'MACD crossover'))
    elif last_row['MACD'] < last_row['MACD_Signal']:
        signals.append(('SELL', 'MACD crossunder'))
    
    return signals

def calculate_indicators(df):
    # Calculate RSI
    rsi = RSIIndicator(df['Close'])
    df['RSI'] = rsi.rsi()
    
    # Calculate Stochastic
    stoch = StochasticOscillator(df['High'], df['Low'], df['Close'])
    df['%K'] = stoch.stoch()
    df['%D'] = stoch.stoch_signal()
    
    # Calculate MACD
    macd = MACD(df['Close'])
    df['MACD'] = macd.macd()
    df['MACD_Signal'] = macd.macd_signal()
    
    # Calculate ATR
    atr = AverageTrueRange(df['High'], df['Low'], df['Close'])
    df['ATR'] = atr.average_true_range()
    
    # Add multiple SMAs
    df['SMA20'] = df['Close'].rolling(window=20).mean()
    df['SMA50'] = df['Close'].rolling(window=50).mean()
    df['SMA100'] = df['Close'].rolling(window=100).mean()
    df['SMA200'] = df['Close'].rolling(window=200).mean()
    
    # Calculate ATR
    atr = AverageTrueRange(df['High'], df['Low'], df['Close'])
    df['ATR'] = atr.average_true_range()
    
    # Calculate VWAP
    vwap = VolumeWeightedAveragePrice(high=df['High'], 
                                     low=df['Low'], 
                                     close=df['Close'], 
                                     volume=df['Volume'])
    df['VWAP'] = vwap.volume_weighted_average_price()
    
    return df

def calculate_additional_indicators(df):
    # Add Bollinger Bands
    bb = BollingerBands(df['Close'])
    df['BB_upper'] = bb.bollinger_hband()
    df['BB_lower'] = bb.bollinger_lband()
    df['BB_middle'] = bb.bollinger_mavg()
    
    # Add Moving Averages
    df['MA50'] = df['Close'].rolling(window=50).mean()
    df['MA200'] = df['Close'].rolling(window=200).mean()
    
    # Add VWAP
    vwap = VolumeWeightedAveragePrice(high=df['High'], low=df['Low'], 
                                     close=df['Close'], volume=df['Volume'])
    df['VWAP'] = vwap.volume_weighted_average_price()
    
    return df

def calculate_ema(df):
    # Calculate EMAs
    df['EMA9'] = df['Close'].ewm(span=9, adjust=False).mean()
    df['EMA21'] = df['Close'].ewm(span=21, adjust=False).mean()
    return df

def calculate_obv(df):
    # Calculate OBV
    obv = []
    prev_obv = 0
    for i in range(len(df)):
        if i == 0:
            obv.append(prev_obv)
            continue
        if df['Close'].iloc[i] > df['Close'].iloc[i-1]:
            current_obv = prev_obv + df['Volume'].iloc[i]
        elif df['Close'].iloc[i] < df['Close'].iloc[i-1]:
            current_obv = prev_obv - df['Volume'].iloc[i]
        else:
            current_obv = prev_obv
        obv.append(current_obv)
        prev_obv = current_obv
    df['OBV'] = obv
    return df

def calculate_ichimoku(df):
    ichimoku = IchimokuIndicator(high=df['High'], low=df['Low'])
    df['ichimoku_a'] = ichimoku.ichimoku_a()
    df['ichimoku_b'] = ichimoku.ichimoku_b()
    df['ichimoku_base'] = ichimoku.ichimoku_base_line()
    df['ichimoku_conversion'] = ichimoku.ichimoku_conversion_line()
    return df

def calculate_all_indicators(df):
    df = calculate_indicators(df)
    df = calculate_additional_indicators(df)
    df = calculate_ema(df)
    df = calculate_obv(df)
    df = calculate_ichimoku(df)
    return df

def enhanced_recommendation(df):
    last_row = df.iloc[-1]
    prev_row = df.iloc[-2]
    signals = []
    
    # Add EMA signals
    if last_row['EMA9'] > last_row['EMA21']:
        signals.append(('BUY', 'EMA9 crossed above EMA21'))
    elif last_row['EMA9'] < last_row['EMA21']:
        signals.append(('SELL', 'EMA9 crossed below EMA21'))
    
    # Add Ichimoku signals
    if (last_row['ichimoku_conversion'] > last_row['ichimoku_base'] and 
        last_row['Close'] > last_row['ichimoku_a']):
        signals.append(('BUY', 'Ichimoku bullish signal'))
    elif (last_row['ichimoku_conversion'] < last_row['ichimoku_base'] and 
          last_row['Close'] < last_row['ichimoku_b']):
        signals.append(('SELL', 'Ichimoku bearish signal'))
    
    # Add OBV signals
    if df['OBV'].iloc[-1] > df['OBV'].iloc[-2]:
        signals.append(('BUY', 'OBV increasing'))
    else:
        signals.append(('SELL', 'OBV decreasing'))

    # Add SMA signals
    if (last_row['SMA20'] > last_row['SMA50'] and 
        prev_row['SMA20'] <= prev_row['SMA50']):
        signals.append(('BUY', 'SMA20 crossed above SMA50'))
    elif (last_row['SMA20'] < last_row['SMA50'] and 
          prev_row['SMA20'] >= prev_row['SMA50']):
        signals.append(('SELL', 'SMA20 crossed below SMA50'))
    
    # Add VWAP signals
    if last_row['Close'] > last_row['VWAP']:
        signals.append(('BUY', 'Price above VWAP'))
    else:
        signals.append(('SELL', 'Price below VWAP'))
    
    # Add ATR-based volatility signals
    atr_threshold = df['ATR'].mean() * 1.5
    if last_row['ATR'] > atr_threshold:
        signals.append(('HOLD', 'High volatility detected by ATR'))
    
    return signals


# Main app layout
st.title('Advanced Stock Technical Analysis')

# Sidebar for inputs
with st.sidebar:
    st.header('Input Parameters')
    market = st.selectbox('Select Market', ['HK','US'])
    symbol = st.text_input('Enter Stock Symbol (e.g. 0700 for HK):')
    
    # Add analysis timeframe option
    timeframe = st.selectbox('Select Timeframe', ['1y','2y','3y','5y','8y','10y'])
    nYear = int(timeframe.split("y")[0])

if st.sidebar.button('Analyze'):
    if symbol:
        with st.spinner('Fetching and analyzing data...'):
            df = get_stock_data(symbol, market,nYear)
            
            if df is not None:
                df = calculate_all_indicators(df)
                
                # Create tabs for different analyses
                tab1, tab2, tab3 = st.tabs(['Price Analysis', 'Technical Indicators', 'Technical Analysis'])
                
                with tab1:
                    # Main price chart with volume
                    fig = sp.make_subplots(rows=2, cols=1, shared_xaxes=True, 
                                         vertical_spacing=0.03, row_heights=[0.7, 0.3])
                    
                    fig.add_trace(go.Candlestick(x=df.index, open=df['Open'],
                                                high=df['High'], low=df['Low'],
                                                close=df['Close'], name='Price'),
                                 row=1, col=1)
                    
                    # Add Moving Averages
                    fig.add_trace(go.Scatter(x=df.index, y=df['MA50'],
                                           name='MA50', line=dict(color='orange')),
                                 row=1, col=1)
                    fig.add_trace(go.Scatter(x=df.index, y=df['MA200'],
                                           name='MA200', line=dict(color='blue')),
                                 row=1, col=1)
                    
                    # Add volume bars
                    colors = ['red' if row['Open'] > row['Close'] else 'green' 
                             for index, row in df.iterrows()]
                    fig.add_trace(go.Bar(x=df.index, y=df['Volume'],
                                       marker_color=colors, name='Volume'),
                                 row=2, col=1)
                    
                    fig.update_layout(height=800)
                    st.plotly_chart(fig, use_container_width=True)
                
                with tab2:
                    col1, col2 = st.columns(2)
                    
                    with col1:
                        # RSI Plot
                        fig_rsi = go.Figure()
                        fig_rsi.add_trace(go.Scatter(x=df.index, y=df['RSI'],
                                                    name='RSI'))
                        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 Indicator')
                        st.plotly_chart(fig_rsi)
                        
                        # MACD Plot
                        fig_macd = go.Figure()
                        fig_macd.add_trace(go.Scatter(x=df.index, y=df['MACD'],
                                                     name='MACD'))
                        fig_macd.add_trace(go.Scatter(x=df.index, y=df['MACD_Signal'],
                                                     name='Signal'))
                        fig_macd.update_layout(title='MACD Indicator')
                        st.plotly_chart(fig_macd)

                        # EMA Plot
                        fig_ema = go.Figure()
                        fig_ema.add_trace(go.Scatter(x=df.index, y=df['EMA9'], name='EMA9'))
                        fig_ema.add_trace(go.Scatter(x=df.index, y=df['EMA21'], name='EMA21'))
                        fig_ema.update_layout(title='EMA Indicators')
                        st.plotly_chart(fig_ema)
                        
                        # OBV Plot
                        fig_obv = go.Figure()
                        fig_obv.add_trace(go.Scatter(x=df.index, y=df['OBV'], name='OBV'))
                        fig_obv.update_layout(title='On-Balance Volume (OBV)')
                        st.plotly_chart(fig_obv)

                        # VWAP Plot
                        fig_vwap = go.Figure()
                        fig_vwap.add_trace(go.Scatter(x=df.index, y=df['Close'], name='Price'))
                        fig_vwap.add_trace(go.Scatter(x=df.index, y=df['VWAP'], 
                                                     name='VWAP', line=dict(color='orange')))
                        fig_vwap.update_layout(title='Volume Weighted Average Price (VWAP)')
                        st.plotly_chart(fig_vwap)                        

                    
                    with col2:
                        # Stochastic Plot
                        fig_stoch = go.Figure()
                        fig_stoch.add_trace(go.Scatter(x=df.index, y=df['%K'],
                                                      name='%K'))
                        fig_stoch.add_trace(go.Scatter(x=df.index, y=df['%D'],
                                                      name='%D'))
                        fig_stoch.update_layout(title='Stochastic Oscillator')
                        st.plotly_chart(fig_stoch)
                        
                        # Bollinger Bands
                        fig_bb = go.Figure()
                        fig_bb.add_trace(go.Scatter(x=df.index, y=df['BB_upper'],
                                                   name='Upper Band'))
                        fig_bb.add_trace(go.Scatter(x=df.index, y=df['Close'],
                                                   name='Price'))
                        fig_bb.add_trace(go.Scatter(x=df.index, y=df['BB_lower'],
                                                   name='Lower Band'))
                        fig_bb.update_layout(title='Bollinger Bands')
                        st.plotly_chart(fig_bb)

                        # Ichimoku Cloud
                        fig_ichimoku = go.Figure()
                        fig_ichimoku.add_trace(go.Scatter(x=df.index, y=df['ichimoku_a'], name='Senkou Span A'))
                        fig_ichimoku.add_trace(go.Scatter(x=df.index, y=df['ichimoku_b'], name='Senkou Span B'))
                        fig_ichimoku.add_trace(go.Scatter(x=df.index, y=df['ichimoku_base'], name='Kijun-sen'))
                        fig_ichimoku.add_trace(go.Scatter(x=df.index, y=df['ichimoku_conversion'], name='Tenkan-sen'))
                        fig_ichimoku.update_layout(title='Ichimoku Cloud')
                        st.plotly_chart(fig_ichimoku)

                        # SMA Plot
                        fig_sma = go.Figure()
                        fig_sma.add_trace(go.Scatter(x=df.index, y=df['Close'], name='Price'))
                        fig_sma.add_trace(go.Scatter(x=df.index, y=df['SMA20'], name='SMA20'))
                        fig_sma.add_trace(go.Scatter(x=df.index, y=df['SMA50'], name='SMA50'))
                        fig_sma.add_trace(go.Scatter(x=df.index, y=df['SMA100'], name='SMA100'))
                        fig_sma.add_trace(go.Scatter(x=df.index, y=df['SMA200'], name='SMA200'))
                        fig_sma.update_layout(title='Simple Moving Averages (SMA)')
                        st.plotly_chart(fig_sma)
                        
                        # ATR Plot
                        fig_atr = go.Figure()
                        fig_atr.add_trace(go.Scatter(x=df.index, y=df['ATR'], name='ATR'))
                        fig_atr.update_layout(title='Average True Range (ATR)')
                        st.plotly_chart(fig_atr)


                with tab3:
                    st.subheader('Technical Analysis')
                    
                    # Combine all signals
                    signals = generate_recommendation(df)
                    enhanced_signals = enhanced_recommendation(df)
                    all_signals = signals + enhanced_signals
                    
                    # Count buy and sell signals
                    buy_signals = len([s for s in all_signals if s[0] == 'BUY'])
                    sell_signals = len([s for s in all_signals if s[0] == 'SELL'])
                    
                    # Display recommendation summary
                    col1, col2, col3 = st.columns(3)
                    with col1:
                        st.metric("Buy Signals", buy_signals)
                    with col2:
                        st.metric("Sell Signals", sell_signals)
                    with col3:
                        overall_rec = "BUY" if buy_signals > sell_signals else "SELL" if sell_signals > buy_signals else "HOLD"
                        if overall_rec == "BUY":
                            st.success(f"Overall: {overall_rec} πŸ“ˆ")
                        elif overall_rec == "SELL":
                            st.error(f"Overall: {overall_rec} πŸ“‰")
                        else:
                            st.warning(f"Overall: {overall_rec} ↔️")
                    
                    # Display detailed signals
                    st.subheader("Detailed Signals:")
                    for signal, reason in all_signals:
                        if signal == 'BUY':
                            st.success(f"🟒 {signal}: {reason}")
                        else:
                            st.error(f"πŸ”΄ {signal}: {reason}")

                    # Technical Indicators Summary
                    st.subheader("Technical Indicators Summary")
                    
                    # Create expandable section for current price levels
                    with st.expander("πŸ“Š Current Price Levels", expanded=True):
                        current_price = df['Close'].iloc[-1]
                        prev_close = df['Close'].iloc[-2]
                        price_change = ((current_price - prev_close) / prev_close) * 100
                        
                        # Price information with colored indicators
                        if price_change > 0:
                            st.success(f"πŸ“ˆ Current Price: ${current_price:.2f} (+{price_change:.2f}%)")
                        else:
                            st.error(f"πŸ“‰ Current Price: ${current_price:.2f} ({price_change:.2f}%)")
                        
                        # Display technical levels in a more organized way
                        col1, col2, col3 = st.columns(3)
                        with col1:
                            st.info(f"πŸ’Ή VWAP\n${df['VWAP'].iloc[-1]:.2f}")
                        with col2:
                            st.info(f"πŸ“ ATR\n${df['ATR'].iloc[-1]:.2f}")
                        with col3:
                            st.info(f"πŸ“ˆ SMA50\n${df['SMA50'].iloc[-1]:.2f}")

                    # Moving Averages Analysis
                    with st.expander("πŸ“ˆ Moving Averages Analysis", expanded=True):
                        sma_status = "Bullish" if (df['SMA20'].iloc[-1] > df['SMA50'].iloc[-1]) else "Bearish"
                        sma_icon = "🟒" if sma_status == "Bullish" else "πŸ”΄"
                        st.write(f"{sma_icon} SMA20 vs SMA50: {sma_status}")
                        
                        # Add more SMA comparisons
                        sma_100_status = "Bullish" if (df['Close'].iloc[-1] > df['SMA100'].iloc[-1]) else "Bearish"
                        sma_100_icon = "🟒" if sma_100_status == "Bullish" else "πŸ”΄"
                        st.write(f"{sma_100_icon} Price vs SMA100: {sma_100_status}")
                        
                        sma_200_status = "Bullish" if (df['Close'].iloc[-1] > df['SMA200'].iloc[-1]) else "Bearish"
                        sma_200_icon = "🟒" if sma_200_status == "Bullish" else "πŸ”΄"
                        st.write(f"{sma_200_icon} Price vs SMA200: {sma_200_status}")

                    # Volatility Analysis
                    with st.expander("πŸ“Š Volatility Analysis", expanded=True):
                        atr_avg = df['ATR'].mean()
                        current_atr = df['ATR'].iloc[-1]
                        atr_ratio = current_atr / atr_avg
                        
                        if atr_ratio > 1.5:
                            volatility = "High"
                            vol_icon = "⚠️"
                            st.warning(f"{vol_icon} Volatility: {volatility}")
                        elif atr_ratio < 0.5:
                            volatility = "Low"
                            vol_icon = "πŸ’€"
                            st.info(f"{vol_icon} Volatility: {volatility}")
                        else:
                            volatility = "Normal"
                            vol_icon = "βœ…"
                            st.success(f"{vol_icon} Volatility: {volatility}")
                        
                        st.write(f"ATR Ratio: {atr_ratio:.2f}x average")

                    # VWAP Analysis
                    with st.expander("πŸ’Ή VWAP Analysis", expanded=True):
                        vwap_diff = ((df['Close'].iloc[-1] - df['VWAP'].iloc[-1]) / df['VWAP'].iloc[-1]) * 100
                        if vwap_diff > 0:
                            st.success(f"🟒 Price is ABOVE VWAP by {abs(vwap_diff):.2f}%")
                        else:
                            st.error(f"πŸ”΄ Price is BELOW VWAP by {abs(vwap_diff):.2f}%")
                    
                    # Add risk warning
                    st.warning("⚠️ Disclaimer: Above analysis is for AI Research and Learning purposes only. DO NOT make any investment decisions according to the displayed information and analysis result.")
            
            else:
                st.error('Error fetching stock data. Please check the symbol.')
    else:
        st.warning('Please enter a stock symbol.')


with st.sidebar:
    # Add a horizontal line
    st.markdown("---")

    # Add text at the bottom
    st.warning("⚠️ Disclaimer: This is for AI Research and Learning purposes only. DO NOT make any investment decisions according to the displayed information and analysis result.")

    st.markdown("---")
    st.write("https://www.facebook.com/AIsparking")