3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT 平台的 ContextInfo.get_full_tick() 函数返回的分笔(Tick)数据字典中,lastPrice 和 lastClose 代表着完全不同的价格概念。lastClose 并不是指上一分钟的收盘价。
它们的具体区别如下:
lastPrice (最新价)lastClose (昨收价/前收盘价)lastClose 是上一根 K 线(比如上一分钟)的收盘价。lastClose 永远代表昨收价。如果您需要获取上一分钟的收盘价,应该使用 ContextInfo.get_market_data() 或 ContextInfo.get_history_data() 获取 1m 周期的 K 线数据,然后取倒数第二根 Bar 的 close 值。以下是如何在策略中获取并打印这两个值的示例:
#coding:gbk
def init(ContextInfo):
# 设置股票池为平安银行
ContextInfo.set_universe(['000001.SZ'])
def handlebar(ContextInfo):
# 确保在最新行情下运行
if not ContextInfo.is_last_bar():
return
# 获取最新分笔数据
tick_data = ContextInfo.get_full_tick(['000001.SZ'])
if '000001.SZ' in tick_data:
stock_tick = tick_data['000001.SZ']
last_price = stock_tick['lastPrice']
last_close = stock_tick['lastClose']
print(f"平安银行 最新价(lastPrice): {last_price}")
print(f"平安银行 昨收价(lastClose): {last_close}")
# 计算实时涨跌幅
if last_close > 0:
pct_change = (last_price - last_close) / last_close * 100
print(f"当前涨跌幅: {pct_change:.2f}%")