3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT(迅投量化交易系统)中,将交易信号、指标突破点或特定数值直观地标注在 K 线图上,可以极大地方便策略调试与盘中信号观察。QMT 提供了 ContextInfo.draw_text() 和 ContextInfo.draw_number() 两个核心绘图 API,支持在满足指定条件时在图形对应位置画出文字与数字。
ContextInfo.draw_text()ContextInfo.draw_text(condition, position, text)condition (bool):触发显示的条件(例如:True 或表达式 close > ma)。position (number):绘制文字的纵坐标位置(通常传入当前 K 线的价格或指标值)。text (string):需要显示的文本内容。ContextInfo.draw_number()ContextInfo.draw_number(cond, height, number, precision)cond (bool):触发显示的条件。height (number):显示数字的纵坐标高度位置。number (number/string):显示的数字数值。precision (int):保留的小数位数(取值范围 0 ~ 7)。以下策略代码展示了如何在 K 线图上:
#encoding:gbk
def init(ContextInfo):
# 设置运行的默认标的
ContextInfo.set_universe(['600000.SH'])
print("绘图策略初始化完成")
def handlebar(ContextInfo):
# 获取当前 K 线的收盘价与开盘价
close_list = ContextInfo.get_market_data(['close'], period='1d', count=1)
open_list = ContextInfo.get_market_data(['open'], period='1d', count=1)
if close_list.empty or open_list.empty:
return
close_price = close_list['close'].iloc[-1]
open_price = open_list['open'].iloc[-1]
# 设定买入条件:收盘价高于开盘价 1%
buy_condition = close_price > open_price * 1.01
# 1. 在满足买入条件时,在最低价下方(价格 * 0.99)绘制文字提示
low_list = ContextInfo.get_market_data(['low'], period='1d', count=1)
low_price = low_list['low'].iloc[-1] if not low_list.empty else open_price
ContextInfo.draw_text(buy_condition, low_price * 0.99, '买入信号')
# 2. 在高点位置(高价 * 1.01)绘制当前收盘价数值(保留 2 位小数)
high_list = ContextInfo.get_market_data(['high'], period='1d', count=1)
high_price = high_list['high'].iloc[-1] if not high_list.empty else close_price
ContextInfo.draw_number(buy_condition, high_price * 1.01, close_price, 2)
position 或 height 设置在最高价之上(如 high * 1.01)或最低价之下(如 low * 0.99)。主图或主图叠加,才能在主图 K 线上准确呈现标记效果。ContextInfo.paint() 绘制均线,以及 ContextInfo.draw_icon() 绘制图标,构建完整的图表信号视觉展示系统。