Pharos
Trade

exchange.GetOrders()

获取所有未完成的订单(挂单)。

语法

python
orders = exchange.GetOrders()

返回值

返回一个列表,每个元素是一个字典:

字段类型说明
Idstr订单ID
Pricefloat委托价格
Amountfloat委托数量
DealAmountfloat已成交数量
Statusstr订单状态(pending/closed/canceled)
Typestr订单类型(buy/sell)
Timeint创建时间戳(毫秒)

示例

基础用法

python
orders = exchange.GetOrders()
Log(f"当前有 {len(orders)} 个未完成订单")

for order in orders:
    Log(f"订单 {order['Id']}: {order['Type']} {order['Amount']} @ {order['Price']}")

取消所有挂单

python
def cancel_all():
    """取消所有未完成订单"""
    orders = exchange.GetOrders()
    for order in orders:
        exchange.CancelOrder(order['Id'])
        Log(f"已取消订单: {order['Id']}")
    Log(f"共取消 {len(orders)} 个订单")

cancel_all()

取消旧订单

python
import time

def cancel_old_orders(max_age_seconds=300):
    """取消超过指定时间的订单"""
    orders = exchange.GetOrders()
    current_time = int(time.time() * 1000)
    
    for order in orders:
        age = (current_time - order['Time']) / 1000
        if age > max_age_seconds:
            exchange.CancelOrder(order['Id'])
            Log(f"取消旧订单: {order['Id']}, 挂单时长: {age:.0f}秒")

# 取消超过5分钟的订单
cancel_old_orders(300)

统计挂单情况

python
def analyze_orders():
    """分析当前挂单"""
    orders = exchange.GetOrders()
    
    buy_orders = [o for o in orders if o['Type'] == 'buy']
    sell_orders = [o for o in orders if o['Type'] == 'sell']
    
    Log(f"买单数量: {len(buy_orders)}")
    Log(f"卖单数量: {len(sell_orders)}")
    
    if buy_orders:
        total_buy = sum(o['Amount'] for o in buy_orders)
        avg_buy_price = sum(o['Price'] * o['Amount'] for o in buy_orders) / total_buy
        Log(f"买单总量: {total_buy:.6f}, 平均价格: {avg_buy_price:.2f}")
    
    if sell_orders:
        total_sell = sum(o['Amount'] for o in sell_orders)
        avg_sell_price = sum(o['Price'] * o['Amount'] for o in sell_orders) / total_sell
        Log(f"卖单总量: {total_sell:.6f}, 平均价格: {avg_sell_price:.2f}")

analyze_orders()

相关方法