绘图与alert

算出指标值之后,还得把它画出来、在关键时刻提醒你。Pine Script 提供了一组丰富的绘图函数和预警函数。本章讲清怎么画线、画形状、染色、做填充,以及怎么设置预警条件。

一、画线 plot

plot() 是最基础的绘图函数,把一个数值序列画成线、柱状图或面积。

//@version=6
indicator("plot 示例", overlay = true)
plot(ta.sma(close, 20), color = color.orange, linewidth = 2, title = "MA20")
参数含义常用值
series要画的数值序列(必填)closeta.sma(...)
title图例名称"MA20"
color颜色color.red#FF6600
linewidth线宽1~4
style绘图样式见下表
offset左右偏移根数正数向右、负数向左

常用 style 样式:

样式含义
plot.style_line折线(默认)
plot.style_stepline阶梯线
plot.style_histogram柱状图
plot.style_area面积图
plot.style_columns柱形图
plot.style_circles圆点
plot.style_cross十字叉

动态颜色

maColor = close >= open ? color.green : color.red
plot(ta.ema(close, 10), color = maColor, linewidth = 2)

颜色参数可以接收条件表达式——阳线时绿色、阴线时红色。

二、画形状 plotshape

plotshape() 在 K 线上画箭头、三角、圆圈等标记,是标注买卖信号的主力。

//@version=6
indicator("金叉标记", overlay = true)
fastMA = ta.sma(close, 5)
slowMA = ta.sma(close, 20)
金叉 = ta.crossover(fastMA, slowMA)
死叉 = ta.crossunder(fastMA, slowMA)

plotshape(金叉, style = shape.triangleup, location = location.belowbar, color = color.green, size = size.small, text = "买")
plotshape(死叉, style = shape.triangledown, location = location.abovebar, color = color.red, size = size.small, text = "卖")
style 参数形状
shape.triangleup / shape.triangledown上/下三角
shape.arrowup / shape.arrowdown上/下箭头
shape.circle / shape.square圆形 / 方块
shape.cross / shape.xcross十字 / 叉叉
shape.labelup / shape.labeldown带标签的箭头
location 参数位置
location.abovebarK 线上方
location.belowbarK 线下方
location.top / location.bottom副图顶部 / 底部
location.absolute绝对坐标(需 series 为价格值)

三、K 线染色与背景

barcolor 染 K 线

barcolor(close > open ? color.green : color.red)

barcolor() 直接改变 K 线本身的颜色。

bgcolor 染背景

bgcolor(close > ta.sma(close, 20) ? color.new(color.green, 90) : na)

bgcolor() 给背景上色。color.new(color.green, 90) 的第二个参数是透明度(0 不透明,100 全透明),加透明度避免背景太抢眼。传 na 表示不染色。

四、填充 fill

fill() 在两条 plot 线之间填充颜色:

p1 = plot(high)
p2 = plot(low)
fill(p1, p2, color = color.new(color.blue, 85))

plot() 的返回值存到变量,再传给 fill()。常用于布林带通道、成交量区间填充。

五、水平线 hline

indicator("RSI", overlay = false)
plot(ta.rsi(close, 14))
hline(70, "超买", color = color.red)
hline(30, "超卖", color = color.green)
hline(50, "中线", color = color.gray, linestyle = hline.style_dotted)

hline() 画一条水平价格线。注意它只能画常数水平线(如 70、30),不能画动态值。动态水平线用 plot() 配合具体值。

六、预警 alertcondition

alertcondition() 在指标里定义一个预警条件。用户在图表上「创建预警」(Create Alert)时,可以选中这个条件。

//@version=6
indicator("RSI 预警", overlay = false)
rsi = ta.rsi(close, 14)
plot(rsi)

alertcondition(rsi > 70, title = "RSI 超买", message = "RSI 超过 70!")
alertcondition(rsi < 30, title = "RSI 超卖", message = "RSI 低于 30!")

注意:

  • alertcondition() 不会自动发预警,它只是在「创建预警」对话框里多出一条可选项。
  • title 显示在选择列表里,message 是预警触发时的提示文字。
  • message 必须是固定字符串(const string),不能含动态变量。
  • 只能在 indicator() 里用,strategy() 不支持。

七、动态预警 alert

alert() 是更灵活的预警函数,支持动态消息,指标和策略里都能用:

//@version=6
indicator("动态预警", overlay = true)
fastMA = ta.sma(close, 5)
slowMA = ta.sma(close, 20)
金叉 = ta.crossover(fastMA, slowMA)

if 金叉
    msg = "金叉触发!收盘价:" + str.tostring(close)
    alert(msg, freq = alert.freq_once_per_bar)
freq 参数触发频率
alert.freq_once_per_bar每根 K 线最多触发一次(默认)
alert.freq_once_per_bar_close仅在 K 线收盘时触发
alert.freq_all每次调用都触发

alert() 的优势:消息可以动态拼接(用 str.tostring() 把数字转字符串),放在 if 块里精准控制触发时机。

预警只在实时 K 线(realtime bar)上触发,历史 K 线不会发预警。

八、综合示例:带预警的双均线指标

//@version=6
indicator("双均线 + 预警", overlay = true)

fastLen = input.int(5, "快线周期")
slowLen = input.int(20, "慢线周期")

fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)

plot(fastMA, color = color.blue, linewidth = 2, title = "快线")
plot(slowMA, color = color.orange, linewidth = 2, title = "慢线")

金叉 = ta.crossover(fastMA, slowMA)
死叉 = ta.crossunder(fastMA, slowMA)

plotshape(金叉, style = shape.triangleup, location = location.belowbar, color = color.green, text = "金叉")
plotshape(死叉, style = shape.triangledown, location = location.abovebar, color = color.red, text = "死叉")

// 预警
alertcondition(金叉, title = "金叉信号", message = "快线上穿慢线")
alertcondition(死叉, title = "死叉信号", message = "快线下穿慢线")

小结

  • plot() 画线/柱/面积,plotshape() 在 K 线上画买卖标记。
  • barcolor() 染 K 线、bgcolor() 染背景、fill() 填充区间、hline() 画水平线。
  • alertcondition() 定义预警条件(固定消息,仅指标);alert() 发动态预警(指标和策略通用)。

画图和预警都掌握了,下一步进入实战核心——用策略脚本做回测:

strategy策略与回测