File size: 40,510 Bytes
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bc0151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
 
278cb18
a1bc0de
 
 
 
278cb18
 
a1bc0de
a0bf426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278cb18
 
 
a1bc0de
 
 
278cb18
 
 
 
 
 
 
 
 
 
a0bf426
 
278cb18
 
a1bc0de
 
278cb18
a1bc0de
 
 
 
278cb18
 
 
a0bf426
 
278cb18
 
a774f18
 
 
 
 
 
 
278cb18
a1bc0de
278cb18
a1bc0de
 
278cb18
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
bf56cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bf426
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
62985bb
 
a1bc0de
 
 
 
 
 
62985bb
 
a1bc0de
 
 
 
 
 
 
62985bb
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bf426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79ac946
 
a1bc0de
 
79ac946
a1bc0de
 
79ac946
a1bc0de
 
 
 
 
185dd5e
a1bc0de
 
 
cbb764c
a1bc0de
cbb764c
 
a1bc0de
 
a0bf426
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79ac946
 
 
 
 
 
 
a1bc0de
 
 
 
 
11c4092
a1bc0de
 
 
 
 
 
278cb18
 
 
 
 
 
 
 
 
62985bb
278cb18
a0bf426
 
 
 
79ac946
 
 
 
 
 
a0bf426
bf56cf6
 
 
 
 
 
 
 
 
79ac946
 
 
 
 
 
 
 
 
 
 
 
 
278cb18
 
 
 
a1bc0de
278cb18
bf56cf6
7c20310
a1bc0de
 
a0bf426
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c4092
a1bc0de
 
 
 
 
01bf896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a774f18
 
01bf896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
 
 
 
 
 
01bf896
 
a0bf426
a1bc0de
 
 
 
278cb18
 
a1bc0de
278cb18
01bf896
 
a1bc0de
3318a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3318a9f
 
a1bc0de
3318a9f
 
a1bc0de
3318a9f
 
 
 
 
a1bc0de
3318a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7461b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3318a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
3318a9f
 
a1bc0de
3318a9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bc0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7461b0
 
 
 
 
8d90ed6
 
 
e7461b0
3318a9f
 
a1bc0de
 
 
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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
import streamlit as st
import pandas as pd
import numpy as np
import yfinance as yf
import plotly.graph_objects as go
from plotly.subplots import make_subplots 
import plotly.express as px
from datetime import datetime, timedelta
from statsmodels.tsa.statespace.sarimax import SARIMAX
from prophet import Prophet
from sklearn.ensemble import RandomForestRegressor
import xgboost as xgb
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Dense, LSTM
from sklearn.model_selection import train_test_split, TimeSeriesSplit
from sklearn.metrics import mean_squared_error, mean_absolute_percentage_error
import requests
from bs4 import BeautifulSoup
import base64
import warnings
from ta.trend import SMAIndicator, EMAIndicator
from ta.momentum import RSIIndicator
from ta.volatility import BollingerBands
from pmdarima import auto_arima
warnings.filterwarnings('ignore')

# List of companies (display name, ticker symbol)
COMPANIES = [
        ("3M", "MMM"), ("Abbott Laboratories", "ABT"), ("AbbVie", "ABBV"), ("Accenture", "ACN"), ("Adobe", "ADBE"),
        ("Advanced Micro Devices", "AMD"), ("Aflac", "AFL"), ("Agilent Technologies", "A"), ("Air Products and Chemicals", "APD"),
        ("Alcoa", "AA"), ("Allstate", "ALL"), ("Alphabet (Google)", "GOOGL"), ("Altria Group", "MO"),
        ("Amazon", "AMZN"), ("American Express", "AXP"), ("American International Group", "AIG"), ("Amgen", "AMGN"),
        ("Analog Devices", "ADI"), ("Apple", "AAPL"), ("Applied Materials", "AMAT"), ("Archer-Daniels-Midland", "ADM"),
        ("AT&T", "T"), ("Autodesk", "ADSK"), ("AutoZone", "AZO"), ("Bank of America", "BAC"), ("Bank of New York Mellon", "BK"),
        ("Baxter International", "BAX"), ("BB&T Corporation", "BBT"), ("Best Buy", "BBY"), ("Biogen", "BIIB"), ("BlackRock", "BLK"),
        ("Boeing", "BA"), ("Bristol-Myers Squibb", "BMY"), ("Broadcom", "AVGO"), ("Capital One Financial", "COF"),
        ("Caterpillar", "CAT"), ("Celgene", "CELG"), ("CenterPoint Energy", "CNP"), ("Chevron", "CVX"),
        ("Chipotle Mexican Grill", "CMG"), ("Chubb", "CB"), ("Cigna", "CI"), ("Cisco Systems", "CSCO"),
        ("Citigroup", "C"), ("Coca-Cola", "KO"), ("Colgate-Palmolive", "CL"), ("Comcast", "CMCSA"), ("ConocoPhillips", "COP"),
        ("Costco Wholesale", "COST"), ("CVS Health", "CVS"), ("Danaher", "DHR"), ("Deere & Company", "DE"),
        ("Dell Technologies", "DELL"), ("Delta Air Lines", "DAL"), ("Disney", "DIS"), ("Dollar General", "DG"),
        ("Dominion Energy", "D"), ("Dow", "DOW"), ("Duke Energy", "DUK"), ("DuPont", "DD"), ("Eaton", "ETN"),
        ("eBay", "EBAY"), ("Eli Lilly", "LLY"), ("Emerson Electric", "EMR"), ("EOG Resources", "EOG"), ("Equinix", "EQIX"),
        ("Exelon", "EXC"), ("Exxon Mobil", "XOM"), ("FedEx", "FDX"), ("Fidelity National Information Services", "FIS"),
        ("Ford Motor", "F"), ("General Dynamics", "GD"), ("General Electric", "GE"), ("General Mills", "GIS"),
        ("General Motors", "GM"), ("Gilead Sciences", "GILD"), ("Goldman Sachs", "GS"), ("Halliburton", "HAL"),
        ("Hewlett Packard Enterprise", "HPE"), ("Home Depot", "HD"), ("Honeywell International", "HON"), ("HP", "HPQ"),
        ("Humana", "HUM"), ("IBM", "IBM"), ("Illinois Tool Works", "ITW"), ("Intel", "INTC"), ("International Paper", "IP"),
        ("Intuit", "INTU"), ("Johnson & Johnson", "JNJ"), ("JPMorgan Chase", "JPM"), ("Kellogg", "K"), ("Kimberly-Clark", "KMB"),
        ("Kohl's", "KSS"), ("Kraft Heinz", "KHC"), ("Kroger", "KR"), ("Lockheed Martin", "LMT"), ("Lowe's", "LOW"),
        ("Macy's", "M"), ("Marathon Oil", "MRO"), ("Marriott International", "MAR"), ("Mastercard", "MA"), ("McDonald's", "MCD"),
        ("Medtronic", "MDT"), ("Merck", "MRK"), ("Meta Platforms (Facebook)", "META"), ("MetLife", "MET"), ("Microsoft", "MSFT"),
        ("Mondelez International", "MDLZ"), ("Morgan Stanley", "MS"), ("Motorola Solutions", "MSI"), ("Netflix", "NFLX"),
        ("Nike", "NKE"), ("Northrop Grumman", "NOC"), ("NVIDIA", "NVDA"), ("Oracle", "ORCL"), ("PepsiCo", "PEP"),
        ("Pfizer", "PFE"), ("Philip Morris International", "PM"), ("Phillips 66", "PSX"), ("PNC Financial Services", "PNC"),
        ("PPG Industries", "PPG"), ("Procter & Gamble", "PG"), ("Qualcomm", "QCOM"), ("Raytheon Technologies", "RTX"),
        ("Rockwell Automation", "ROK"), ("Ross Stores", "ROST"), ("Schlumberger", "SLB"), ("Starbucks", "SBUX"),
        ("State Street", "STT"), ("Sysco", "SYY"), ("Target", "TGT"), ("Texas Instruments", "TXN"), ("Thermo Fisher Scientific", "TMO"),
        ("TJX Companies", "TJX"), ("T-Mobile US", "TMUS"), ("Union Pacific", "UNP"), ("United Airlines Holdings", "UAL"),
        ("United Parcel Service", "UPS"), ("United Technologies", "UTX"), ("UnitedHealth Group", "UNH"), ("US Bancorp", "USB"),
        ("Valero Energy", "VLO"), ("Verizon Communications", "VZ"), ("Visa", "V"), ("Walgreens Boots Alliance", "WBA"), ("Walmart", "WMT"), ("Walt Disney", "DIS"),
        ("Wells Fargo", "WFC"), ("Western Digital", "WDC"), ("Weyerhaeuser", "WY"), ("Whirlpool", "WHR"), ("Williams Companies", "WMB"), ("Xcel Energy", "XEL"),
        ("Xilinx", "XLNX"), ("Yum! Brands", "YUM"), ("Zimmer Biomet", "ZBH") 
            ]

class StockPredictor:
    def __init__(self, data):
        self.data = data
        self.model = None

    def preprocess_data(self):
        self.data = self.data.reset_index()
        self.data = self.data.rename(columns={'Date': 'ds', 'Close': 'y'})
        
        # Add technical indicators
        self.data['SMA_20'] = SMAIndicator(close=self.data['y'], window=20).sma_indicator()
        self.data['EMA_20'] = EMAIndicator(close=self.data['y'], window=20).ema_indicator()
        self.data['RSI'] = RSIIndicator(close=self.data['y'], window=14).rsi()
        bb = BollingerBands(close=self.data['y'], window=20, window_dev=2)
        self.data['BB_high'] = bb.bollinger_hband()
        self.data['BB_low'] = bb.bollinger_lband()
        
        # Add lagged features
        self.data['lag_1'] = self.data['y'].shift(1)
        self.data['lag_7'] = self.data['y'].shift(7)
        
        # Add rolling statistics
        self.data['rolling_mean_7'] = self.data['y'].rolling(window=7).mean()
        self.data['rolling_std_7'] = self.data['y'].rolling(window=7).std()
        
        # Handle NaN values
        self.data = self.data.dropna()

    def train_model(self):
        try:
            self.model = Prophet(
                changepoint_prior_scale=0.05,
                seasonality_prior_scale=10,
                holidays_prior_scale=10,
                daily_seasonality=True,
                weekly_seasonality=True,
                yearly_seasonality=True
            )
            
            # Add additional regressors
            for column in ['SMA_20', 'EMA_20', 'RSI', 'BB_high', 'BB_low', 'lag_1', 'lag_7', 'rolling_mean_7', 'rolling_std_7']:
                self.model.add_regressor(column)
            
            self.model.fit(self.data)
            return True
        except Exception as e:
            print(f"Error training Prophet model: {str(e)}")
            return False

    def predict(self, days=30):
        try:
            future = self.model.make_future_dataframe(periods=days)
            
            # Add regressor values for future dates
            for column in ['SMA_20', 'EMA_20', 'RSI', 'BB_high', 'BB_low', 'lag_1', 'lag_7', 'rolling_mean_7', 'rolling_std_7']:
                future[column] = self.data[column].iloc[-1]  # Use last known value
            
            forecast = self.model.predict(future)
            
            # Calculate components
            forecast['trend'] = forecast['trend']
            forecast['yearly'] = forecast['yearly'] if 'yearly' in forecast.columns else 0
            forecast['weekly'] = forecast['weekly'] if 'weekly' in forecast.columns else 0
            forecast['daily'] = forecast['daily'] if 'daily' in forecast.columns else 0
            
            return forecast
        except Exception as e:
            print(f"Error predicting with Prophet model: {str(e)}")
            return None

    def evaluate_model(self, test_data):
        predictions = self.predict(days=len(test_data))
        
        if predictions is None:
            return None, None, None

        actual = test_data['Close'].values
        predicted = predictions['yhat'].values[-len(test_data):]

        mse = mean_squared_error(actual, predicted)
        mape = mean_absolute_percentage_error(actual, predicted)
        rmse = np.sqrt(mse)

        return mse, mape, rmse

    def cross_validate_model(self, n_splits=5):
        tscv = TimeSeriesSplit(n_splits=n_splits)
        cv_results = []

        for train_index, test_index in tscv.split(self.data):
            train_data = self.data.iloc[train_index]
            test_data = self.data.iloc[test_index]

            # Train the model
            model = Prophet()
            model.fit(train_data)

            # Make predictions
            future = model.make_future_dataframe(periods=len(test_data))
            forecast = model.predict(future)

            # Calculate metrics
            y_true = test_data['y'].values
            y_pred = forecast['yhat'].tail(len(test_data)).values

            mse = mean_squared_error(y_true, y_pred)
            rmse = np.sqrt(mse)
            mape = mean_absolute_percentage_error(y_true, y_pred)

            cv_results.append({
                'mse': mse,
                'rmse': rmse,
                'mape': mape
            })

        return pd.DataFrame(cv_results)

def fetch_stock_data(ticker):
    try:
        end_date = datetime.now()
        start_date = datetime(2000, 1, 1) 
        data = yf.download(ticker, start=start_date, end=end_date)
        return data
    except Exception as e:
        st.error(f"Error fetching data for {ticker}: {str(e)}")
        return None

def create_test_plot(train_data, test_data, predicted_data, company_name):
    fig = go.Figure()

    fig.add_trace(go.Scatter(
        x=train_data['ds'],
        y=train_data['y'],
        mode='lines',
        name='Training Data',
        line=dict(color='blue')
    ))

    fig.add_trace(go.Scatter(
        x=test_data['ds'],
        y=test_data['y'],
        mode='lines',
        name='Actual (Test) Data',
        line=dict(color='green')
    ))

    if predicted_data is not None:
        fig.add_trace(go.Scatter(
            x=test_data['ds'],  # Align predicted data with test data
            y=predicted_data['yhat'][-len(test_data):],
            mode='lines',
            name='Predicted Data',
            line=dict(color='red', dash='dash')
        ))

    fig.update_layout(
        title=f'{company_name} Stock Price Prediction (Test Model)',
        xaxis_title='Date',
        yaxis_title='Close Price',
        template='plotly_dark',
        hovermode='x unified',
        xaxis_rangeslider_visible=True,
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
    )
    return fig

def create_prediction_plot(data, predicted_data, company_name):
    fig = go.Figure()

    fig.add_trace(go.Scatter(
        x=data.index,
        y=data['Close'],
        mode='lines',
        name='Historical Data',
        line=dict(color='cyan')
    ))

    if predicted_data is not None:
        future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=len(predicted_data))
        fig.add_trace(go.Scatter(
            x=future_dates,
            y=predicted_data['yhat'],
            mode='lines',
            name='Predicted Data',
            line=dict(color='yellow')
        ))
        
        # Add prediction intervals
        fig.add_trace(go.Scatter(
            x=future_dates,
            y=predicted_data['yhat_upper'],
            mode='lines',
            line=dict(width=0),
            showlegend=False
        ))
        fig.add_trace(go.Scatter(
            x=future_dates,
            y=predicted_data['yhat_lower'],
            mode='lines',
            line=dict(width=0),
            fillcolor='rgba(255, 255, 0, 0.3)',
            fill='tonexty',
            name='Prediction Interval'
        ))

    fig.update_layout(
        title=f'{company_name} Stock Price Prediction',
        xaxis_title='Date',
        yaxis_title='Close Price',
        template='plotly_dark',
        hovermode='x unified',
        xaxis_rangeslider_visible=True,
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
    )
    return fig

def create_candlestick_plot(data, company_name):
    fig = go.Figure(data=[go.Candlestick(x=data.index,
                                         open=data['Open'],
                                         high=data['High'],
                                         low=data['Low'],
                                         close=data['Close'])])
    fig.update_layout(
        title=f'{company_name} Stock Price Candlestick Chart',
        xaxis_title='Date',
        yaxis_title='Price',
        template='plotly_dark',
        xaxis_rangeslider_visible=True
    )
    return fig

def fetch_news(company_name):
    try:
        url = f"https://news.google.com/rss/search?q={company_name}+stock&hl=en-US&gl=US&ceid=US:en"
        response = requests.get(url)
        soup = BeautifulSoup(response.content, features='xml')
        news_items = soup.findAll('item')
        
        news = []
        for item in news_items[:5]:
            news.append({
                'title': item.title.text,
                'link': item.link.text,
                'pubDate': item.pubDate.text
            })
        
        return news
    except Exception as e:
        st.error(f"Error fetching news: {str(e)}")
        return []

def get_table_download_link(df, filename, text):
    """Generates a link to download the given dataframe as a CSV file."""
    csv = df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    href = f'<a href="data:file/csv;base64,{b64}" download="{filename}">{text}</a>'
    return href


def main():
    st.set_page_config(page_title="Stock Price Predictor", layout="wide")
    st.title("Advanced Stock Price Predictor using Prophet")

    st.sidebar.title("Options")
    app_mode = st.sidebar.selectbox("Choose the app mode", ["Test Model", "Predict Stock Prices", "Explore Data"])

    if app_mode == "Test Model":
        test_model()
    elif app_mode == "Predict Stock Prices":
        predict_stock_prices()
    elif app_mode == "Explore Data":
        explore_data()

def test_model():
    st.header("Test Enhanced Prophet Model")

    col1, col2 = st.columns(2)

    with col1:
        company = st.selectbox("Select Company", [company for company, _ in COMPANIES])
        test_split = st.slider("Test Data Split", 0.1, 0.5, 0.2, 0.05)

    if st.button("Train and Test Model"):
        with st.spinner("Fetching data and training model..."):
            company_name, ticker = next((name, symbol) for name, symbol in COMPANIES if name == company)

            data = fetch_stock_data(ticker)

            if data is not None:
                st.subheader("Stock Data Information")
                st.write(data.info())
                st.write(data.describe())
                
                # Display interactive dataframe
                st.subheader("Stock Data Preview")
                st.dataframe(data.head(100), use_container_width=True)
                
                # Provide download link for full dataset
                st.markdown(get_table_download_link(data, f"{ticker}_stock_data.csv", "Download full stock data CSV"), unsafe_allow_html=True)

                split_index = int(len(data) * (1 - test_split))
                train_data = data.iloc[:split_index]
                test_data = data.iloc[split_index:]

                predictor = StockPredictor(train_data)
                predictor.preprocess_data()
                if predictor.train_model():
                    test_pred = predictor.predict(days=len(test_data))

                    if test_pred is not None:
                        mse, mape, rmse = predictor.evaluate_model(test_data)
                        
                        if mse is not None and mape is not None and rmse is not None:
                            accuracy = 100 - mape * 100

                            st.subheader("Model Performance")
                            st.metric("Prediction Accuracy", f"{accuracy:.2f}%")
                            st.metric("Mean Squared Error", f"{mse:.4f}")
                            st.metric("Root Mean Squared Error", f"{rmse:.4f}")

                            plot = create_test_plot(predictor.data, test_data.reset_index().rename(columns={'Date': 'ds', 'Close': 'y'}), test_pred, company_name)
                            st.plotly_chart(plot, use_container_width=True)

                            # Cross-validation results
                            st.subheader("Cross-Validation Results")
                            cv_results = predictor.cross_validate_model()
                            
                            # Display interactive dataframe
                            st.dataframe(cv_results, use_container_width=True)
                            
                            # Provide download link for cross-validation results
                            st.markdown(get_table_download_link(cv_results, f"{ticker}_cv_results.csv", "Download cross-validation results CSV"), unsafe_allow_html=True)

                            # Calculate and display average metrics
                            avg_mse = cv_results['mse'].mean()
                            avg_rmse = cv_results['rmse'].mean()
                            avg_mape = cv_results['mape'].mean()
                            
                            st.write(f"Average MSE: {avg_mse:.4f}")
                            st.write(f"Average RMSE: {avg_rmse:.4f}")
                            st.write(f"Average MAPE: {avg_mape:.4f}")

                            # Display predictions
                            st.subheader("Predictions")
                            predictions_df = pd.DataFrame({
                                'Date': test_pred['ds'],
                                'Predicted': test_pred['yhat'],
                                'Lower Bound': test_pred['yhat_lower'],
                                'Upper Bound': test_pred['yhat_upper']
                            })
                            st.dataframe(predictions_df, use_container_width=True)
                            
                            # Provide download link for predictions
                            st.markdown(get_table_download_link(predictions_df, f"{ticker}_predictions.csv", "Download predictions CSV"), unsafe_allow_html=True)

                        else:
                            st.error("Failed to evaluate the model. The evaluation metrics are None.")
                    else:
                        st.error("Failed to generate predictions. The predicted data is None.")
                else:
                    st.error("Failed to train the Prophet model. Please try a different dataset.")
            else:
                st.error("Failed to fetch stock data. Please try again.")

def predict_stock_prices():
    st.header("Predict Stock Prices with Enhanced Model")

    col1, col2 = st.columns(2)

    with col1:
        company = st.selectbox("Select Company", [company for company, _ in COMPANIES])
        days_to_predict = st.slider("Days to Predict", 1, 365, 30)

    if st.button("Predict Stock Prices"):
        with st.spinner("Fetching data and making predictions..."):
            company_name, ticker = next((name, symbol) for name, symbol in COMPANIES if name == company)

            data = fetch_stock_data(ticker)

            if data is not None:
                st.subheader("Stock Data Information")
                st.write(data.info())
                st.write(data.describe())
                st.dataframe(data.head())

                predictor = StockPredictor(data)
                predictor.preprocess_data()
                if predictor.train_model():
                    predictions = predictor.predict(days=days_to_predict)

                    if predictions is not None:
                        # Create prediction plot
                        fig = go.Figure()

                        fig.add_trace(go.Scatter(
                            x=data.index,
                            y=data['Close'],
                            mode='lines',
                            name='Historical Data',
                            line=dict(color='cyan')
                        ))

                        future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=days_to_predict)
                        fig.add_trace(go.Scatter(
                            x=future_dates,
                            y=predictions['yhat'].tail(days_to_predict),
                            mode='lines',
                            name='Predicted Data',
                            line=dict(color='yellow')
                        ))

                        fig.add_trace(go.Scatter(
                            x=future_dates,
                            y=predictions['yhat_upper'].tail(days_to_predict),
                            mode='lines',
                            line=dict(width=0),
                            showlegend=False
                        ))

                        fig.add_trace(go.Scatter(
                            x=future_dates,
                            y=predictions['yhat_lower'].tail(days_to_predict),
                            mode='lines',
                            line=dict(width=0),
                            fillcolor='rgba(255, 255, 0, 0.3)',
                            fill='tonexty',
                            name='Prediction Interval'
                        ))

                        fig.update_layout(
                            title=f'{company_name} Stock Price Prediction',
                            xaxis_title='Date',
                            yaxis_title='Close Price',
                            template='plotly_dark',
                            hovermode='x unified',
                            xaxis_rangeslider_visible=True,
                            legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
                        )

                        st.plotly_chart(fig, use_container_width=True)

                        # Create forecast components plot using Plotly
                        # Instead of using predict_components, we'll extract the components from the predictions DataFrame
                        components = predictions[['trend', 'yearly', 'weekly', 'daily']]
                        n_components = len(components.columns)
                        
                        fig_components = make_subplots(rows=n_components, cols=1, 
                                                       subplot_titles=components.columns)
                        
                        for i, component in enumerate(components.columns, start=1):
                            fig_components.add_trace(
                                go.Scatter(x=predictions['ds'], y=components[component], 
                                           mode='lines', name=component),
                                row=i, col=1
                            )
                        
                        fig_components.update_layout(height=300*n_components, 
                                                     title_text="Forecast Components",
                                                     showlegend=False)
                        
                        st.plotly_chart(fig_components, use_container_width=True)

                        st.subheader("Predicted Prices")
                        pred_df = predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_to_predict)
                        pred_df.columns = ['Date', 'Predicted Price', 'Lower Bound', 'Upper Bound']
                        st.dataframe(pred_df)

                        # Provide download link for predictions
                        st.markdown(get_table_download_link(pred_df, f"{ticker}_predictions.csv", "Download predictions CSV"), unsafe_allow_html=True)

                        news = fetch_news(company_name)
                        st.subheader("Latest News")
                        for item in news:
                            st.markdown(f"[{item['title']}]({item['link']}) ({item['pubDate']})")
                    else:
                        st.error("Failed to generate predictions. The predicted data is None.")
                else:
                    st.error("Failed to train the Prophet model. Please try a different dataset.")
            else:
                st.error("Failed to fetch stock data. Please try again.")

def calculate_bollinger_bands(data, window=20, num_std=2):
    rolling_mean = data.rolling(window=window).mean()
    rolling_std = data.rolling(window=window).std()
    upper_band = rolling_mean + (rolling_std * num_std)
    lower_band = rolling_mean - (rolling_std * num_std)
    return upper_band, lower_band

def calculate_rsi(data, window=14):
    delta = data.diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
    rs = gain / loss
    return 100 - (100 / (1 + rs))

def explore_data():
    st.header("Explore Stock Data")

    col1, col2 = st.columns(2)
    
    with col1:
        company = st.selectbox("Select Company", [company for company, _ in COMPANIES])
    
    with col2:
        period = st.selectbox("Select Time Period", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "max"])

    company_name, ticker = next((name, symbol) for name, symbol in COMPANIES if name == company)

    if st.button("Explore Data"):
        with st.spinner("Fetching and analyzing data..."):
            try:
                data = yf.download(ticker, period=period)

                if data is not None and not data.empty:
                    st.subheader(f"{company_name} Stock Data")
                    
                    # Create tabs for different visualizations
                    tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
                        "Price History", "OHLC", "Technical Indicators", 
                        "Volume Analysis", "Returns Distribution", "Moving Averages"
                    ])
                    
                    with tab1:
                        st.markdown("""
                        ### Price History
                        This chart shows the historical closing prices of the stock over time. 
                        
                        **How to use**: 
                        - Use the slider at the bottom to zoom in on specific time periods.
                        - Hover over the line to see exact prices at different dates.
                        
                        **How it helps**: 
                        - Identify long-term trends in the stock's price.
                        - Spot key price levels and potential support/resistance areas.
                        - Understand the overall price movement and volatility of the stock.
                        """)
                        
                        fig = go.Figure()
                    fig.add_trace(go.Scatter(x=data.index, y=data['Open'], mode='lines', name='Open'))
                    fig.add_trace(go.Scatter(x=data.index, y=data['High'], mode='lines', name='High'))
                    fig.add_trace(go.Scatter(x=data.index, y=data['Low'], mode='lines', name='Low'))
                    fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close'))
                    
                    # Add rolling mean and standard deviation
                    data['Rolling_Mean'] = data['Close'].rolling(window=20).mean()
                    data['Rolling_Std'] = data['Close'].rolling(window=20).std()
                    fig.add_trace(go.Scatter(x=data.index, y=data['Rolling_Mean'], mode='lines', name='20-day Rolling Mean', line=dict(dash='dash')))
                    fig.add_trace(go.Scatter(x=data.index, y=data['Rolling_Std'], mode='lines', name='20-day Rolling Std', line=dict(dash='dot')))
                    
                    fig.update_layout(title=f"{company_name} Stock Price History",
                                      xaxis_title="Date",
                                      yaxis_title="Price",
                                      hovermode="x unified",
                                      template="plotly_dark")
                    st.plotly_chart(fig, use_container_width=True)
                    with tab2:
                        st.markdown("""
                        ### OHLC (Open-High-Low-Close) Chart
                        This candlestick chart shows the opening, high, low, and closing prices for each trading day.
                        
                        **How to use**:
                        - Green candles indicate days where the closing price was higher than the opening price.
                        - Red candles indicate days where the closing price was lower than the opening price.
                        - The thin lines (wicks) show the high and low prices for the day.
                        
                        **How it helps**:
                        - Identify daily price movements and volatility.
                        - Spot potential reversal patterns or trends.
                        - Understand the intraday price action of the stock.
                        """)
                        
                        ohlc_fig = go.Figure(data=[go.Candlestick(x=data.index,
                                                                  open=data['Open'],
                                                                  high=data['High'],
                                                                  low=data['Low'],
                                                                  close=data['Close'])])
                        ohlc_fig.update_layout(title=f"{company_name} OHLC Chart",
                                               xaxis_title="Date",
                                               yaxis_title="Price",
                                               template="plotly_dark",
                                               xaxis_rangeslider_visible=False)
                        st.plotly_chart(ohlc_fig, use_container_width=True)

                    with tab3:
                        st.markdown("""
                        ### Technical Indicators
                        This chart displays various technical indicators alongside the stock price.
                        
                        **How to use**:
                        - The top chart shows the stock price with SMA (Simple Moving Average), EMA (Exponential Moving Average), and Bollinger Bands.
                        - The bottom chart shows the RSI (Relative Strength Index).
                        
                        **How it helps**:
                        - Identify potential buy/sell signals using moving average crossovers.
                        - Spot overbought or oversold conditions using RSI.
                        - Understand price volatility using Bollinger Bands.
                        - Combine multiple indicators for more robust trading decisions.
                        """)
                        
                        data['SMA_20'] = data['Close'].rolling(window=20).mean()
                        data['EMA_20'] = data['Close'].ewm(span=20, adjust=False).mean()
                        data['Upper_BB'], data['Lower_BB'] = calculate_bollinger_bands(data['Close'])
                        data['RSI'] = calculate_rsi(data['Close'])

                        fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                                            vertical_spacing=0.03, 
                                            subplot_titles=("Price and Indicators", "RSI"),
                                            row_heights=[0.7, 0.3])

                        fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close'), row=1, col=1)
                        fig.add_trace(go.Scatter(x=data.index, y=data['SMA_20'], mode='lines', name='SMA 20'), row=1, col=1)
                        fig.add_trace(go.Scatter(x=data.index, y=data['EMA_20'], mode='lines', name='EMA 20'), row=1, col=1)
                        fig.add_trace(go.Scatter(x=data.index, y=data['Upper_BB'], mode='lines', name='Upper BB'), row=1, col=1)
                        fig.add_trace(go.Scatter(x=data.index, y=data['Lower_BB'], mode='lines', name='Lower BB'), row=1, col=1)

                        fig.add_trace(go.Scatter(x=data.index, y=data['RSI'], mode='lines', name='RSI'), row=2, col=1)
                        fig.add_hline(y=70, line_dash="dash", line_color="red", row=2, col=1)
                        fig.add_hline(y=30, line_dash="dash", line_color="green", row=2, col=1)

                        fig.update_layout(height=800, title_text=f"{company_name} Technical Indicators",
                                          hovermode="x unified", template="plotly_dark")
                        fig.update_xaxes(rangeslider_visible=False, row=2, col=1)
                        fig.update_yaxes(title_text="Price", row=1, col=1)
                        fig.update_yaxes(title_text="RSI", row=2, col=1)

                        st.plotly_chart(fig, use_container_width=True)

                    with tab4:
                        st.markdown("""
                        ### Volume Analysis
                        This chart shows the trading volume alongside the stock price.
                        
                        **How to use**:
                        - The top chart displays the daily trading volume.
                        - The bottom chart shows the corresponding stock price.
                        
                        **How it helps**:
                        - Identify periods of high and low trading activity.
                        - Confirm price trends (higher volume often validates price movements).
                        - Spot potential reversals (e.g., high volume at price extremes).
                        - Understand the liquidity of the stock.
                        """)
                        
                        fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                                            vertical_spacing=0.03, 
                                            subplot_titles=("Volume", "Price"),
                                            row_heights=[0.7, 0.3])

                        fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume'), row=1, col=1)
                        fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price'), row=2, col=1)

                        fig.update_layout(height=600, title_text=f"{company_name} Volume Analysis",
                                          hovermode="x unified", template="plotly_dark")
                        fig.update_xaxes(rangeslider_visible=False, row=2, col=1)
                        fig.update_yaxes(title_text="Volume", row=1, col=1)
                        fig.update_yaxes(title_text="Price", row=2, col=1)

                        st.plotly_chart(fig, use_container_width=True)

                    with tab5:
                        st.markdown("""
                        ### Returns Distribution
                        This histogram shows the distribution of daily returns for the stock.
                        
                        **How to use**:
                        - The x-axis represents the daily return percentages.
                        - The y-axis shows the frequency of each return range.
                        
                        **How it helps**:
                        - Understand the volatility and risk profile of the stock.
                        - Identify the most common daily return ranges.
                        - Spot any skewness or unusual patterns in returns.
                        - Compare the stock's return distribution to a normal distribution for risk assessment.
                        """)
                        
                        returns = data['Close'].pct_change().dropna()
                        fig = go.Figure(data=[go.Histogram(x=returns, nbinsx=50)])
                        fig.update_layout(title=f"{company_name} Returns Distribution",
                                          xaxis_title="Daily Returns",
                                          yaxis_title="Frequency",
                                          template="plotly_dark")
                        st.plotly_chart(fig, use_container_width=True)

                    with tab6:
                        st.markdown("""
                        ### Moving Averages
                        This chart shows the stock price along with 50-day and 200-day Simple Moving Averages (SMA).
                        
                        **How to use**:
                        - The blue line represents the stock's closing price.
                        - The orange line is the 50-day SMA.
                        - The green line is the 200-day SMA.
                        
                        **How it helps**:
                        - Identify long-term trends in the stock price.
                        - Spot potential buy/sell signals when the price crosses above/below the moving averages.
                        - Recognize "Golden Cross" (50-day crosses above 200-day) and "Death Cross" (50-day crosses below 200-day) signals.
                        - Understand the overall momentum of the stock.
                        """)
                        
                        data['SMA_50'] = data['Close'].rolling(window=50).mean()
                        data['SMA_200'] = data['Close'].rolling(window=200).mean()

                        fig = go.Figure()
                        fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close'))
                        fig.add_trace(go.Scatter(x=data.index, y=data['SMA_50'], mode='lines', name='50-day SMA'))
                        fig.add_trace(go.Scatter(x=data.index, y=data['SMA_200'], mode='lines', name='200-day SMA'))

                        fig.update_layout(title=f"{company_name} Moving Averages",
                                          xaxis_title="Date",
                                          yaxis_title="Price",
                                          template="plotly_dark",
                                          hovermode="x unified")
                        st.plotly_chart(fig, use_container_width=True)

                    # Display key statistics
                    st.subheader("Key Statistics")
                    col1, col2, col3 = st.columns(3)
                    with col1:
                        st.metric("Current Price", f"${data['Close'].iloc[-1]:.2f}")
                        st.metric("52 Week High", f"${data['High'].max():.2f}")
                    with col2:
                        st.metric("Volume", f"{data['Volume'].iloc[-1]:,}")
                        st.metric("52 Week Low", f"${data['Low'].min():.2f}")
                    with col3:
                        returns = (data['Close'].pct_change() * 100).dropna()
                        st.metric("Avg Daily Return", f"{returns.mean():.2f}%")
                        st.metric("Return Volatility", f"{returns.std():.2f}%")

                    # Display news
                    st.subheader("Latest News")
                    news = fetch_news(company_name)
                    for item in news:
                        st.markdown(f"[{item['title']}]({item['link']}) ({item['pubDate']})")
                    
                else:
                    st.error("Failed to fetch data. Please try again.")

            except Exception as e:
                st.error(f"An error occurred: {str(e)}. Please try a different time period or check your internet connection.")

if __name__ == "__main__":
    main()