luguog commited on
Commit
74fec9c
·
verified ·
1 Parent(s): b904011

Create api.py

Browse files
Files changed (1) hide show
  1. api.py +74 -0
api.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import ccxt
4
+ import os
5
+ import pandas as pd
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ app = FastAPI()
11
+
12
+ # CORS (optional)
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ exchange = 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")
29
+ def 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) * 100
44
+ 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_depth
47
+ 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()) * 100
50
+
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
+ ) / 4
57
+
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
+ continue
69
+
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)}