下面是基于您需求编写的 Pine Script v5 代码,用于创建“平滑云指标(Smooth Cloud Indicator)”。该指标使用三条 SuperSmoother 滤波器线来判断市场趋势,并在图表上显示云层填充和交易信号。
pine
//@version=5
indicator("平滑云指标 (Smooth Cloud Indicator)", overlay=true)
// 输入参数
length1 = input.int(20, title="基础周期长度", minval=1)
src = input.source(hl2, title="计算源数据")
signalType = input.string("Slow", title="信号类型", options=["Fast", "Slow"])
// SuperSmoother 滤波器函数
// 参考 John F. Ehlers 的 SuperSmoother 设计
superSmoother(source, length) =>
alpha = 1.0 / length
theta = (1 - (alpha / 2)) / (1 + alpha / 2)
gamma = (0.5 + theta) / 2
delta = theta * theta
y1 = na
y2 = na
y1 := nz(y1[1], source)
y2 := nz(y2[1], source)
result = gamma * source + gamma * source + gamma * source - theta * y1 - delta * y2
result
// 计算三条 SuperSmoother 滤波器线
s1 = superSmoother(src, length1 * 3) // 慢速线
s2 = superSmoother(src, length1 * 2) // 中速线
s3 = superSmoother(src, length1) // 快速线
// 判断趋势
isUptrend = s2 > s1
isDowntrend = s2 < s1
// 绘制滤波器线
plot(s1, title="慢速线 (s1)", color=color.blue, linewidth=2)
plot(s2, title="中速线 (s2)", color=color.orange, linewidth=2)
plot(s3, title="快速线 (s3)", color=color.purple, linewidth=2)
// 填充云层
fill(s1, s2, color=isUptrend ? color.new(color.green, 90) : color.new(color.red, 90), title="趋势云层")
// 交易信号
var buySignal = false
var sellSignal = false
if (signalType == "Fast")
// 快速信号:s3 上穿或下穿 s1
buySignal := ta.crossover(s3, s1)
sellSignal := ta.crossunder(s3, s1)
else
// 慢速信号:s2 上穿或下穿 s1
buySignal := ta.crossover(s2, s1)
sellSignal := ta.crossunder(s2, s1)
// 绘制买入和卖出箭头
plotshape(buySignal, title="买入信号", location=location.belowbar, color=color.green, style=shape.labelup, text="买入")
plotshape(sellSignal, title="卖出信号", location=location.abovebar, color=color.red, style=shape.labeldown, text="卖出")
// 创建表格显示趋势和价格信息
var table infoTable = table.new(position.top_right, 1, 2, border_width=1)
if (barstate.islast)
// 当前趋势
currentTrend = isUptrend ? "上升趋势" : isDowntrend ? "下降趋势" : "横盘趋势"
// 当前价格
currentPrice = close
// 更新表格内容
table.cell(infoTable, 0, 0, "当前趋势:", text_color=color.white, bgcolor=isUptrend ? color.green : isDowntrend ? color.red : color.gray)
table.cell(infoTable, 0, 1, currentTrend, text_color=color.white, bgcolor=isUptrend ? color.green : isDowntrend ? color.red : color.gray)
table.cell(infoTable, 1, 0, "当前价格:", text_color=color.white, bgcolor=color.blue)
table.cell(infoTable, 1, 1, tostring(currentPrice), text_color=color.white, bgcolor=color.blue)
// 可选:绘制背景颜色以增强视觉效果
bgcolor(isUptrend ? color.new(color.green, 90) : isDowntrend ? color.new(color.red, 90) : na, title="背景颜色")