3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT 平台中,判断个股是否涨停需要分两种情况来处理:当前交易日和历史指定日期。
如果是判断当前交易日(今日)的收盘价或最新价是否涨停,QMT 提供了直接获取当日涨跌停价的函数 ContextInfo.get_instrumentdetail()。
核心函数:
ContextInfo.get_instrumentdetail(stockcode)
该函数返回一个字典,其中包含 UpStopPrice(当日涨停价)和 DownStopPrice(当日跌停价)。
示例代码:
def handlebar(ContextInfo):
stock_code = '600000.SH'
# 获取当前K线的收盘价
close_price = ContextInfo.get_market_data(['close'], [stock_code], period='1d', count=1)
if close_price.empty:
return
current_close = close_price['close'][0]
# 获取合约详细信息
detail = ContextInfo.get_instrumentdetail(stock_code)
if detail:
up_stop_price = detail.get('UpStopPrice', 0)
# 判断收盘价是否等于或大于涨停价(注意浮点数精度问题)
if current_close >= up_stop_price - 0.001:
print(f"{stock_code} 今日已涨停!涨停价:{up_stop_price}")
else:
print(f"{stock_code} 未涨停。当前价:{current_close},涨停价:{up_stop_price}")
QMT 的 get_instrumentdetail 函数仅返回当前交易日的涨跌停价。如果需要判断历史指定日期是否涨停,API 没有直接提供历史涨停价的查询接口,通常需要通过获取前一交易日的收盘价进行手动计算。
计算规则:
示例代码:
def handlebar(ContextInfo):
stock_code = '000001.SZ'
# 假设我们要判断 20231010 这一天是否涨停
target_date = '20231010'
# 获取目标日期及前一天的日线数据(count=2)
df = ContextInfo.get_market_data_ex(
fields=['close'],
stock_code=[stock_code],
period='1d',
end_time=target_date,
count=2
)
if stock_code in df and len(df[stock_code]) >= 2:
pre_close = df[stock_code]['close'].iloc[-2] # 前一日收盘价
target_close = df[stock_code]['close'].iloc[-1] # 目标日收盘价
# 简单计算涨停价(以主板10%为例,实际应用中需根据板块和ST状态调整比例)
limit_up_price = round(pre_close * 1.1, 2)
if target_close >= limit_up_price - 0.001:
print(f"{stock_code} 在 {target_date} 涨停!收盘价:{target_close}")
else:
print(f"{stock_code} 在 {target_date} 未涨停。")
ContextInfo.get_instrumentdetail(stockcode)['UpStopPrice']。ContextInfo.get_market_data_ex 获取前一日收盘价,结合板块涨跌幅限制规则(10%或20%)自行计算涨停价,再与当日收盘价对比。