Spaces:
Build error

File size: 24,263 Bytes
9426350
 
 
 
 
 
 
 
 
 
 
3ba4ff0
 
 
 
 
9426350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
import requests
import json
import time
import os
from datetime import datetime, timedelta
from IPython.display import HTML, display
import random

try:
    # Try to access tt["address"] from the app.py global variable
    TRADER_ADDRESSES = tt["address"]
    print(f"Successfully loaded {len(TRADER_ADDRESSES)} trader addresses from global variable")
except:
    # If tt is not defined, use a placeholder address for testing
    TRADER_ADDRESSES = ["0xac50a255e330c388f44b9d01259d6b153a9f0ed9"]
    print(f"Using test address: {TRADER_ADDRESSES[0]}")

# Fallback prices if needed
DEFAULT_PRICES = {
    "BTC": 83100.00,
    "ETH": 4450.00,
    "SOL": 267.60,
    "AVAX": 114.85,
    "HYPE": 11.39,
    "XRP": 0.52,
    "HBAR": 0.09,
    "FARTCOIN": 1.20,
    "MELANIA": 0.39,
    "@107": 0.09
}

def save_to_file(data, filename):
    """Save data to a JSON file"""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"{filename}_{timestamp}.json"
    
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2)
    
    print(f"Data saved to {filename}")
    return filename

def get_price_data():
    """Fetch current prices from Hyperliquid API"""
    url = "https://api.hyperliquid.xyz/info"
    payload = {"type": "metaAndAssetCtxs"}
    
    try:
        response = requests.post(url, json=payload)
        if response.status_code == 200:
            data = response.json()
            
            # Check if the response has the expected structure
            if not isinstance(data, list) or len(data) < 2:
                print("Unexpected response structure from metaAndAssetCtxs")
                return {}, {}
            
            # Extract universe (metadata) and asset contexts (prices)
            meta = data[0]
            asset_ctxs = data[1]
            
            # Create price mappings
            current_prices = {}
            prev_day_prices = {}
            
            # Get list of coin names from universe
            coin_names = [coin['name'] for coin in meta['universe']]
            
            # Extract prices from asset contexts
            for i, ctx in enumerate(asset_ctxs):
                if i < len(coin_names):
                    coin_name = coin_names[i]
                    
                    # Get current price in priority order: midPx, markPx, oraclePx
                    current_price = None
                    if 'midPx' in ctx and ctx['midPx'] is not None:
                        try:
                            current_price = float(ctx['midPx'])
                        except (ValueError, TypeError):
                            pass
                    
                    if current_price is None and 'markPx' in ctx and ctx['markPx'] is not None:
                        try:
                            current_price = float(ctx['markPx'])
                        except (ValueError, TypeError):
                            pass
                    
                    if current_price is None and 'oraclePx' in ctx and ctx['oraclePx'] is not None:
                        try:
                            current_price = float(ctx['oraclePx'])
                        except (ValueError, TypeError):
                            pass
                    
                    # Store current price if valid
                    if current_price is not None:
                        current_prices[coin_name] = current_price
                    
                    # Get previous day price
                    prev_price = None
                    if 'prevDayPx' in ctx and ctx['prevDayPx'] is not None:
                        try:
                            prev_price = float(ctx['prevDayPx'])
                        except (ValueError, TypeError):
                            pass
                    
                    # Store previous day price if valid
                    if prev_price is not None:
                        prev_day_prices[coin_name] = prev_price
            
            print(f"Fetched current prices for {len(current_prices)} coins")
            return current_prices, prev_day_prices
        else:
            print(f"Error fetching market data: Status code {response.status_code}")
            return {}, {}
    except Exception as e:
        print(f"Exception when fetching market data: {e}")
        return {}, {}

def get_user_fills(address):
    """Fetch fills data for a specific address"""
    url = "https://api.hyperliquid.xyz/info"
    
    payload = {
        "type": "userFills",
        "user": address,
        "aggregateByTime": True
    }
    headers = {"Content-Type": "application/json"}
    
    try:
        response = requests.post(url, headers=headers, json=payload)
        if response.status_code == 200:
            data = response.json()
            print(f"Fetched {len(data)} fills for {address}")
            return data
        else:
            print(f"Error fetching fills for {address}: Status code {response.status_code}")
            return []
    except Exception as e:
        print(f"Exception when fetching fills for {address}: {e}")
        return []

def save_fills_to_csv(fills, filename):
    """Save fills data to CSV"""
    if not fills:
        print("No fills data to save")
        return None
    
    # Convert to DataFrame
    df = pd.DataFrame(fills)
    
    # Add human-readable timestamp column
    if 'time' in df.columns:
        df['datetime'] = pd.to_datetime(df['time'], unit='ms')
    
    # Save to CSV
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    csv_filename = f"{filename}_{timestamp}.csv"
    df.to_csv(csv_filename, index=False)
    
    print(f"Saved {len(df)} fills to CSV: {csv_filename}")
    return csv_filename

def calculate_price_change(current, previous):
    """Calculate percentage change between current and previous values"""
    if current is None or previous is None or previous == 0:
        return None
    
    return ((current - previous) / previous) * 100

def analyze_trader_activity():
    """Main function to analyze trader activity based on fills data"""
    # Calculate cutoff time for 24h window
    now = datetime.now()
    cutoff_times = {
        '24h': now - timedelta(hours=24),
        '12h': now - timedelta(hours=12),
        '6h': now - timedelta(hours=6),
        '3h': now - timedelta(hours=3),
        '1h': now - timedelta(hours=1)
    }
    
    # Convert to timestamps (milliseconds)
    cutoff_timestamps = {
        window: int(dt.timestamp() * 1000) 
        for window, dt in cutoff_times.items()
    }
    
    print(f"Using cutoff timestamp for 24h: {cutoff_timestamps['24h']} ({cutoff_times['24h'].strftime('%Y-%m-%d %H:%M:%S')})")
    
    # Step 1: Fetch current prices
    print("Fetching current prices...")
    current_prices, prev_day_prices = get_price_data()
    
    # Calculate price changes
    price_changes = {}
    for coin in current_prices:
        if coin in prev_day_prices:
            change = calculate_price_change(current_prices[coin], prev_day_prices[coin])
            if change is not None:
                price_changes[coin] = change
    
    # Step 2: Fetch and process fills for each address
    all_fills = []
    
    for address in TRADER_ADDRESSES:
        # Fetch all fills for this address
        fills = get_user_fills(address)
        
        # Add trader address to each fill
        for fill in fills:
            fill['trader_address'] = address
        
        # Add to all fills
        all_fills.extend(fills)
    
    # Step 3: Filter only fills from the last 24 hours
    last_24h_cutoff = cutoff_timestamps['24h']
    fills_24h = [f for f in all_fills if int(f.get('time', 0)) >= last_24h_cutoff]
    
    print(f"Total fills: {len(all_fills)}")
    print(f"Fills from last 24 hours: {len(fills_24h)}")
    
    # Save all 24h fills to CSV file for investigation
    fills_csv = save_fills_to_csv(fills_24h, "fills_last_24h")
    
    # Step 4: Initialize data structures for each time window
    time_windows = {}
    for window, cutoff in cutoff_timestamps.items():
        fills_window = [f for f in all_fills if int(f.get('time', 0)) >= cutoff]
        time_windows[window] = {
            'fills': fills_window,
            'open_positions': {},  # Coin -> {long, short}
            'volumes': {},         # Coin -> total volume
            'unique_traders': {},  # Coin -> set of traders
            'entry_prices': {}     # Coin -> {long_value, long_size, short_value, short_size}
        }
    
    # Step 5: Process fills for each time window
    for window, data in time_windows.items():
        fills = data['fills']
        
        # Initialize entry price tracking
        data['entry_prices'] = {}  # Coin -> {long_value, long_size, short_value, short_size}
        
        # Process each fill
        for fill in fills:
            # Get basic fill data
            coin = fill.get('coin')
            if not coin:
                continue
                
            # Get trader address
            trader = fill.get('trader_address')
            
            # Track unique traders for this coin
            if coin not in data['unique_traders']:
                data['unique_traders'][coin] = set()
            if trader:
                data['unique_traders'][coin].add(trader)
            
            # Get size, price and direction
            size = abs(float(fill.get('sz', 0.0)))
            price = float(fill.get('px', 0.0))
            direction = fill.get('dir', '')
            
            # Track trading volume for this coin
            if coin not in data['volumes']:
                data['volumes'][coin] = 0.0
            data['volumes'][coin] += size
            
            # Track open positions and weighted entry prices
            if 'Open' in direction:
                # Initialize entry price tracking for this coin
                if coin not in data['entry_prices']:
                    data['entry_prices'][coin] = {
                        'long_value': 0.0,
                        'long_size': 0.0,
                        'short_value': 0.0,
                        'short_size': 0.0
                    }
                
                # Initialize open positions for this coin
                if coin not in data['open_positions']:
                    data['open_positions'][coin] = {'long': 0.0, 'short': 0.0}
                
                if 'Long' in direction:
                    data['open_positions'][coin]['long'] += size
                    # Add to weighted entry price calculation
                    data['entry_prices'][coin]['long_value'] += size * price
                    data['entry_prices'][coin]['long_size'] += size
                elif 'Short' in direction:
                    data['open_positions'][coin]['short'] += size
                    # Add to weighted entry price calculation
                    data['entry_prices'][coin]['short_value'] += size * price
                    data['entry_prices'][coin]['short_size'] += size
    
    # Step 6: Calculate metrics for the summary table
    summary_data = []
    
    # Process each coin with activity
    all_coins = set()
    for data in time_windows.values():
        all_coins.update(data['volumes'].keys())
    
    for coin in all_coins:
        # Skip coins with no data
        if coin not in time_windows['24h']['volumes']:
            continue
        
        # Get current price for this coin
        if coin in current_prices:
            current_price = current_prices[coin]
        elif coin in DEFAULT_PRICES:
            current_price = DEFAULT_PRICES[coin]
        else:
            # Use $1 as fallback price
            current_price = 1.0
            print(f"Warning: No price found for {coin}, using $1.00")
        
        # Calculate total volume in USD
        volume_usd = {}
        for window, data in time_windows.items():
            if coin in data['volumes']:
                volume_usd[window] = data['volumes'][coin] * current_price
            else:
                volume_usd[window] = 0.0
        
        # Calculate long/short ratios
        ls_ratios = {}
        for window, data in time_windows.items():
            if coin in data['open_positions']:
                pos = data['open_positions'][coin]
                total = pos['long'] + pos['short']
                if total > 0:
                    ls_ratios[window] = {
                        'long': (pos['long'] / total) * 100,
                        'short': (pos['short'] / total) * 100
                    }
                else:
                    ls_ratios[window] = {'long': 0, 'short': 0}
            else:
                ls_ratios[window] = {'long': 0, 'short': 0}
        
        # Count unique traders
        trader_counts = {}
        for window, data in time_windows.items():
            if coin in data['unique_traders']:
                trader_counts[window] = len(data['unique_traders'][coin])
            else:
                trader_counts[window] = 0
        
        # Get price change
        price_change = price_changes.get(coin, 0)
        
        # Calculate weighted average entry prices (24h window only)
        entry_prices = {}
        if coin in time_windows['24h']['entry_prices']:
            entry_data = time_windows['24h']['entry_prices'][coin]
            
            # Calculate long entry price
            if entry_data['long_size'] > 0:
                long_entry = entry_data['long_value'] / entry_data['long_size']
            else:
                long_entry = None
                
            # Calculate short entry price
            if entry_data['short_size'] > 0:
                short_entry = entry_data['short_value'] / entry_data['short_size']
            else:
                short_entry = None
                
            # Calculate total entry price
            total_size = entry_data['long_size'] + entry_data['short_size']
            if total_size > 0:
                total_entry = (entry_data['long_value'] + entry_data['short_value']) / total_size
            else:
                total_entry = None
                
            entry_prices = {
                'total': total_entry,
                'long': long_entry,
                'short': short_entry
            }
            
            # Debug print entry prices for this coin
            print(f"Entry prices for {coin}: Total=${entry_prices['total']}, Long=${entry_prices['long']}, Short=${entry_prices['short']}")
        else:
            entry_prices = {'total': None, 'long': None, 'short': None}
        
        # Add to summary data
        summary_data.append({
            'Asset': coin,
            'Current Price': current_price,
            'Price Change': price_change,
            'Total Notional Value': volume_usd['24h'],  # Use 24h volume
            
            # Open position percentages (24h)
            'Open Pct Long': ls_ratios['24h']['long'],
            'Open Pct Short': ls_ratios['24h']['short'],
            
            # Entry prices
            'Open Total Avg Entry': entry_prices['total'],
            'Open Long Avg Entry': entry_prices['long'],
            'Open Short Avg Entry': entry_prices['short'],
            
            # Time window data
            '24h Volume': volume_usd['24h'],
            '24h Pct Long': ls_ratios['24h']['long'],
            '24h Pct Short': ls_ratios['24h']['short'],
            '24h Traders': trader_counts['24h'],
            
            '12h Volume': volume_usd['12h'],
            '12h Pct Long': ls_ratios['12h']['long'],
            '12h Pct Short': ls_ratios['12h']['short'],
            '12h Traders': trader_counts['12h'],
            
            '6h Volume': volume_usd['6h'],
            '6h Pct Long': ls_ratios['6h']['long'],
            '6h Pct Short': ls_ratios['6h']['short'],
            '6h Traders': trader_counts['6h'],
            
            '3h Volume': volume_usd['3h'],
            '3h Pct Long': ls_ratios['3h']['long'],
            '3h Pct Short': ls_ratios['3h']['short'],
            '3h Traders': trader_counts['3h'],
            
            '1h Volume': volume_usd['1h'],
            '1h Pct Long': ls_ratios['1h']['long'],
            '1h Pct Short': ls_ratios['1h']['short'],
            '1h Traders': trader_counts['1h'],
        })
    
    # Create DataFrame
    df = pd.DataFrame(summary_data)
    
    # Debug column names
    print("Summary data columns:", df.columns.tolist())
    
    # Sort by 24h volume (descending)
    if '24h Volume' in df.columns and not df.empty:
        df = df.sort_values('24h Volume', ascending=False)
    
    # Save the summary data to file
    save_to_file(summary_data, "trading_summary")
    
    return df

def format_currency(value):
    """Format numeric value as currency"""
    if value is None or pd.isna(value):
        return "N/A"
    
    if value >= 1000000:
        return f"${value/1000000:.2f}M"
    elif value >= 1000:
        return f"${value/1000:.2f}K"
    else:
        return f"${value:.2f}"

def format_percent(value):
    """Format numeric value as percentage with + or - sign"""
    if value is None or pd.isna(value):
        return ""
    
    return f"+{value:.0f}%" if value >= 0 else f"{value:.0f}%"

def format_ls_ratio(long_pct, short_pct):
    """Format long/short ratio properly ensuring sum is 100%"""
    # Round to nearest integer
    long_rounded = round(long_pct)
    short_rounded = 100 - long_rounded  # Ensure they sum to 100%
    
    return f"{long_rounded}% L / {short_rounded}% S"

def format_volume_with_traders(volume, traders):
    """Format volume with trader count"""
    vol_str = format_currency(volume)
    return f"{vol_str}\n({traders} traders)"

def format_for_display(df):
    """Format DataFrame for display"""
    if df is None or df.empty:
        return None
    
    # Make a copy
    formatted_df = df.copy()
    
    # Format current price
    formatted_df['Current Price'] = formatted_df.apply(
        lambda row: f"{format_currency(row['Current Price'])}\n{format_percent(row['Price Change'])}", 
        axis=1
    )
    
    # Format volume
    formatted_df['Volume'] = formatted_df.apply(
        lambda row: f"{format_currency(row['Total Notional Value'])}", 
        axis=1
    )
    
    # Format open positions
    formatted_df['Open Positions'] = formatted_df.apply(
        lambda row: format_ls_ratio(row['Open Pct Long'], row['Open Pct Short']), 
        axis=1
    )
    
    # Only attempt to format entry prices if the columns exist
    if all(col in df.columns for col in ['Open Total Avg Entry', 'Open Long Avg Entry', 'Open Short Avg Entry']):
        print("Entry price columns found, formatting...")
        
        # Safe getter function to handle missing values
        def safe_get(row, key):
            val = row.get(key)
            if val is None or pd.isna(val):
                return "N/A"
            return format_currency(val)
        
        # Format entry prices
        formatted_df['Open Trades Entry'] = formatted_df.apply(
            lambda row: f"Total: {safe_get(row, 'Open Total Avg Entry')}\nLong: {safe_get(row, 'Open Long Avg Entry')}\nShort: {safe_get(row, 'Open Short Avg Entry')}",
            axis=1
        )
    else:
        print("Entry price columns not found, using placeholder")
        formatted_df['Open Trades Entry'] = "N/A"
    
    # Format time windows
    for window in ['24h', '12h', '6h', '3h', '1h']:
        # Format L/S ratio
        formatted_df[f'{window} L/S'] = formatted_df.apply(
            lambda row: format_ls_ratio(row[f'{window} Pct Long'], row[f'{window} Pct Short']), 
            axis=1
        )
        
        # Format volume with traders
        formatted_df[f'{window} Volume'] = formatted_df.apply(
            lambda row: format_volume_with_traders(row[f'{window} Volume'], row[f'{window} Traders']),
            axis=1
        )
    
    # Add action column
    formatted_df['Action'] = "LongShort"
    
    # Prepare column list
    columns = [
        'Asset', 'Current Price', 'Volume', 'Open Positions', 
        '24h L/S', '24h Volume',
        '12h L/S', '12h Volume',
        '6h L/S', '6h Volume',
        '3h L/S', '3h Volume',
        '1h L/S', '1h Volume',
        'Open Trades Entry', 'Action'
    ]
    
    # Return the formatted DataFrame
    return formatted_df[columns]

def generate_styled_table(df):
    """Generate styled HTML table"""
    styles = """
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
            font-family: Arial, sans-serif;
            font-size: 14px;
        }
        th {
            background-color: #f2f2f2;
            color: #333;
            font-weight: bold;
            text-align: left;
            padding: 10px;
            border-bottom: 2px solid #ddd;
        }
        td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            white-space: pre-line;  /* Allows line breaks */
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .positive {
            color: green;
        }
        .negative {
            color: red;
        }
        .action-button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 5px 10px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            margin: 2px;
            cursor: pointer;
            border-radius: 4px;
        }
    </style>
    """
    
    html_table = df.to_html(classes='table', index=False, escape=False)
    
    # Add color to positive and negative percentages
    html_table = html_table.replace('>+', ' class="positive">+')
    html_table = html_table.replace('>-', ' class="negative">-')
    
    # Replace "LongShort" text with styled buttons
    html_table = html_table.replace('>LongShort<', '><button class="action-button">Long</button><button class="action-button">Short</button><')
    
    return styles + html_table

def save_formatted_table(df, filename="hyperliquid_analysis"):
    """Save the formatted table as HTML"""
    if df is None or df.empty:
        print("No data to save")
        return None
    
    # Generate HTML table
    styled_table = generate_styled_table(df)
    
    # Add HTML wrapper
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>Hyperliquid Analysis</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <h1>Hyperliquid Top Traders Analysis</h1>
        <p>Generated on: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</p>
        {styled_table}
    </body>
    </html>
    """
    
    # Save to file
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    html_filename = f"{filename}_{timestamp}.html"
    
    with open(html_filename, 'w', encoding='utf-8') as f:
        f.write(html_content)
    
    print(f"HTML table saved to {html_filename}")
    return html_filename

def run_analysis():
    """Main function to run the analysis"""
    print("Analyzing Hyperliquid trader activity...")
    
    try:
        # Try to create data directory, but continue if it fails
        data_dir = "hyperliquid_data"
        os.makedirs(data_dir, exist_ok=True)
        os.chdir(data_dir)
        print(f"Saving data to {os.path.abspath(data_dir)}")
    except:
        print("Working in current directory")
    
    # Analyze trader activity
    result_df = analyze_trader_activity()
    
    if result_df is not None and not result_df.empty:
        # Format for display
        display_df = format_for_display(result_df)
        
        print("\nHyperliquid Top Traders Analysis")
        print("===============================")
        
        # Generate and display styled table
        styled_table = generate_styled_table(display_df)
        display(HTML(styled_table))
        
        # Save formatted table to HTML file
        save_formatted_table(display_df)
        
        return display_df
    else:
        print("No data available to display")
        return None

# Execute the analysis
if __name__ == "__main__":
    run_analysis()