3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
draw_number 精操数值显示精度在 QMT(迅投量化交易系统)策略开发中,除绘制连续曲线(paint)外,在关键 K 线节点直接标注具体的数值(如信号价格、指标触发值、止盈止损线等)能够极大提升策略可视化的直观性。ContextInfo.draw_number 提供了在图表特定位置显示数字并精准控制小数位数的强大功能。
ContextInfo.draw_number 语法详解ContextInfo.draw_number(cond, height, number, precision)
cond (bool): 触发画图的条件。当条件计算结果为 True(或 1)时,当前 Bar 才会绘制数字。height (number): 数字在图表 Y 轴上的显示位置(纵坐标高度),通常传入当前 K 线的收盘价 close、最高价 high 或对应指标的值。number (number/string): 需要显示的具体数值(如突破价格、移动平均值等)。precision (int): 小数点显示的精度,取值范围为 0 - 7(表示保留 0 到 7 位小数)。以下策略展示了如何在满足特定条件(如创 10 日新高)时,使用 draw_number 在 K 线上方精确标注突破时的收盘价(保留 2 位小数):
#coding:gbk
def init(ContextInfo):
# 设置运行标的
ContextInfo.set_universe(['600000.SH'])
def handlebar(ContextInfo):
# 获取历史收盘价数据(20周期)
close_data = ContextInfo.get_market_data(['close'], stock_code=ContextInfo.get_universe(), period='1d', count=20)
if close_data.empty or len(close_data) < 10:
return
current_close = close_data['close'][-1]
max_prev_close = close_data['close'][:-1].max()
# 条件:当前收盘价突破前 10 日最高收盘价
is_breakthrough = current_close > max_prev_close
# 在当前 K 线上方(最高价上方一定偏移量)显示突破数值,保留 2 位小数
high_data = ContextInfo.get_market_data(['high'], stock_code=ContextInfo.get_universe(), period='1d', count=1)
current_high = high_data['high'][-1]
# 满足突破条件时画出数值
ContextInfo.draw_number(is_breakthrough, current_high * 1.01, current_close, 2)
2(例如显示 12.34)。3 或 4。0。height 参数中增加比例偏移。例如:在最高价上方标注使用 high * 1.01,在最低价下方标注使用 low * 0.99。draw_icon(显示买卖图标)与 draw_text(显示方向提示),形成“图标 + 文字 + 精准数值”的完整交易信号标注。