Download tools/binance.py from 1990two/XTOOLS: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/tools/binance.py
- Command line
-
hf download hf://spaces/1990two/XTOOLS/tools/binance.py
-
curl -L -o binance.py https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/tools/binance.py
1.7 kB
| """ | |
| Binance -- fully public market data, no auth. | |
| """ | |
| import httpx | |
| BASE = "https://api.binance.com/api/v3" | |
| def run(params: dict) -> dict: | |
| mode = params.get("mode", "ticker") | |
| symbol = params.get("symbol", "BTCUSDT") | |
| if mode == "klines": | |
| interval = params.get("interval", "1d") | |
| limit = int(params.get("limit", 30)) | |
| resp = httpx.get( | |
| f"{BASE}/klines", params={"symbol": symbol, "interval": interval, "limit": limit}, timeout=15.0 | |
| ) | |
| resp.raise_for_status() | |
| klines = resp.json() | |
| results = [ | |
| { | |
| "open_time": k[0], | |
| "open": k[1], | |
| "high": k[2], | |
| "low": k[3], | |
| "close": k[4], | |
| "volume": k[5], | |
| } | |
| for k in klines | |
| ] | |
| return {"mode": "klines", "symbol": symbol, "count": len(results), "results": results} | |
| if mode == "depth": | |
| limit = int(params.get("limit", 20)) | |
| resp = httpx.get(f"{BASE}/depth", params={"symbol": symbol, "limit": limit}, timeout=15.0) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| return {"mode": "depth", "symbol": symbol, "bids": data.get("bids", []), "asks": data.get("asks", [])} | |
| # default: 24hr ticker | |
| resp = httpx.get(f"{BASE}/ticker/24hr", params={"symbol": symbol}, timeout=15.0) | |
| resp.raise_for_status() | |
| t = resp.json() | |
| return { | |
| "mode": "ticker", | |
| "symbol": symbol, | |
| "price": t.get("lastPrice"), | |
| "change_24h_pct": t.get("priceChangePercent"), | |
| "high_24h": t.get("highPrice"), | |
| "low_24h": t.get("lowPrice"), | |
| "volume_24h": t.get("volume"), | |
| } | |