luguog/gate_dash
0
1from fastapi import FastAPI2from fastapi.middleware.cors import CORSMiddleware3import ccxt4import os5import pandas as pd6from dotenv import load_dotenv7 8load_dotenv()9 10app = FastAPI()11 12# CORS (optional)13app.add_middleware(14 CORSMiddleware,15 allow_origins=["*"],16 allow_credentials=True,17 allow_methods=["*"],18 allow_headers=["*"],19)20 21exchange = ccxt.gateio({22 'apiKey': os.getenv("GATE_API_KEY"),23 'secret': os.getenv("GATE_API_SECRET"),24 'enableRateLimit': True,25 'options': {'defaultType': 'swap'}26})27 28@app.get("/api/data")29def get_data():30 try:31 markets = exchange.load_markets()32 usdt_pairs = [s for s in markets if "/USDT" in s and markets[s].get("type") == "swap"]33 results = []34 35 for symbol in usdt_pairs:36 try:37 ticker = exchange.fetch_ticker(symbol)38 price = ticker['last']39 volume = ticker['quoteVolume']40 orderbook = exchange.fetch_order_book(symbol)41 if orderbook['asks'] and orderbook['bids']:42 spread = orderbook['asks'][0][0] - orderbook['bids'][0][0]43 spread_pct = (spread / price) * 10044 bid_depth = sum(b[1] for b in orderbook['bids'][:5])45 ask_depth = sum(a[1] for a in orderbook['asks'][:5])46 depth = bid_depth + ask_depth47 ohlcv = exchange.fetch_ohlcv(symbol, '1h', limit=24)48 closes = [x[4] for x in ohlcv]49 volatility = (pd.Series(closes).std() / pd.Series(closes).mean()) * 10050 51 score = (52 max(0, 100 - (spread_pct * 20)) +53 min(100, volume / 200000 * 100) +54 min(100, depth / 100) +55 max(0, 100 - (volatility * 10))56 ) / 457 58 results.append({59 "symbol": symbol,60 "price": price,61 "spread_pct": spread_pct,62 "volume_24h": volume,63 "depth": depth,64 "volatility": volatility,65 "mm_score": round(score, 2)66 })67 except:68 continue69 70 top_symbols = sorted(results, key=lambda x: x['mm_score'], reverse=True)[:10]71 return {"top_symbols": top_symbols}72 73 except Exception as e:74 return {"error": str(e)}