【聚宽 JoinQuant】聚宽如何获取持仓和订单?get_orders()与get_open_orders()教程

本方案由 ++EasyQuant AI++++量化助手++ 提供。

问题背景

聚宽策略实盘或模拟盘运行时,不能只依赖买卖信号,还需要知道当前持仓、当天已提交订单和未完成订单。通过查询账户状态,可以避免重复下单,并为止损、调仓和异常处理提供依据。

完整代码示例

复制代码
def initialize(context):
    g.security = "510300.XSHG"

    set_benchmark("000300.XSHG")

    # 每天开盘后检查账户状态
    run_daily(
        check_account_status,
        time="09:35"
    )


def check_account_status(context):
    print("========== 账户资产 ==========")
    print("总资产:", context.portfolio.total_value)
    print("可用现金:", context.portfolio.available_cash)

    print("========== 当前持仓 ==========")

    positions = context.portfolio.positions

    if not positions:
        print("当前无持仓")
    else:
        for stock, position in positions.items():
            if position.total_amount <= 0:
                continue

            print(
                "股票=%s,持仓=%d,可卖=%d,成本=%.2f,现价=%.2f"
                % (
                    stock,
                    position.total_amount,
                    position.closeable_amount,
                    position.avg_cost,
                    position.price
                )
            )

    print("========== 当日未完成订单 ==========")

    open_orders = get_open_orders()

    if not open_orders:
        print("当前无未完成订单")
    else:
        for order_id, order in open_orders.items():
            print(
                "订单=%s,股票=%s,数量=%d,已成交=%d,状态=%s"
                % (
                    order_id,
                    order.security,
                    order.amount,
                    order.filled,
                    order.status
                )
            )


def handle_data(context, data):
    # 示例:没有未完成订单时才允许发出买入委托
    open_orders = get_open_orders()

    if open_orders:
        return

    position = context.portfolio.positions[g.security]

    # 空仓时才允许买入
    if position.total_amount == 0:
        order_value(
            g.security,
            context.portfolio.available_cash * 0.3
        )


def after_trading_end(context):
    print("========== 当日全部订单 ==========")

    orders = get_orders()

    for order_id, order in orders.items():
        print(
            "订单=%s,股票=%s,状态=%s,成交数量=%d"
            % (
                order_id,
                order.security,
                order.status,
                order.filled
            )
        )

注意事项

  1. context.portfolio.positions 保存当前持仓信息。

  2. get_open_orders() 返回当日未完成订单,可用于防止策略重复下单。

  3. get_orders() 可查询当天全部订单,包括已成交、已撤销和拒绝订单。

  4. 委托已提交不代表成交,策略逻辑应区分订单状态与实际持仓。

  5. A 股卖出时应使用 closeable_amount 判断可卖数量,避免忽略 T+1 规则。

总结

聚宽账户状态管理应同时关注"持仓、可用现金、未完成订单和最终订单状态"。这是避免重复交易、构建稳定实盘策略的基础。

相关推荐
EasyDSS1 小时前
景区客流遇冷?用EasyDSS企业融媒体平台直播/点播解锁「云上文旅」新玩法
人工智能·媒体·直播·点播·easydss
刘一说1 小时前
AI科技热点日报 | 2026年08月04日
人工智能·科技
如此这般英俊1 小时前
手搓Claude Code-第十章 system_prompt
数据结构·人工智能·python·语言模型·自然语言处理·prompt
云端漫步19871 小时前
HarmonyOS NEXT AI 智能生活助手:AI 日程规划
人工智能·华为·生活·harmonyos
孙启超1 小时前
【AI应用开发】怎么降低 Agent 幻觉?有几种可行方案?
大数据·人工智能·llm·agent·rag·幻觉·ai应用开发
丘丘用户思思澪1 小时前
生产环境下优化 Agent Token 成本的系统化实战指南
人工智能
我是大卫1 小时前
图解RAG,一张图理解检索增强生成
人工智能
淡忘suuda2 小时前
生产级 AI Agent 评测体系实战:从数据集构建到自动回归与质量门禁
人工智能·数据挖掘·回归
spider_xcxc2 小时前
生产级AI智能体的可观测性实战:从指标监控到LLM评估
大数据·人工智能