BigQuant使用文档

(期货期权)策略生命周期与常用 API

由qxiao创建,最终由qxiao 被浏览 3 用户

策略生命周期

所有 BigTrader 期货策略都遵循相同的生命周期结构:

from bigquant import bigtrader

# ── 1. 初始化 ──────────────────────────────────────────────
def initialize(context):
    """策略启动时执行一次"""
    context.short_window = 5
    context.long_window  = 20

# ── 2. 盘前处理 ─────────────────────────────────────────────
def before_trading_start(context, data):
    """每日开盘前触发,适合做换月检查、信号预计算等
    注意:此函数中不能下单"""
    # 检查主力合约是否发生换月
    # (使用主力连续合约时平台自动处理,无需手动换月)
    pass

# ── 3. 每个 Bar 执行 ────────────────────────────────────────
def handle_data(context, data):
    """每个时间单位触发,包含核心交易逻辑"""
    pass

# ── 5. 运行回测 ─────────────────────────────────────────────
result = bigtrader.run(
    market=bigtrader.Market.CN_FUTURE,
    initialize=initialize,
    before_trading_start=before_trading_start,
    handle_data=handle_data,
    start_date='2022-01-01',
    end_date='2024-12-31',
    capital_base=1_000_000,
    frequency='1d',
)

常用 API

期货下单

# 买入开仓(做多)N 手
context.buy_open(symbol, N)

# 卖出平仓(平多)N 手
context.sell_close(symbol, N)

# 卖出开仓(做空)N 手
context.sell_open(symbol, N)

# 买入平仓(平空)N 手
context.buy_close(symbol, N)

账户与持仓信息

pos = context.get_account_position(instrument)
long_qty  = pos.long.current_qty  if pos.long  else 0   # 多头持仓
short_qty = pos.short.current_qty if pos.short else 0   # 空头持仓

\

文档

Tick回测demos分钟回测demos日频回测demos
{link}