|
| 1 | + |
| 2 | + |
| 3 | +`spoon_toolkits.crypto.crypto_powerdata` fuses CCXT-powered CEX feeds, OKX Web3 DEX data, TA-Lib/enhanced indicators, and an MCP server that can stream results over stdio or SSE. Use it when agents need richer analytics than simple price lookups. |
| 4 | + |
| 5 | +## Environment & Settings |
| 6 | + |
| 7 | +```bash |
| 8 | +export OKX_API_KEY=... |
| 9 | +export OKX_SECRET_KEY=... |
| 10 | +export OKX_API_PASSPHRASE=... |
| 11 | +export OKX_PROJECT_ID=... |
| 12 | +export OKX_BASE_URL=https://web3.okx.com/api/v5/ # optional override |
| 13 | + |
| 14 | +# Optional overrides (defaults shown) |
| 15 | +export RATE_LIMIT_REQUESTS_PER_SECOND=10 |
| 16 | +export MAX_RETRIES=3 |
| 17 | +export RETRY_DELAY=1.0 |
| 18 | +export TIMEOUT_SECONDS=30 |
| 19 | +``` |
| 20 | + |
| 21 | +`data_provider.Settings` ingests these variables (plus indicator defaults such as SMA/EMA periods). Missing OKX keys raise immediately before any HTTP call, so configure them centrally—either via environment or by passing `env_vars` into the MCP helpers. |
| 22 | + |
| 23 | +## What’s Inside the Toolkit |
| 24 | + |
| 25 | +<table> |
| 26 | + <colgroup> |
| 27 | + <col style={{ width: "22%" }} /> |
| 28 | + <col style={{ width: "22%" }} /> |
| 29 | + <col style={{ width: "56%" }} /> |
| 30 | + </colgroup> |
| 31 | + <thead> |
| 32 | + <tr> |
| 33 | + <th>Component</th> |
| 34 | + <th>File(s)</th> |
| 35 | + <th>Purpose</th> |
| 36 | + </tr> |
| 37 | + </thead> |
| 38 | + <tbody> |
| 39 | + <tr> |
| 40 | + <td>`CryptoPowerDataCEXTool`</td> |
| 41 | + <td>`tools.py`</td> |
| 42 | + <td>Pull OHLCV candles from 100+ CCXT exchanges and pipe them through the enhanced indicator stack.</td> |
| 43 | + </tr> |
| 44 | + <tr> |
| 45 | + <td>`CryptoPowerDataDEXTool`</td> |
| 46 | + <td>`tools.py`</td> |
| 47 | + <td>Hit OKX Web3 DEX APIs for on-chain pairs specified by `chain_index` + token address.</td> |
| 48 | + </tr> |
| 49 | + <tr> |
| 50 | + <td>`CryptoPowerDataPriceTool`</td> |
| 51 | + <td>`tools.py`</td> |
| 52 | + <td>Lightweight spot price snapshot (CEX or DEX) without fetching an entire candle set.</td> |
| 53 | + </tr> |
| 54 | + <tr> |
| 55 | + <td>`CryptoPowerDataIndicatorsTool`</td> |
| 56 | + <td>`tools.py`</td> |
| 57 | + <td>Enumerate every indicator name/parameter accepted by the enhanced TA registry (TA-Lib + custom extras).</td> |
| 58 | + </tr> |
| 59 | + <tr> |
| 60 | + <td>`Settings`, `OKXDEXClient`, `TechnicalAnalysis`</td> |
| 61 | + <td>`data_provider.py`</td> |
| 62 | + <td>Central place for rate limiting, retries, authenticated OKX calls, and TA-Lib helpers.</td> |
| 63 | + </tr> |
| 64 | + <tr> |
| 65 | + <td>MCP server runners (`start_crypto_powerdata_mcp_*`)</td> |
| 66 | + <td>`server.py`, `dual_transport_server.py`</td> |
| 67 | + <td>Start stdio or HTTP/SSE transports so UI agents can subscribe to continuous feeds.</td> |
| 68 | + </tr> |
| 69 | + <tr> |
| 70 | + <td>Analytics core</td> |
| 71 | + <td>`main.py`, `enhanced_indicators.py`, `talib_registry.py`</td> |
| 72 | + <td>Parse indicator configs, register TA functions, and expose them via FastMCP tools.</td> |
| 73 | + </tr> |
| 74 | + </tbody> |
| 75 | +</table> |
| 76 | + |
| 77 | +All tools inherit `CryptoPowerDataBaseTool`, which lazily initializes global settings and reuses throttled clients; you rarely need to micromanage sessions yourself. |
| 78 | + |
| 79 | +## Indicator Configuration Cheatsheet |
| 80 | + |
| 81 | +- Accepts either JSON strings (most MCP clients) or native dicts. Double-encoded JSON like `"\"{\\\"ema\\\": ...}\""` is auto-decoded. |
| 82 | +- Mix-and-match multiple parameters per indicator: |
| 83 | + `{"ema": [{"timeperiod": 12}, {"timeperiod": 26}], "macd": [{"fastperiod": 12, "slowperiod": 26, "signalperiod": 9}]}` |
| 84 | +- Enhanced registry supports 150+ TA-Lib functions plus custom composites (VWAP, BB width/position, Aroon oscillators, etc.). |
| 85 | +- Validation errors bubble back as descriptive `ToolResult.error` messages so you can surface them directly to users. |
| 86 | + |
| 87 | +## Usage Patterns |
| 88 | + |
| 89 | +### CEX candles + indicators |
| 90 | + |
| 91 | +```python |
| 92 | +from spoon_toolkits.crypto.crypto_powerdata import CryptoPowerDataCEXTool |
| 93 | + |
| 94 | +tool = CryptoPowerDataCEXTool() |
| 95 | +result = await tool.execute( |
| 96 | + exchange="binance", |
| 97 | + symbol="BTC/USDT", |
| 98 | + timeframe="1h", |
| 99 | + limit=200, |
| 100 | + indicators_config='{"ema": [{"timeperiod": 12}, {"timeperiod": 26}], "rsi": [{"timeperiod": 14}]}', |
| 101 | +) |
| 102 | + |
| 103 | +ohlcv_rows = result.output["data"] |
| 104 | +metadata = result.output.get("metadata") |
| 105 | +``` |
| 106 | + |
| 107 | +### DEX analytics on OKX Web3 |
| 108 | + |
| 109 | +```python |
| 110 | +from spoon_toolkits.crypto.crypto_powerdata import CryptoPowerDataDEXTool |
| 111 | + |
| 112 | +dex = CryptoPowerDataDEXTool() |
| 113 | +result = await dex.execute( |
| 114 | + chain_index="1", # Ethereum |
| 115 | + token_address="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", # WETH |
| 116 | + timeframe="1H", |
| 117 | + limit=150, |
| 118 | + indicators_config='{"macd": [{"fastperiod": 12, "slowperiod": 26, "signalperiod": 9}], "bb": [{"period": 20, "std": 2}]}', |
| 119 | +) |
| 120 | + |
| 121 | +candles = result.output["data"] |
| 122 | +``` |
| 123 | + |
| 124 | +### Real-time price snapshot |
| 125 | + |
| 126 | +```python |
| 127 | +from spoon_toolkits.crypto.crypto_powerdata import CryptoPowerDataPriceTool |
| 128 | + |
| 129 | +price_tool = CryptoPowerDataPriceTool() |
| 130 | +btc = await price_tool.execute(source="cex", exchange="okx", symbol="BTC/USDT") |
| 131 | +dex_price = await price_tool.execute(source="dex", chain_index="42161", token_address="0xFF970A61A04b1cA14834A43f5de4533eBDDB5CC8") |
| 132 | +``` |
| 133 | + |
| 134 | +### Discover supported indicators |
| 135 | + |
| 136 | +```python |
| 137 | +from spoon_toolkits.crypto.crypto_powerdata import CryptoPowerDataIndicatorsTool |
| 138 | + |
| 139 | +catalog = await CryptoPowerDataIndicatorsTool().execute() |
| 140 | +print(catalog.output["indicators"]) # list of indicator metadata with defaults |
| 141 | +``` |
| 142 | + |
| 143 | +## MCP Server & Streaming |
| 144 | + |
| 145 | +- `start_crypto_powerdata_mcp_stdio(env_vars=...)` spins up a FastMCP stdio server; pass `background=True` if you need it alongside other async work. |
| 146 | +- `start_crypto_powerdata_mcp_sse(host, port, env_vars)` exposes identical tools over HTTP + Server-Sent Events (see `dual_transport_server.py` for endpoints `/mcp` and `/health`). |
| 147 | +- `start_crypto_powerdata_mcp_auto` picks stdio vs SSE automatically based on environment; handy for container images. |
| 148 | +- `CryptoPowerDataMCPServer` keeps track of running threads so you can query `status()` or stop everything on shutdown. |
| 149 | +- `mcp_bridge.py` wires FastMCP methods into the dual transport so CLI agents and browser extensions consume the same tool definitions. |
| 150 | + |
| 151 | +When you need streaming OHLCV updates (vs. periodic `execute` calls), run the SSE server and subscribe to `/mcp` with a persistent session ID. The same OKX rate limiting (`rate_limit_requests_per_second`) and retry envelopes apply regardless of transport. |
0 commit comments