分頁、篩選與排序
掌握高效數據檢索的分頁、篩選與排序技巧。學習如何優化查詢並使用查詢參數減少 API 呼叫。
分頁
Smart Money API 使用基於游標的分頁來實現高效數據檢索。指定頁碼和限制以控制結果集。
| 參數 | 類型 | 預設值 | 最大值 |
|---|---|---|---|
| page | integer | 1 | N/A |
| limit | integer | 50 | 500 |
分頁範例
cURL
# 獲取前 100 筆結果
curl "https://api.smartmoneyapi.com/v1/whales/events?page=1&limit=100"
# 獲取第二頁
curl "https://api.smartmoneyapi.com/v1/whales/events?page=2&limit=100"
回應分頁資訊
JSON
{
"success": true,
"data": {...},
"pagination": {
"page": 1,
"limit": 50,
"total": 420,
"total_pages": 9,
"has_next": true,
"has_prev": false
}
}
篩選
根據交易對、交易所、方向和其他條件篩選結果。篩選條件使用 AND 邏輯組合。
| 篩選 | 值 | 範例 |
|---|---|---|
| symbol | 交易對 | BTCUSDT, ETHUSDT |
| exchange | bybit, binance, hyperliquid | bybit |
| direction | long, short | long |
| min_position_size | 數字 | 10.5 |
| min_pnl_percent | 數字 | 2.0 |
篩選範例
cURL
# 根據交易對和方向篩選
curl "https://api.smartmoneyapi.com/v1/whales/events?symbol=BTCUSDT&direction=long"
# 根據交易所和最小持倉量篩選
curl "https://api.smartmoneyapi.com/v1/whales/events?exchange=bybit&min_position_size=15"
排序
根據持倉量、盈虧或其他欄位排序結果。指定欄位和方向 (asc/desc)。
cURL
# 根據持倉量降序排序 (最大優先)
curl "https://api.smartmoneyapi.com/v1/whales/events?sort=position_sizeℴ=desc"
# 根據盈虧升序排序
curl "https://api.smartmoneyapi.com/v1/whales/events?sort=pnl_percentℴ=asc"
完整範例
Python: 遍歷所有頁面
Python
import requests
def get_all_whale_positions(api_key, symbol=None):
"""Fetch all whale positions with pagination"""
all_positions = []
page = 1
while True:
params = {
"page": page,
"limit": 100, # Max batch size
"sort": "position_size",
"order": "desc"
}
if symbol:
params["symbol"] = symbol
response = requests.get(
"https://api.smartmoneyapi.com/v1/whales/events",
headers={"Authorization": f"Bearer {api_key}"},
params=params
)
data = response.json()
all_positions.extend(data["data"]["positions"])
# Check if there are more pages
if not data["pagination"]["has_next"]:
break
page += 1
return all_positions
# Usage
positions = get_all_whale_positions("sk_live_abc123xyz789", symbol="BTCUSDT")
print(f"Found {len(positions)} whale positions")
最佳實踐
1. 使用最大限制 (100-500): 減少 API 呼叫以檢索相同數量的數據。高效批次處理結果。
2. 在伺服器端篩選: 使用查詢參數在源頭篩選,而不是在應用程式代碼中。減少數據傳輸。
3. 在伺服器端排序: 從 API 請求排序結果。在伺服器端進行大規模排序更高效。
4. 緩存和分頁: 本地緩存結果並分頁瀏覽緩存數據,以最小化 API 呼叫。
5. 盡早停止: 不必總是獲取所有頁面。當您有足夠數據時即可停止。