import requests
import json

def get_market_data():
    url = "https://api.hyperliquid.xyz/info"
    headers = {"Content-Type": "application/json"}
    payload = {"type": "metaAndAssetCtxs"}
    
    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()
        return response.json()
    except Exception as e:
        return f"Fehler bei der API-Abfrage: {e}"

def scan_market(min_volume=500000, min_pump=0.05, min_dump=-0.05):
    data = get_market_data()
    if isinstance(data, str):
        print(data)
        return

    try:
        universe = data[0]["universe"]
        asset_ctxs = data[1]
        opportunities = []
        
        for i, coin_info in enumerate(universe):
            coin_name = coin_info["name"]
            ctx = asset_ctxs[i]
            
            volume_24h = float(ctx["dayNtlVlm"])
            current_price = float(ctx["markPx"])
            prev_price = float(ctx["prevDayPx"])
            
            if prev_price > 0:
                price_change = (current_price - prev_price) / prev_price
            else:
                price_change = 0
                
            if volume_24h >= min_volume:
                if price_change >= min_pump:
                    opportunities.append({
                        "Coin": coin_name, "Type": "🟢 PUMP (LONG)",
                        "Price": current_price, "Change_24h": round(price_change * 100, 2),
                        "Volume_24h": round(volume_24h, 2)
                    })
                elif price_change <= min_dump:
                    opportunities.append({
                        "Coin": coin_name, "Type": "🔴 DUMP (SHORT)",
                        "Price": current_price, "Change_24h": round(price_change * 100, 2),
                        "Volume_24h": round(volume_24h, 2)
                    })
        
        print("--- HYPERLIQUID MARKET SCANNER ---")
        if not opportunities:
            print("STATUS: Aktuell keine frühen Signale (Weder >+5% noch <-5%).")
        else:
            opportunities = sorted(opportunities, key=lambda x: abs(x["Change_24h"]), reverse=True)
            for coin in opportunities[:8]:
                print(f"🚨 {coin['Type']}: {coin['Coin']} | {coin['Change_24h']:+}% | Vol: ${coin['Volume_24h']:,.0f} | Preis: ${coin['Price']}")
                
    except Exception as e:
        print(f"Fehler bei der Datenverarbeitung: {e}")

if __name__ == "__main__":
    scan_market()
