Market
exchange.GetDepth()
获取交易所的订单簿深度数据(买卖盘口)。
语法
python
depth = exchange.GetDepth()返回值
返回一个字典,包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| Asks | list | 卖盘列表,格式:[[价格, 数量], ...],按价格从低到高排序 |
| Bids | list | 买盘列表,格式:[[价格, 数量], ...],按价格从高到低排序 |
| Time | int | 时间戳(毫秒) |
示例
基础用法
python
depth = exchange.GetDepth()
# 获取最优买卖价
best_bid = depth['Bids'][0][0] # 买一价
best_ask = depth['Asks'][0][0] # 卖一价
Log("买一价:", best_bid)
Log("卖一价:", best_ask)
Log("价差:", best_ask - best_bid)查看深度档位
python
depth = exchange.GetDepth()
Log("=== 卖盘(Ask)===")
for i in range(min(5, len(depth['Asks']))):
price, amount = depth['Asks'][i]
Log(f"卖{i+1}: 价格 {price}, 数量 {amount}")
Log("=== 买盘(Bid)===")
for i in range(min(5, len(depth['Bids']))):
price, amount = depth['Bids'][i]
Log(f"买{i+1}: 价格 {price}, 数量 {amount}")计算订单簿深度
python
def calculate_depth(depth, levels=10):
"""计算指定档位的深度"""
bid_depth = sum([amount for price, amount in depth['Bids'][:levels]])
ask_depth = sum([amount for price, amount in depth['Asks'][:levels]])
Log(f"买盘深度(前{levels}档):", bid_depth)
Log(f"卖盘深度(前{levels}档):", ask_depth)
return bid_depth, ask_depth
depth = exchange.GetDepth()
calculate_depth(depth, levels=10)计算市价成交均价
python
def estimate_market_price(depth, amount, side='buy'):
"""估算市价单成交均价"""
orders = depth['Asks'] if side == 'buy' else depth['Bids']
total_amount = 0
total_value = 0
for price, order_amount in orders:
if total_amount >= amount:
break
execute_amount = min(order_amount, amount - total_amount)
total_amount += execute_amount
total_value += price * execute_amount
if total_amount == 0:
return 0
avg_price = total_value / total_amount
return avg_price
depth = exchange.GetDepth()
buy_amount = 1.0 # 买入1个BTC
avg_price = estimate_market_price(depth, buy_amount, side='buy')
Log(f"买入{buy_amount}个的估算均价: {avg_price:.2f}")检测大单压盘
python
def detect_large_orders(depth, threshold=10.0):
"""检测大单"""
large_bids = [order for order in depth['Bids'] if order[1] >= threshold]
large_asks = [order for order in depth['Asks'] if order[1] >= threshold]
if large_bids:
Log("检测到买盘大单:")
for price, amount in large_bids[:3]:
Log(f" 价格: {price}, 数量: {amount}")
if large_asks:
Log("检测到卖盘大单:")
for price, amount in large_asks[:3]:
Log(f" 价格: {price}, 数量: {amount}")
depth = exchange.GetDepth()
detect_large_orders(depth, threshold=10.0)计算买卖压力
python
def calculate_pressure(depth, levels=20):
"""计算买卖压力比"""
bid_volume = sum([amount for _, amount in depth['Bids'][:levels]])
ask_volume = sum([amount for _, amount in depth['Asks'][:levels]])
if ask_volume == 0:
return float('inf')
pressure_ratio = bid_volume / ask_volume
Log(f"买盘量: {bid_volume:.2f}")
Log(f"卖盘量: {ask_volume:.2f}")
Log(f"买卖压力比: {pressure_ratio:.2f}")
if pressure_ratio > 1.5:
Log("买盘压力大,可能上涨")
elif pressure_ratio < 0.67:
Log("卖盘压力大,可能下跌")
else:
Log("买卖平衡")
return pressure_ratio
depth = exchange.GetDepth()
calculate_pressure(depth, levels=20)注意事项
- 数据时效性:深度数据实时变化,获取后应尽快使用
- 档位数量:不同交易所返回的深度档位数量不同(通常 20-100 档)
- 数据完整性:某些交易所在流动性不足时,Asks 或 Bids 可能为空
- 性能考虑:深度数据量较大,不建议高频调用
python
# 推荐做法:缓存深度数据
depth_cache = None
last_update = 0
def get_depth_cached(max_age=1000):
"""获取缓存的深度数据"""
global depth_cache, last_update
import time
current_time = int(time.time() * 1000)
if depth_cache is None or (current_time - last_update) > max_age:
depth_cache = exchange.GetDepth()
last_update = current_time
return depth_cache