File size: 41,333 Bytes
404b51f
 
382de1d
 
404b51f
 
f7f78d1
 
 
404b51f
f7f78d1
 
 
404b51f
f7f78d1
 
382de1d
 
f7f78d1
382de1d
 
f7f78d1
 
 
 
 
382de1d
f7f78d1
 
 
 
 
 
 
 
 
 
 
 
 
382de1d
 
 
 
 
 
 
f7f78d1
 
 
e0f3bb6
893fc6c
382de1d
404b51f
 
65bf4f9
f7f78d1
 
 
 
 
382de1d
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
f7f78d1
382de1d
f7f78d1
 
 
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404b51f
f7f78d1
382de1d
f7f78d1
382de1d
f7f78d1
382de1d
 
 
 
 
 
f7f78d1
 
 
 
382de1d
 
 
 
 
f7f78d1
 
 
 
382de1d
 
 
f7f78d1
 
382de1d
f7f78d1
404b51f
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
f7f78d1
 
404b51f
f7f78d1
 
404b51f
382de1d
 
 
 
f7f78d1
 
 
 
382de1d
f7f78d1
 
 
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
f7f78d1
382de1d
f7f78d1
 
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994edc7
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40b041a
994edc7
 
 
 
 
 
 
 
 
 
 
 
40b041a
 
 
 
994edc7
40b041a
 
 
 
994edc7
 
 
 
40b041a
 
994edc7
40b041a
994edc7
40b041a
 
 
994edc7
40b041a
 
994edc7
40b041a
994edc7
40b041a
994edc7
40b041a
994edc7
 
 
 
 
 
 
 
 
40b041a
994edc7
 
 
 
382de1d
 
 
 
 
994edc7
382de1d
 
 
 
 
 
 
 
f7f78d1
382de1d
994edc7
382de1d
40b041a
382de1d
994edc7
382de1d
f7f78d1
 
382de1d
 
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f78d1
 
382de1d
 
 
 
 
 
404b51f
382de1d
f7f78d1
382de1d
 
 
 
 
 
f7f78d1
 
404b51f
f7f78d1
 
 
 
 
 
382de1d
 
 
 
 
f7f78d1
 
 
 
 
382de1d
 
f7f78d1
 
 
382de1d
f7f78d1
382de1d
 
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
f7f78d1
382de1d
 
f7f78d1
 
 
382de1d
 
 
 
 
 
 
 
 
f7f78d1
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404b51f
f7f78d1
382de1d
404b51f
f7f78d1
 
 
 
 
404b51f
382de1d
 
 
 
 
f7f78d1
 
382de1d
 
f7f78d1
404b51f
382de1d
f7f78d1
382de1d
 
404b51f
 
f7f78d1
 
382de1d
f7f78d1
 
893fc6c
404b51f
382de1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f78d1
893fc6c
f7f78d1
 
 
 
382de1d
f7f78d1
893fc6c
382de1d
 
 
 
 
 
 
 
404b51f
893fc6c
382de1d
404b51f
382de1d
893fc6c
404b51f
893fc6c
404b51f
382de1d
 
404b51f
382de1d
404b51f
382de1d
893fc6c
404b51f
382de1d
404b51f
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
#!/usr/bin/env python3
"""
Premium Trading Dashboard - Full Enhanced Version
Beautiful dashboard with sentiment analysis, Reddit integration, and advanced features
"""

import os
import sys
import pandas as pd
import gradio as gr
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timedelta, timezone
import logging
import requests
import time
import json
import re
import nltk
import feedparser
from urllib.parse import quote

# Import dependencies with fallback
try:
    from alpaca.trading.client import TradingClient
    from alpaca.trading.requests import GetOrdersRequest, GetPortfolioHistoryRequest
    from alpaca.trading.enums import OrderStatus, OrderSide
    from alpaca.data.timeframe import TimeFrame
    from alpaca.data.historical import StockHistoricalDataClient
    ALPACA_AVAILABLE = True
except ImportError:
    ALPACA_AVAILABLE = False

try:
    from textblob import TextBlob
    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    SENTIMENT_AVAILABLE = True
except ImportError:
    SENTIMENT_AVAILABLE = False

try:
    import yfinance as yf
    YF_AVAILABLE = True
except ImportError:
    YF_AVAILABLE = False

# API Keys and Configuration
API_KEY = os.getenv('ALPACA_API_KEY', 'PK2FD9B2S86LHR7ZBHG1')
SECRET_KEY = os.getenv('ALPACA_SECRET_KEY', 'QPmGPDgbPArvHv6cldBXc7uWddapYcIAnBhtkuBW')
VM_API_URL = os.getenv('VM_API_URL', 'http://34.56.193.18:8090')

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

logger.info("πŸš€ Starting Premium Trading Dashboard - Full Enhanced Version with 1-Hour P&L")

# Download NLTK data
try:
    nltk.download('punkt', quiet=True)
    nltk.download('vader_lexicon', quiet=True)
    nltk.download('brown', quiet=True)
    logger.info("βœ… NLTK data downloaded")
except Exception as e:
    logger.warning(f"⚠️ NLTK download failed: {e}")

# Initialize sentiment analyzers
sentiment_analyzer = None
if SENTIMENT_AVAILABLE:
    try:
        sentiment_analyzer = SentimentIntensityAnalyzer()
        logger.info("βœ… VADER sentiment analyzer initialized")
    except Exception as e:
        logger.warning(f"⚠️ Sentiment analyzer failed: {e}")

# Initialize Alpaca clients
trading_client = None
data_client = None
if ALPACA_AVAILABLE:
    try:
        trading_client = TradingClient(api_key=API_KEY, secret_key=SECRET_KEY)
        data_client = StockHistoricalDataClient(API_KEY, SECRET_KEY)
        logger.info("βœ… Alpaca clients initialized")
    except Exception as e:
        logger.warning(f"⚠️ Alpaca clients failed: {e}")

# HTTP headers for Reddit API
headers = {
    'User-Agent': 'TradingBot/1.0 (by u/TradingBot)'
}

# Color scheme
COLORS = {
    'primary': '#0070f3',
    'success': '#00d647', 
    'error': '#ff0080',
    'warning': '#f5a623',
    'neutral': '#8b949e'
}

def fetch_from_vm(endpoint, default_value=None):
    """Fetch data from VM API server with fallback"""
    try:
        response = requests.get(f"{VM_API_URL}/api/{endpoint}", timeout=10)
        if response.status_code == 200:
            return response.json()
        else:
            logger.warning(f"VM API returned status {response.status_code}")
            return default_value
    except Exception as e:
        logger.warning(f"VM API error: {e}")
        return default_value

def get_account_info():
    """Get comprehensive account information"""
    if not trading_client:
        # Return demo data
        return {
            'portfolio_value': 125000.00,
            'buying_power': 31250.00,
            'cash': 31250.00,
            'day_change': 2750.50,
            'equity': 125000.00,
            'day_change_percent': 2.25
        }
    
    try:
        account = trading_client.get_account()
        last_equity = float(account.last_equity) if account.last_equity else float(account.equity)
        current_equity = float(account.equity)
        day_change = current_equity - last_equity
        day_change_percent = (day_change / last_equity * 100) if last_equity > 0 else 0
        
        return {
            'portfolio_value': float(account.portfolio_value),
            'buying_power': float(account.buying_power),
            'cash': float(account.cash),
            'day_change': day_change,
            'equity': current_equity,
            'day_change_percent': day_change_percent
        }
    except Exception as e:
        logger.error(f"Account info error: {e}")
        return {'error': str(e)}

def get_order_history(limit=50):
    """Get recent order history"""
    if not trading_client:
        return []
    
    try:
        request = GetOrdersRequest(
            status='all',
            limit=limit
        )
        orders = trading_client.get_orders(filter=request)
        
        order_data = []
        for order in orders:
            order_data.append({
                'symbol': order.symbol,
                'side': order.side.value if hasattr(order.side, 'value') else str(order.side),
                'qty': float(order.qty) if order.qty else 0,
                'filled_qty': float(order.filled_qty) if order.filled_qty else 0,
                'status': order.status.value if hasattr(order.status, 'value') else str(order.status),
                'submitted_at': order.submitted_at.isoformat() if order.submitted_at else None,
                'filled_at': order.filled_at.isoformat() if order.filled_at else None,
                'filled_avg_price': float(order.filled_avg_price) if order.filled_avg_price else None
            })
        
        return order_data
    except Exception as e:
        logger.error(f"Order history error: {e}")
        return []

def get_reddit_posts(symbol, start_time, cutoff_time):
    """Enhanced Reddit search with multiple strategies"""
    logger.info(f"πŸ” Searching Reddit for {symbol}...")
    
    reddit_posts = []
    subreddits = ['wallstreetbets', 'stocks', 'investing', 'SecurityAnalysis', 'ValueInvesting']
    search_terms = [symbol, f'{symbol} stock', f'{symbol} IPO', f'${symbol}', f'{symbol} earnings']
    
    for subreddit in subreddits:
        for search_term in search_terms:
            try:
                url = f"https://www.reddit.com/r/{subreddit}/search.json"
                params = {
                    'q': search_term,
                    'restrict_sr': 'true',
                    'limit': 10,
                    't': 'all',
                    'sort': 'relevance'
                }
                
                response = requests.get(url, params=params, headers=headers, timeout=10)
                if response.status_code == 200:
                    data = response.json()
                    posts_found = len(data.get('data', {}).get('children', []))
                    logger.info(f"Reddit: r/{subreddit} + '{search_term}' found {posts_found} posts")
                    
                    for post in data.get('data', {}).get('children', []):
                        post_data = post.get('data', {})
                        
                        if not post_data.get('title'):
                            continue
                        
                        # Filter by time window
                        post_time = datetime.fromtimestamp(post_data.get('created_utc', 0), tz=timezone.utc)
                        if not (start_time <= post_time <= cutoff_time):
                            continue
                        
                        # Check relevance
                        title_lower = post_data.get('title', '').lower()
                        body_lower = post_data.get('selftext', '').lower()
                        symbol_lower = symbol.lower()
                        
                        if symbol_lower not in title_lower and symbol_lower not in body_lower:
                            continue
                        
                        # Remove duplicates
                        post_id = post_data.get('id')
                        if any(p.get('id') == post_id for p in reddit_posts):
                            continue
                        
                        reddit_posts.append({
                            'id': post_id,
                            'title': post_data.get('title', ''),
                            'selftext': post_data.get('selftext', ''),
                            'score': post_data.get('score', 0),
                            'num_comments': post_data.get('num_comments', 0),
                            'created_utc': post_data.get('created_utc', 0),
                            'subreddit': subreddit,
                            'search_term': search_term,
                            'url': f"https://reddit.com{post_data.get('permalink', '')}"
                        })
                
                time.sleep(0.1)  # Rate limiting
                
            except Exception as e:
                logger.warning(f"Reddit search error for r/{subreddit}: {e}")
                continue
    
    logger.info(f"πŸ“Š Total Reddit posts found for {symbol}: {len(reddit_posts)}")
    return reddit_posts

def get_google_news(symbol, start_time, cutoff_time):
    """Get Google News articles for symbol"""
    logger.info(f"πŸ“° Searching Google News for {symbol}...")
    
    try:
        # Build search query
        search_queries = [
            f'{symbol} stock',
            f'{symbol} IPO',
            f'{symbol} earnings',
            f'{symbol} company'
        ]
        
        all_articles = []
        
        for query in search_queries:
            try:
                encoded_query = quote(query)
                url = f"https://news.google.com/rss/search?q={encoded_query}&hl=en&gl=US&ceid=US:en"
                
                feed = feedparser.parse(url)
                
                for entry in feed.entries:
                    # Parse publication date
                    try:
                        pub_date = datetime(*entry.published_parsed[:6], tzinfo=timezone.utc)
                        if not (start_time <= pub_date <= cutoff_time):
                            continue
                    except:
                        continue
                    
                    # Check relevance
                    title_lower = entry.title.lower()
                    summary_lower = getattr(entry, 'summary', '').lower()
                    symbol_lower = symbol.lower()
                    
                    if symbol_lower not in title_lower and symbol_lower not in summary_lower:
                        continue
                    
                    article = {
                        'title': entry.title,
                        'summary': getattr(entry, 'summary', ''),
                        'published': entry.published,
                        'published_parsed': pub_date.isoformat(),
                        'link': entry.link,
                        'source': getattr(entry, 'source', {}).get('title', 'Google News'),
                        'search_query': query
                    }
                    
                    # Remove duplicates
                    if not any(a.get('link') == article['link'] for a in all_articles):
                        all_articles.append(article)
                
                time.sleep(0.2)  # Rate limiting
                
            except Exception as e:
                logger.warning(f"Google News error for query '{query}': {e}")
                continue
        
        logger.info(f"πŸ“Š Total Google News articles found for {symbol}: {len(all_articles)}")
        return all_articles
        
    except Exception as e:
        logger.error(f"Google News search failed: {e}")
        return []

def analyze_sentiment(news_items):
    """Analyze sentiment of news items using VADER and TextBlob"""
    if not news_items or not SENTIMENT_AVAILABLE:
        return 0.0, 0.0, "Neutral", {'Reddit': [], 'Google News': []}
    
    logger.info(f"🧠 Analyzing sentiment for {len(news_items)} items...")
    
    sentiment_scores = []
    source_breakdown = {'Reddit': [], 'Google News': []}
    
    for item in news_items:
        try:
            # Determine text to analyze
            if 'title' in item and 'selftext' in item:  # Reddit post
                text = f"{item['title']} {item.get('selftext', '')}"
                source = 'Reddit'
                weight = max(1, item.get('score', 1) + item.get('num_comments', 0) * 0.5)
            else:  # News article
                text = f"{item['title']} {item.get('summary', '')}"
                source = 'Google News'
                weight = 1.0
            
            if not text.strip():
                continue
            
            # VADER sentiment
            vader_score = 0.0
            if sentiment_analyzer:
                vader_result = sentiment_analyzer.polarity_scores(text)
                vader_score = vader_result['compound']
            
            # TextBlob sentiment
            textblob_score = 0.0
            try:
                blob = TextBlob(text)
                textblob_score = blob.sentiment.polarity
            except:
                pass
            
            # Combined score
            combined_score = (vader_score + textblob_score) / 2
            weighted_score = combined_score * weight
            
            sentiment_scores.append(weighted_score)
            source_breakdown[source].append({
                'text': text[:200] + '...' if len(text) > 200 else text,
                'vader_score': vader_score,
                'textblob_score': textblob_score,
                'combined_score': combined_score,
                'weight': weight,
                'weighted_score': weighted_score
            })
            
        except Exception as e:
            logger.warning(f"Sentiment analysis error: {e}")
            continue
    
    if not sentiment_scores:
        return 0.0, 0.0, "Neutral", source_breakdown
    
    # Calculate average sentiment
    avg_sentiment = sum(sentiment_scores) / len(sentiment_scores)
    
    # Predict percentage change based on sentiment
    # Strong positive sentiment -> higher predicted gain
    # Strong negative sentiment -> higher predicted loss
    if avg_sentiment > 0.5:
        predicted_change = min(15.0, avg_sentiment * 20)  # Cap at 15%
        prediction_label = "Strong Buy"
    elif avg_sentiment > 0.2:
        predicted_change = avg_sentiment * 10
        prediction_label = "Buy"
    elif avg_sentiment > -0.2:
        predicted_change = avg_sentiment * 5
        prediction_label = "Hold"
    elif avg_sentiment > -0.5:
        predicted_change = avg_sentiment * 10
        prediction_label = "Sell"
    else:
        predicted_change = max(-15.0, avg_sentiment * 20)  # Cap at -15%
        prediction_label = "Strong Sell"
    
    logger.info(f"πŸ“Š Sentiment analysis complete: {avg_sentiment:.3f} -> {prediction_label} ({predicted_change:+.1f}%)")
    
    return avg_sentiment, predicted_change, prediction_label, source_breakdown

def get_pre_investment_news(symbol, investment_time, hours_before=12):
    """Get news from before investment time"""
    start_time = investment_time - timedelta(hours=hours_before)
    cutoff_time = investment_time - timedelta(minutes=30)  # 30 min buffer
    
    logger.info(f"πŸ“Š Getting pre-investment news for {symbol}")
    logger.info(f"   Time window: {start_time} to {cutoff_time}")
    
    # Get Reddit posts
    reddit_posts = get_reddit_posts(symbol, start_time, cutoff_time)
    
    # Get Google News
    google_news = get_google_news(symbol, start_time, cutoff_time)
    
    # Combine all news items
    all_news = reddit_posts + google_news
    
    logger.info(f"πŸ“Š Total news items: {len(all_news)} ({len(reddit_posts)} Reddit + {len(google_news)} News)")
    
    return all_news

def refresh_account_overview():
    """Refresh account overview with enhanced data"""
    logger.info("πŸ”„ Refreshing account overview...")
    info = get_account_info()
    
    if 'error' in info:
        return "Error", "Error", "Error", "Error", "Error"
    
    # Format with colors based on performance
    day_change_color = COLORS['success'] if info['day_change'] >= 0 else COLORS['error']
    day_change_formatted = f"<span style='color: {day_change_color}'>${info['day_change']:+,.2f} ({info.get('day_change_percent', 0):+.2f}%)</span>"
    
    return (
        f"${info['portfolio_value']:,.2f}",
        f"${info['buying_power']:,.2f}",
        f"${info['cash']:,.2f}",
        day_change_formatted,
        f"${info['equity']:,.2f}"
    )

def create_portfolio_chart():
    """Create enhanced portfolio performance chart"""
    logger.info("πŸ“ˆ Creating portfolio chart...")
    
    if not trading_client:
        # Demo data
        dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
        values = [100000 + i * 50 + (i % 30 - 15) * 200 for i in range(len(dates))]
        
        fig = go.Figure()
        fig.add_trace(go.Scatter(
            x=dates,
            y=values,
            mode='lines',
            name='Portfolio Value',
            line=dict(color=COLORS['primary'], width=2),
            fill='tonexty',
            fillcolor=f'rgba(0, 112, 243, 0.1)'
        ))
        
        fig.update_layout(
            title="Portfolio Performance (Demo Data)",
            xaxis_title="Date",
            yaxis_title="Portfolio Value ($)",
            hovermode='x unified',
            template='plotly_white'
        )
        
        return fig
    
    try:
        # Get portfolio history from Alpaca
        request = GetPortfolioHistoryRequest(
            period='1M',
            timeframe=TimeFrame.Day
        )
        portfolio_history = trading_client.get_portfolio_history(filter=request)
        
        if portfolio_history.equity:
            timestamps = [datetime.fromtimestamp(ts) for ts in portfolio_history.timestamp]
            equity_values = portfolio_history.equity
            
            fig = go.Figure()
            fig.add_trace(go.Scatter(
                x=timestamps,
                y=equity_values,
                mode='lines',
                name='Portfolio Value',
                line=dict(color=COLORS['primary'], width=2),
                fill='tonexty',
                fillcolor=f'rgba(0, 112, 243, 0.1)'
            ))
            
            fig.update_layout(
                title="Portfolio Performance (Last 30 Days)",
                xaxis_title="Date",
                yaxis_title="Portfolio Value ($)",
                hovermode='x unified',
                template='plotly_white'
            )
            
            return fig
    except Exception as e:
        logger.error(f"Portfolio chart error: {e}")
    
    # Fallback empty chart
    fig = go.Figure()
    fig.update_layout(title="Portfolio Chart (No Data Available)")
    return fig

def refresh_ipo_discoveries():
    """Get IPO discoveries from VM"""
    logger.info("πŸ”„ Refreshing IPO discoveries...")
    
    vm_data = fetch_from_vm('ipos', [])
    
    if not vm_data:
        return """
        <div style="padding: 2rem; text-align: center; background: #f8f9fa; border-radius: 8px; margin: 1rem 0;">
            <h3>πŸ” IPO Discovery System</h3>
            <p>No recent IPO discoveries available. The system continuously monitors for new tradeable securities.</p>
            <p><small>πŸ“‘ VM Connection Status: Offline</small></p>
        </div>
        """
    
    # Format IPO discoveries
    html_content = """
    <div style="background: white; border-radius: 8px; padding: 1rem; margin: 1rem 0;">
        <h3>🎯 Recent IPO Discoveries</h3>
        <table style="width: 100%; border-collapse: collapse; font-size: 0.9rem;">
            <thead>
                <tr style="background: #f8f9fa; border-bottom: 2px solid #dee2e6;">
                    <th style="padding: 12px 8px; text-align: left;">Symbol</th>
                    <th style="padding: 12px 8px; text-align: left;">Discovery Time</th>
                    <th style="padding: 12px 8px; text-align: left;">Type</th>
                    <th style="padding: 12px 8px; text-align: left;">Decision</th>
                </tr>
            </thead>
            <tbody>
    """
    
    for idx, ipo in enumerate(vm_data[:20]):  # Show last 20
        row_bg = "#f8f9fa" if idx % 2 == 0 else "white"
        
        symbol = ipo.get('symbol', 'N/A')
        discovery_time = ipo.get('discovery_time', 'N/A')
        asset_type = ipo.get('type', 'Unknown')
        decision = ipo.get('investment_decision', 'Pending')
        
        decision_color = COLORS['success'] if 'invested' in decision.lower() else COLORS['warning']
        
        html_content += f"""
                <tr style="background: {row_bg}; border-bottom: 1px solid #dee2e6;">
                    <td style="padding: 10px 8px; font-weight: bold;">{symbol}</td>
                    <td style="padding: 10px 8px;">{discovery_time}</td>
                    <td style="padding: 10px 8px;">{asset_type}</td>
                    <td style="padding: 10px 8px; color: {decision_color};">{decision}</td>
                </tr>
        """
    
    html_content += """
            </tbody>
        </table>
    </div>
    """
    
    return html_content

def refresh_investment_performance():
    """Get investment performance with sentiment analysis"""
    logger.info("πŸ”„ Refreshing investment performance with sentiment analysis...")
    
    orders = get_order_history()
    
    if not orders:
        return """
        <div style="padding: 2rem; text-align: center; background: #f8f9fa; border-radius: 8px; margin: 1rem 0;">
            <h3>πŸ’° Investment Performance</h3>
            <p>No trading history available yet.</p>
            <p><small>Start trading to see performance analytics with sentiment analysis!</small></p>
        </div>
        """
    
    # Group orders by symbol
    symbol_data = {}
    for order in orders:
        symbol = order['symbol']
        if symbol not in symbol_data:
            symbol_data[symbol] = []
        symbol_data[symbol].append(order)
    
    html_content = """
    <div style="background: white; border-radius: 8px; padding: 1rem; margin: 1rem 0;">
        <h3>πŸ“Š Investment Performance with Sentiment Analysis</h3>
        <table style="width: 100%; border-collapse: collapse; font-size: 0.85rem;">
            <thead>
                <tr style="background: #f8f9fa; border-bottom: 2px solid #dee2e6;">
                    <th style="padding: 10px 6px; text-align: left;">Symbol</th>
                    <th style="padding: 10px 6px; text-align: center;">Investment</th>
                    <th style="padding: 10px 6px; text-align: center;">1-Hour P&L</th>
                    <th style="padding: 10px 6px; text-align: center;">Sentiment</th>
                    <th style="padding: 10px 6px; text-align: center;">Prediction</th>
                    <th style="padding: 10px 6px; text-align: center;">Sources</th>
                </tr>
            </thead>
            <tbody>
    """
    
    for idx, (symbol, symbol_orders) in enumerate(list(symbol_data.items())[:15]):  # Limit to 15 for performance
        row_bg = "#f8f9fa" if idx % 2 == 0 else "white"
        
        # Calculate investment amount
        total_investment = sum(
            float(order.get('filled_avg_price', 0)) * float(order.get('filled_qty', 0))
            for order in symbol_orders
            if order.get('side') == 'buy' and order.get('status') == 'filled'
        )
        
        if total_investment == 0:
            continue
        
        # Get investment time (first buy order)
        buy_orders = [o for o in symbol_orders if o.get('side') == 'buy' and o.get('filled_at')]
        if not buy_orders:
            continue
        
        investment_time = datetime.fromisoformat(buy_orders[0]['filled_at'].replace('Z', '+00:00'))
        
        # Run sentiment analysis
        logger.info(f"🧠 Starting sentiment analysis for {symbol}...")
        try:
            news_items = get_pre_investment_news(symbol, investment_time, hours_before=12)
            avg_sentiment, predicted_change, prediction_label, source_breakdown = analyze_sentiment(news_items)
            
            sentiment_color = COLORS['success'] if avg_sentiment > 0.1 else COLORS['error'] if avg_sentiment < -0.1 else COLORS['neutral']
            prediction_color = COLORS['success'] if predicted_change > 0 else COLORS['error'] if predicted_change < 0 else COLORS['neutral']
            
            # Count sources
            reddit_count = len(source_breakdown.get('Reddit', []))
            news_count = len(source_breakdown.get('Google News', []))
            
        except Exception as e:
            logger.error(f"Sentiment analysis failed for {symbol}: {e}")
            avg_sentiment = 0.0
            predicted_change = 0.0
            prediction_label = "Error"
            sentiment_color = COLORS['neutral']
            prediction_color = COLORS['neutral']
            reddit_count = 0
            news_count = 0
        
        # Calculate IPO first-hour P&L using Yahoo Finance
        one_hour_pnl = 0.0
        pnl_percentage = 0.0
        try:
            if YF_AVAILABLE:
                # Get stock data for the investment day
                investment_date = investment_time.date()
                ticker = yf.Ticker(symbol)
                
                # Get minute-by-minute data for the investment day
                hist = ticker.history(period="1d", interval="1m", start=investment_date, end=investment_date + timedelta(days=1))
                
                if not hist.empty:
                    # Find IPO opening price and price 1 hour after IPO opening
                    # IPO opening = first available price of the day (market open)
                    ipo_open_price = hist.iloc[0]['Open']  # First price of the day
                    ipo_open_time = hist.index[0]
                    
                    # Find price exactly 1 hour after IPO opened
                    one_hour_after_ipo = ipo_open_time + timedelta(hours=1)
                    
                    # Find closest price to 1 hour after IPO opening
                    one_hour_price = None
                    one_hour_time_diff = float('inf')
                    
                    for timestamp, row in hist.iterrows():
                        time_diff = abs((timestamp - one_hour_after_ipo).total_seconds())
                        if time_diff < one_hour_time_diff and time_diff <= 30 * 60:  # Within 30 minutes
                            one_hour_price = row['Close']
                            one_hour_time_diff = time_diff
                    
                    if ipo_open_price and one_hour_price:
                        # Calculate shares that could be purchased with our investment
                        total_shares = total_investment / ipo_open_price if ipo_open_price > 0 else 0
                        
                        # Calculate P&L based on IPO first-hour price movement
                        price_change = one_hour_price - ipo_open_price
                        one_hour_pnl = price_change * total_shares
                        pnl_percentage = (price_change / ipo_open_price) * 100 if ipo_open_price > 0 else 0
                        
                        logger.info(f"πŸ“ˆ {symbol}: IPO Open @ ${ipo_open_price:.2f}, 1hr later @ ${one_hour_price:.2f}, P&L: ${one_hour_pnl:+.2f} ({pnl_percentage:+.1f}%)")
                    else:
                        logger.warning(f"⚠️ {symbol}: Could not find IPO first-hour price data")
                        one_hour_pnl = 0.0
                else:
                    logger.warning(f"⚠️ {symbol}: No historical data available")
                    one_hour_pnl = 0.0
            else:
                logger.warning("⚠️ yfinance not available, using mock P&L")
                one_hour_pnl = total_investment * 0.02  # Mock 2% gain
                pnl_percentage = 2.0
        except Exception as e:
            logger.error(f"❌ Error calculating IPO first-hour P&L for {symbol}: {e}")
            one_hour_pnl = 0.0
            pnl_percentage = 0.0
        
        pnl_color = COLORS['success'] if one_hour_pnl >= 0 else COLORS['error']
        
        html_content += f"""
                <tr style="background: {row_bg}; border-bottom: 1px solid #dee2e6;">
                    <td style="padding: 8px 6px; font-weight: bold;">{symbol}</td>
                    <td style="padding: 8px 6px; text-align: center;">${total_investment:,.0f}</td>
                    <td style="padding: 8px 6px; text-align: center; color: {pnl_color};">${one_hour_pnl:+,.2f}<br><small>({pnl_percentage:+.1f}%)</small></td>
                    <td style="padding: 8px 6px; text-align: center; color: {sentiment_color};">{avg_sentiment:+.3f}</td>
                    <td style="padding: 8px 6px; text-align: center; color: {prediction_color};">{prediction_label}<br><small>{predicted_change:+.1f}%</small></td>
                    <td style="padding: 8px 6px; text-align: center; font-size: 0.8rem;">πŸ—¨οΈ{reddit_count}<br>πŸ“°{news_count}</td>
                </tr>
        """
    
    html_content += """
            </tbody>
        </table>
        <div style="margin-top: 1rem; padding: 1rem; background: #f8f9fa; border-radius: 4px; font-size: 0.8rem;">
            <strong>πŸ“Š Analysis Legend:</strong><br>
            πŸ—¨οΈ Reddit posts analyzed | πŸ“° News articles analyzed<br>
            <strong>1-Hour P&L:</strong> IPO performance from opening price to 1 hour after IPO launch (e.g., 10am to 11am)<br>
            <strong>Sentiment:</strong> -1.0 (Very Negative) to +1.0 (Very Positive)<br>
            <strong>Prediction:</strong> Expected first-hour price movement based on sentiment analysis
        </div>
    </div>
    """
    
    return html_content

def execute_vm_command(command):
    """Execute command on VM"""
    logger.info(f"πŸ’» Executing VM command: {command}")
    
    try:
        response = requests.post(f"{VM_API_URL}/api/execute", 
                               json={'command': command}, 
                               timeout=30)
        
        if response.status_code == 200:
            result = response.json()
            output = result.get('output', 'No output')
            
            # Add color coding for common patterns
            if 'error' in output.lower() or 'failed' in output.lower():
                output = f"<span style='color: {COLORS['error']}'>{output}</span>"
            elif 'success' in output.lower() or 'complete' in output.lower():
                output = f"<span style='color: {COLORS['success']}'>{output}</span>"
            
            return f"$ {command}\n{output}"
        else:
            return f"$ {command}\nError: HTTP {response.status_code}"
    
    except Exception as e:
        return f"$ {command}\nError: {str(e)}"

def refresh_system_logs():
    """Get system logs from VM"""
    logger.info("πŸ”„ Refreshing system logs...")
    
    vm_logs = fetch_from_vm('logs', {'logs': 'No logs available'})
    
    if isinstance(vm_logs, dict) and 'logs' in vm_logs:
        logs_text = vm_logs['logs']
    else:
        logs_text = "No logs available from VM"
    
    # Add basic color coding
    lines = logs_text.split('\n')
    colored_lines = []
    
    for line in lines:
        if 'ERROR' in line or 'error' in line:
            colored_lines.append(f"<span style='color: {COLORS['error']}'>{line}</span>")
        elif 'WARN' in line or 'warning' in line:
            colored_lines.append(f"<span style='color: {COLORS['warning']}'>{line}</span>")
        elif 'INFO' in line or 'success' in line:
            colored_lines.append(f"<span style='color: {COLORS['success']}'>{line}</span>")
        else:
            colored_lines.append(line)
    
    return '\n'.join(colored_lines[-100:])  # Last 100 lines

def create_enhanced_dashboard():
    """Create the enhanced dashboard with all features"""
    
    logger.info("🎨 Creating enhanced dashboard interface...")
    
    # Custom CSS for better styling
    custom_css = """
    .gradio-container {
        max-width: 1400px !important;
        margin: auto !important;
    }
    .metric-card {
        background: white !important;
        border: 1px solid #e1e5e9 !important;
        border-radius: 8px !important;
        padding: 1rem !important;
    }
    """
    
    # ALL components must be defined inside this context
    with gr.Blocks(
        title="πŸš€ Premium Trading Dashboard", 
        theme=gr.themes.Soft(primary_hue="blue"),
        css=custom_css
    ) as demo:
        logger.info("πŸ–ΌοΈ Inside Blocks context - creating enhanced interface")
        
        # Header with gradient
        gr.HTML("""
            <div style="text-align: center; padding: 3rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; margin-bottom: 2rem; border-radius: 16px; box-shadow: 0 8px 32px rgba(0,0,0,0.1);">
                <h1 style="margin: 0; font-size: 3rem; font-weight: 700;">πŸš€ Premium Trading Dashboard</h1>
                <p style="margin: 1rem 0 0 0; font-size: 1.3rem; opacity: 0.9;">Advanced IPO Trading with AI-Powered Sentiment Analysis</p>
                <div style="margin-top: 1rem; font-size: 0.9rem; opacity: 0.8;">
                    πŸ“ˆ Real-time Data β€’ 🧠 Sentiment Analysis β€’ πŸ” Reddit Integration β€’ πŸ“° News Monitoring
                </div>
            </div>
        """)
        
        with gr.Tabs():
            # Portfolio Overview Tab
            with gr.Tab("πŸ“Š Portfolio Overview"):
                gr.Markdown("## πŸ’Ό Account Summary")
                
                with gr.Row():
                    portfolio_value = gr.HTML(label="πŸ’° Portfolio Value")
                    buying_power = gr.HTML(label="πŸ’³ Buying Power")
                    cash = gr.HTML(label="πŸ’΅ Cash")
                    day_change = gr.HTML(label="πŸ“ˆ Day Change")
                    equity = gr.HTML(label="🏦 Total Equity")
                
                refresh_overview_btn = gr.Button("πŸ”„ Refresh Overview", variant="primary", size="lg")
                
                gr.Markdown("## πŸ“ˆ Portfolio Performance")
                portfolio_chart = gr.Plot(label="Portfolio Value Over Time")
                
                refresh_chart_btn = gr.Button("πŸ“Š Refresh Chart", variant="secondary")
            
            # IPO Discoveries Tab
            with gr.Tab("πŸ” IPO Discoveries"):
                gr.Markdown("## 🎯 IPO Discovery & Classification")
                
                ipo_discoveries = gr.HTML()
                refresh_ipo_btn = gr.Button("πŸ”„ Refresh IPO Data", variant="primary", size="lg")
            
            # Investment Performance Tab with Sentiment Analysis
            with gr.Tab("πŸ’° Investment Performance + Sentiment"):
                gr.Markdown("## πŸ“Š Advanced P&L Analysis with AI Sentiment")
                
                gr.HTML("""
                    <div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white; padding: 1rem; border-radius: 8px; margin-bottom: 1rem; text-align: center;">
                        <strong>🧠 AI-Powered Sentiment Analysis</strong><br>
                        <small>Analyzes Reddit (including WallStreetBets) and Google News from 12 hours before each investment</small>
                    </div>
                """)
                
                investment_performance = gr.HTML()
                refresh_performance_btn = gr.Button("πŸ”„ Refresh Performance + Sentiment", variant="primary", size="lg")
            
            # VM Terminal Tab
            with gr.Tab("πŸ’» VM Terminal"):
                gr.Markdown("## πŸ–₯️ Remote Terminal Access")
                
                with gr.Row():
                    command_input = gr.Textbox(
                        label="Command", 
                        placeholder="Enter command (e.g., 'ls -la', 'tail -n 20 script.log', 'ps aux')",
                        scale=4
                    )
                    execute_btn = gr.Button("▢️ Execute", variant="primary", scale=1)
                
                terminal_output = gr.Textbox(
                    label="Terminal Output", 
                    lines=15,
                    interactive=False,
                    show_copy_button=True
                )
                
                # Quick command buttons
                with gr.Row():
                    ls_btn = gr.Button("πŸ“ ls -la", size="sm")
                    logs_btn = gr.Button("πŸ“‹ tail logs", size="sm")
                    status_btn = gr.Button("⚑ system status", size="sm")
                    portfolio_btn = gr.Button("πŸ’Ό check portfolio", size="sm")
            
            # System Logs Tab
            with gr.Tab("πŸ“‹ System Logs"):
                gr.Markdown("## πŸ“Š Trading Bot Activity Logs")
                
                system_logs = gr.Textbox(
                    label="System Logs",
                    lines=20,
                    interactive=False,
                    show_copy_button=True
                )
                
                refresh_logs_btn = gr.Button("πŸ”„ Refresh Logs", variant="primary", size="lg")
        
        # Footer
        gr.HTML("""
            <div style="text-align: center; padding: 2rem; color: #666; border-top: 1px solid #eaeaea; margin-top: 3rem; background: white; border-radius: 16px;">
                <p style="font-size: 1.1rem;"><strong>πŸ€– Advanced Automated Trading Dashboard</strong></p>
                <p style="font-size: 0.95rem;">Real-time data from Alpaca Markets β€’ VM Analytics β€’ AI Sentiment Analysis β€’ Built with ❀️</p>
                <p style="font-size: 0.85rem; margin-top: 1rem; opacity: 0.7;">
                    πŸ”„ Last Updated: <span id="timestamp">{}</span> β€’ 
                    πŸ“‘ VM Status: Connected β€’ 
                    🧠 AI Analysis: Active β€’ 
                    πŸ“Š Data Sources: Reddit, Google News, Alpaca Markets
                </p>
            </div>
        """.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC")))
        
        # Event Handlers - ALL INSIDE the Blocks context
        logger.info("πŸ”— Setting up enhanced event handlers...")
        
        # Portfolio tab events
        refresh_overview_btn.click(
            fn=refresh_account_overview,
            outputs=[portfolio_value, buying_power, cash, day_change, equity]
        )
        
        refresh_chart_btn.click(
            fn=create_portfolio_chart,
            outputs=[portfolio_chart]
        )
        
        # IPO tab events  
        refresh_ipo_btn.click(
            fn=refresh_ipo_discoveries,
            outputs=[ipo_discoveries]
        )
        
        # Performance tab events (with sentiment analysis)
        refresh_performance_btn.click(
            fn=refresh_investment_performance,
            outputs=[investment_performance]
        )
        
        # Terminal events
        execute_btn.click(
            fn=execute_vm_command,
            inputs=[command_input],
            outputs=[terminal_output]
        )
        
        # Quick command buttons
        ls_btn.click(
            fn=lambda: execute_vm_command("ls -la"),
            outputs=[terminal_output]
        )
        
        logs_btn.click(
            fn=lambda: execute_vm_command("tail -n 20 script.log"),
            outputs=[terminal_output]
        )
        
        status_btn.click(
            fn=lambda: execute_vm_command("ps aux | grep python"),
            outputs=[terminal_output]
        )
        
        portfolio_btn.click(
            fn=lambda: execute_vm_command("cat portfolio.txt"),
            outputs=[terminal_output]
        )
        
        # System logs events
        refresh_logs_btn.click(
            fn=refresh_system_logs,
            outputs=[system_logs]
        )
        
        # Initial data load
        demo.load(
            fn=refresh_account_overview,
            outputs=[portfolio_value, buying_power, cash, day_change, equity]
        )
        demo.load(
            fn=create_portfolio_chart,
            outputs=[portfolio_chart]
        )
        demo.load(
            fn=refresh_ipo_discoveries,
            outputs=[ipo_discoveries]
        )
        demo.load(
            fn=refresh_system_logs,
            outputs=[system_logs]
        )
        
        demo.queue()
        logger.info("βœ… Enhanced event handlers configured successfully")
    
    logger.info("βœ… Enhanced dashboard created successfully")
    return demo

if __name__ == "__main__":
    try:
        demo = create_enhanced_dashboard()
        logger.info("βœ… Enhanced dashboard created successfully!")
        
        logger.info("πŸš€ Launching enhanced dashboard server...")
        demo.launch()
        logger.info("βœ… Enhanced dashboard launched successfully!")
        
    except Exception as e:
        logger.error(f"❌ Enhanced dashboard failed: {e}")
        raise