File size: 1,698 Bytes
0ee2df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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"),
    }