3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT(迅投量化交易系统)中,用户可以通过内置的绘图 API 将策略运算结果、自定义技术指标或交易信号直接渲染在客户端的 K 线界面上(主图或副图)。
ContextInfo.paint() 是 QMT 中最主要的画线函数,可以在每个 bar 驱动时输出对应的数值。
ContextInfo.paint(name, value, index, line_style, color='white', limit='')
-1,表示随主图时间轴/K 线位置逐 bar 绘制。0: 曲线(Line)42: 柱状线(Histogram / Bar)'white'。支持:'red', 'green', 'blue', 'yellow', 'white', 'cyan', 'magenta', 'brown'。'noaxis': 不影响纵坐标轴缩放'nodraw': 只计算数值,不画出线条在 handlebar 函数中,随着行情逐 K 线运行,调用 paint() 函数输出指标:
#coding:gbk
import numpy as np
def init(ContextInfo):
ContextInfo.set_universe(['600000.SH'])
def handlebar(ContextInfo):
# 获取收盘价
close_prices = ContextInfo.get_market_data(['close'], count=30)
if len(close_prices) < 20:
return
current_close = close_prices['close'][-1]
ma5 = np.mean(close_prices['close'][-5:])
ma20 = np.mean(close_prices['close'][-20:])
# 1. 绘制 MA5 和 MA20 曲线 (线型 0)
ContextInfo.paint('MA5', ma5, -1, 0, 'yellow')
ContextInfo.paint('MA20', ma20, -1, 0, 'magenta')
# 2. 绘制均线差值柱状图 (线型 42)
diff = ma5 - ma20
color = 'red' if diff >= 0 else 'green'
ContextInfo.paint('MA_Diff', diff, -1, 42, color)
除了 paint() 外,QMT 还提供了以下图形标记接口:
ContextInfo.draw_text(condition, position, text)
condition 为 True 时,在指定高度 position 标出 text 文字。ContextInfo.draw_number(cond, height, number, precision)
height 处绘制指定保留位数的数字。ContextInfo.draw_vertline(cond, number1, number2, color='', limit='')
number1 与 number2 的纵向区间内画垂直线。ContextInfo.draw_icon(cond, height, type)
type: 1 为椭圆,0 为矩形。绘图函数输出到主图还是副图,取决于策略在客户端的运行设置: