回测命名空间 / future.py
bt.tick
行算子, 通过tick数据回测,生成pnl 输入 col(target, last_price, bid1, ask1) :target: 目标手数 :last_price: tick的最新价 :bid1: tick的买1价 :ask1: tick的卖1价 所有数据必须非空 参数 :trade_price: `TradePriceType`, 挂单价格方法
可执行示例returns: Exprbt
输入 / 输出
输入
target, last_price, bid1, ask1
| 输入项 | 类型 | 示例 |
|---|---|---|
target | Float64 | 0.0 |
last_price | Float64 | 10.0 |
bid1 | Float64 | 9.97 |
ask1 | Float64 | 10.03 |
输出
| 项目 | 说明 |
|---|---|
| 返回类型 | Expr |
| 输出对象 | Expr;执行后得到 Polars DataFrame |
| 输出语义 | 输出列由算子、alias 或底层实现决定;需要稳定列名时显式使用 alias。 |
| 执行方式 | 用 col(...).runtime() 或 col.with_cols(...).runtime() 创建执行计划后 calc_data。 |
| 核心调用 | col("target", "last_price", "bid1", "ask1").bt.tick(qs.TradePriceType.last_price, qs.MatchPriceType.void) |
打印输入 / 打印输出
下面内容来自本页示例代码真实执行后的 stdout,不是手写占位。
打印输入
shape: (20, 4) ┌────────┬────────────┬───────┬───────┐ │ target ┆ last_price ┆ bid1 ┆ ask1 │ │ --- ┆ --- ┆ --- ┆ --- │ │ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞════════╪════════════╪═══════╪═══════╡ │ 0.0 ┆ 10.0 ┆ 9.97 ┆ 10.03 │ │ 0.5 ┆ 10.2 ┆ 10.17 ┆ 10.23 │ │ 1.0 ┆ 10.1 ┆ 10.07 ┆ 10.13 │ │ 1.0 ┆ 10.5 ┆ 10.47 ┆ 10.53 │ │ 0.0 ┆ 10.7 ┆ 10.67 ┆ 10.73 │ │ … ┆ … ┆ … ┆ … │ │ 0.0 ┆ 12.4 ┆ 12.37 ┆ 12.43 │ │ 0.5 ┆ 12.2 ┆ 12.17 ┆ 12.23 │ │ 1.0 ┆ 12.7 ┆ 12.67 ┆ 12.73 │ │ 1.0 ┆ 12.9 ┆ 12.87 ┆ 12.93 │ │ 0.0 ┆ 13.1 ┆ 13.07 ┆ 13.13 │ └────────┴────────────┴───────┴───────┘
打印输出
shape: (1, 6) ┌───────────┬─────────────┬──────────┬────────────┬─────┬─────┐ │ trade_num ┆ trade_price ┆ hold_num ┆ hold_price ┆ pnl ┆ fee │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞═══════════╪═════════════╪══════════╪════════════╪═════╪═════╡ │ 0.0 ┆ 0.0 ┆ 1.0 ┆ 12.45 ┆ 0.0 ┆ 0.0 │ └───────────┴─────────────┴──────────┴────────────┴─────┴─────┘
调用
col("target", "last_price", "bid1", "ask1").bt.tick(qs.TradePriceType.last_price, qs.MatchPriceType.void)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
trade_price | TradePriceType | 必填 | 位置参数 |
match_price | MatchPriceType | 必填 | 位置参数 |
fee_rate | float | Params | 0.0 | 位置参数 |
源码参数说明
:trade_price: `TradePriceType`, 挂单价格方法 :match_price: `MatchPriceType`, 撮合成交方法 :fee_rate: 单边手续费率
完整代码
本页完整例子会执行真实的
calc_data 或对象调用。展开可复制完整代码
import datetime as dt
import polars as pl
import qust as qs
from qust import col, pms
data = pl.DataFrame(
{
"target": [0.0, 0.5, 1.0, 1.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0],
"last_price": [10.0, 10.2, 10.1, 10.5, 10.7, 10.4, 10.9, 11.1, 10.8, 11.3, 11.6, 11.4, 11.9, 12.1, 11.8, 12.4, 12.2, 12.7, 12.9, 13.1],
"bid1": [9.97, 10.17, 10.07, 10.47, 10.67, 10.37, 10.87, 11.07, 10.77, 11.27, 11.57, 11.37, 11.87, 12.07, 11.77, 12.37, 12.17, 12.67, 12.87, 13.07],
"ask1": [10.03, 10.23, 10.13, 10.53, 10.73, 10.43, 10.93, 11.13, 10.83, 11.33, 11.63, 11.43, 11.93, 12.13, 11.83, 12.43, 12.23, 12.73, 12.93, 13.13],
}
)
print("算子:")
print('bt.tick')
print("场景:")
print('回测:从信号、持仓和价格生成成交或收益输出。')
print("模式:")
print('可执行示例:构造表达式并运行 calc_data。')
print("输入列:")
print('target, last_price, bid1, ask1')
print("调用:")
print('col("target", "last_price", "bid1", "ask1").bt.tick(qs.TradePriceType.last_price, qs.MatchPriceType.void)')
print("输入数据:")
print(data)
expr = col("target", "last_price", "bid1", "ask1").bt.tick(qs.TradePriceType.last_price, qs.MatchPriceType.void)
df = col(expr).runtime()
out = df.calc_data(data)
print("输出:")
print(out)改成业务代码
| 改哪里 | 怎么改 |
|---|---|
| 列名 | 把示例 DataFrame 里的列名换成你的真实列名,列顺序保持和用法一致。 |
| 参数 | 只改函数括号里的参数;不要随意改变 rolling/over/batch/select 的链式层级。 |
| 输出名 | 需要稳定输出列名时,在表达式尾部加 .alias("name")。 |
| 调试 | 先打印输入数据和调用字符串,再执行 calc_data;报 schema 错时先检查列数和 dtype。 |
注意事项
- 参数类型与预期不一致会导致运行时报错或返回空值。
来源
| 项目 | 位置 |
|---|---|
| 源码文件 | future.py |
| 类/对象 | Bt |