3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
draw_vertline 在 K 线图绘制垂直线标记关键节点在量化策略开发与回测过程中,直观的图表可视化能极大提升信号调优和策略诊断的效率。QMT 平台提供了丰富的绘图 API,其中 ContextInfo.draw_vertline 接口专用于在图表中绘制垂直线,帮助开发者精准标记事件发生点、交易信号触发时刻或指定的时间区间。
draw_vertline 函数可以在满足指定条件时,在图表上绘制连接两个高度数值(如最高价与最低价,或开盘价与收盘价)的垂直线。
ContextInfo.draw_vertline(cond, number1, number2, color='', limit='')
| 参数名 | 类型 | 说明 |
|---|---|---|
| cond | bool |
绘图触发条件。当条件为 True 时绘制垂直线。 |
| number1 | number |
垂直线的起始高度(例如当前 Bar 的开盘价或最低价)。 |
| number2 | number |
垂直线的终止高度(例如当前 Bar 的收盘价或最高价)。 |
| color | string |
颜色(可选,默认白色)。支持:red(红)、green(绿)、blue(蓝)、cyan(蓝绿)、yellow(黄)、magenta(品红)、white(白)、brown(棕)。 |
| limit | string |
画线控制(可选)。'noaxis' 表示不影响坐标轴缩放;'nodraw' 表示不画线。 |
在日内高频或定时策略中,常需要标记特定时间(如每日 10:00 或末盘)的位置:
#coding:gbk
import time
def init(ContextInfo):
pass
def handlebar(ContextInfo):
# 获取当前 bar 的时间戳并转换为 HHMMSS 格式
timetag = ContextInfo.get_bar_timetag(ContextInfo.barpos)
time_str = timetag_to_datetime(timetag, '%H%M%S')
# 获取当前 K 线高低价
high = ContextInfo.get_market_data(['high'])
low = ContextInfo.get_market_data(['low'])
# 条件:在每天 10:00:00 标记一条青色垂直线
is_target_time = (time_str == '100000')
ContextInfo.draw_vertline(is_target_time, low, high, 'cyan', 'noaxis')
当策略出现买入或卖出信号时,通过不同颜色的垂直线直观穿透当前 K 线:
#coding:gbk
def init(ContextInfo):
ContextInfo.set_universe(['600000.SH'])
def handlebar(ContextInfo):
# 示例信号条件(如收盘价创20日新高)
close_list = ContextInfo.get_history_data(21, '1d', 'close')
if len(close_list) < 21:
return
current_close = close_list[-1]
max_20_close = max(close_list[:-1])
open_price = ContextInfo.get_market_data(['open'])
close_price = ContextInfo.get_market_data(['close'])
# 买入信号:收盘价突破20日新高,画红色垂直线标记
buy_signal = current_close > max_20_close
ContextInfo.draw_vertline(buy_signal, open_price, close_price, 'red', 'noaxis')
limit 参数设置为 'noaxis',这样画出的垂直线数值不会影响主图坐标轴的价格上下限缩放。ContextInfo.paint()(画指标线)、ContextInfo.draw_text()(标注文字)搭配使用,打造完整的策略分析大盘。