# context.portfolio.positions 持仓对象

获取示例策略

def init(context):
    set_execution("close")
    # 设置股票池
    context.stock = ["000001.SZ", "600000.SH"]
    # 标记只下单一次
    context.traded = False


def handle_bar(context, bar_dict):
    # 第一根bar执行一次买入,生成持仓position
    if not context.traded:
        order_target_value("000001.SZ", 10000)
        context.traded = True

    port = context.portfolio

    for sym in context.universe:
        pos = port.positions.get(sym)
        log.info("===== Symbol: %s Position输出 =====", sym)
        if pos is None:
            log.info("%s 无持仓,pos = None", sym)
            continue

        # 全部公开属性逐个打印
        log.info("symbol           : %s", pos.symbol)
        log.info("amount           : %s", pos.amount)
        log.info("available_amount : %s", pos.available_amount)
        log.info("closeable_amount: %s", pos.closeable_amount)
        log.info("cost_basis       : %s", pos.cost_basis)
        log.info("avg_cost         : %s", pos.avg_cost)
        log.info("last_price       : %s", pos.last_price)
        log.info("market_value     : %s", pos.market_value)
        log.info("pnl              : %s", pos.pnl)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

日志输出