您好,我很高兴帮助您详细理解这个TradingView的Pine Script指标。这个指标名为“Linear Regression Channel”(线性回归通道),用于在图表上绘制线性回归通道和相关的线性回归带、未来预测以及箭头方向指示。下面,我将逐步解释代码的每一部分,并分享我的一些想法。
一、指标概述
线性回归通道是一种技术分析工具,用于捕捉价格走势的趋势和波动范围。它由一条中线(线性回归线)和上下两个偏差带组成,可帮助交易者识别支持和阻力区域,以及价格可能的未来走势。
二、代码详解
1. 指标声明和设置
//@version=5
indicator('Linear Regression Channel', overlay = true, max_lines_count = 3)
@version=5:指定了Pine Script的版本。
indicator('Linear Regression Channel', overlay = true, max_lines_count = 3):定义了指标的名称,并设置它在价格图上叠加显示,最大线条数为3。
2. 用户输入参数
// 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
bool bands = input.bool(true, "Plot Linear Regression Bands", group = "Settings Bands")
int window = input.int(150, "Length", group = "Settings Bands")
float devlen_b = input.float(3., "Deviation Linear Regression Bands", step=0.1, group = "Settings Bands")
bool channel = input.bool(false, "Plot Linear Regression Channel", group = "Settings Channel")
int window1 = input.int(150, "Channel Length", group = "Settings Channel")
float devlen1 = input.float(1., "Deviation Linear Regression Channel", step=0.1, group = "Settings Channel")
bool channel1 = input.bool(false, "Plot Future Projection of linear regression", group = "Future Projection Channel")
bool arr_dirc = input.bool(false, "Plot Arrow Direction", group = "Future Projection Channel")
int window2 = input.int(50, "Length", group = "Future Projection Channel")
float devlen2 = input.float(1., "Deviation Future Projection Regression Channel", step=0.1, group = "Future Projection Channel")
3. 定义颜色和初始化数组
// 定义颜色
color col_dn = #f01313 // 红色
color col_up = color.aqua // 青色
color col_mid = color.yellow // 黄色
color gray = color.gray
color fg_col = chart.fg_color
// 线性回归通道的线数组
var reglines = array.new_line(3)
var reglines_ = array.new_line(3)
- 定义了一些用于绘图的颜色。
- 初始化了两个线数组,用于存储线性回归通道的线对象。
4. 指标计算部分
4.1 线性回归函数
//@function linear_regression
linear_regression(src, window) =>
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
sum_x_sq = 0.0
// 计算各项和
for i = 0 to window - 1 by 1
sum_x += i + 1
sum_y += src[i]
sum_xy += (i + 1) * src[i]
sum_x_sq += math.pow(i + 1, 2)
// 计算线性回归系数
slope = (window * sum_xy - sum_x * sum_y) / (window * sum_x_sq - math.pow(sum_x, 2))
intercept = (sum_y - slope * sum_x) / window
y1 = intercept + slope * (window - 1)
dev = 0.0
for i = 0 to window - 1
dev := dev + math.pow(src[i] - (slope * (window - i) + intercept), 2)
dev := math.sqrt(dev/window)
[intercept, y1, dev, slope]
- 功能:计算给定源数据和窗口长度的线性回归系数,包括截距、斜率和标准偏差。
- 参数:
src:数据源(通常是价格)。
window:计算的窗口长度。
- 返回值:
[intercept, y1, dev, slope],分别是截距、终点Y值、标准差和斜率。
4.2 计算线性回归参数
[y2, y1, dev, slope] = linear_regression(close, window)
[y2_, y1_, dev_, slope_] = linear_regression(close, window1)
[y2__, y1__, dev__, slope__] = linear_regression(close, window2)
- 分别计算不同窗口长度下的线性回归参数,用于绘制不同的通道和带。
4.3 计算中线和上下轨
// 线性回归通道线
series float mid = y2 + slope
series float upper = mid + ta.rma(high - low, window) * devlen_b
series float lower = mid - ta.rma(high - low, window) * devlen_b
mid:回归中线。
upper:上轨线,等于中线加上偏差乘积。
lower:下轨线,等于中线减去偏差乘积。
ta.rma(high - low, window):计算高低价差的移动平均,用于平滑波动。
4.4 判断是否允许绘制
isAllowed = (last_bar_index - bar_index < window1)
isAllowed:用于判断在历史数据中是否绘制特定的元素,以避免在历史数据中重复绘制。
5. 可视化部分
5.1 绘制线性回归带
p_u = plot(upper, color = bands and channel ? na : bands ? gray : na, linewidth = 2)
p_l = plot(lower, color = bands and channel ? na : bands ? gray : na, linewidth = 2)
p_m = plot(mid, color = bands and channel ? na : bands ? gray : na)
fill(p_u, p_m, mid, upper, na, bands and channel ? na : bands ? color.new(col_up, 80) : na)
fill(p_m, p_l, lower, mid, bands and channel ? na : bands ? color.new(col_dn, 80) : na, na)
- 根据用户输入,选择性地绘制上轨、下轨和中线。
fill函数用于在上轨和中线之间,以及下轨和中线之间填充颜色,增强视觉效果。
5.2 绘制线性回归通道
if barstate.islast and channel
for i = 0 to 2
array.set(reglines, i,
line.new(x1 = bar_index - (window1 - 1),
y1 = y1_ + dev_ * devlen1 * (i - 1),
x2 = bar_index,
y2 = y2_ + dev_ * devlen1 * (i - 1),
color = color.gray,
style = i % 2 == 1 ? line.style_dashed : line.style_solid,
width = 2,
extend = extend.none)
)
linefill.new(array.get(reglines, 1), array.get(reglines, 2), color = color.new(col_up, 90))
linefill.new(array.get(reglines, 1), array.get(reglines, 0), color = color.new(col_dn, 90))
- 当用户选择绘制通道时,使用
line.new函数绘制通道的上、中、下三条线。
- 使用
linefill.new在通道区域填充颜色。
barstate.islast确保只在当前最新的K线绘制,避免重复绘制。
5.3 绘制未来预测通道
if barstate.islast and channel1
for i = 0 to 2
array.set(reglines_, i,
line.new(x1 = bar_index - (window2 - 1),
y1 = y1__ + dev__ * devlen2 * (i - 1),
x2 = bar_index,
y2 = y2__ + dev__ * devlen2 * (i - 1),
color = color.gray,
style = i % 2 == 1 ? line.style_dotted : line.style_dashed,
width = 1,
extend = extend.right)
)
linefill.new(array.get(reglines_, 1), array.get(reglines_, 2), color = color.new(col_up, 95))
linefill.new(array.get(reglines_, 1), array.get(reglines_, 0), color = color.new(col_dn, 95))
- 类似地,绘制未来预测通道,线条样式有所不同(点状和虚线),并向右延伸(
extend.right),表示未来的预测区域。
5.4 绘制箭头方向
if arr_dirc
l1 = label.new(chart.point.from_index(bar_index, hl2 > y2__ ? high : low),
text = hl2 > y2__ ? "⇗" : hl2 < y2__ ? "⇘" : "⇒",
textcolor = hl2 > y2__ ? col_up : hl2 < y2__ ? col_dn : gray,
color = color(na),
size = size.huge,
style = label.style_label_left
)
label.delete(l1[1])
- 当用户选择绘制箭头方向时,根据当前价格与回归线的关系,显示向上、向下或水平的箭头,指示价格趋势。
hl2是高低价的平均值。
5.5 条形热力图
b_c = (close - lower) / (upper - lower)
b_c := b_c > 1 ? 1 : b_c < 0 ? 0 : b_c
bar_color = channel ?
(isAllowed ?
(b_c <= 0.5 ? color.from_gradient(b_c, 0, 0.5, col_up, col_mid) : color.from_gradient(b_c, 0.5, 1, col_mid, col_dn))
: na)
: (b_c >= 0.5 ? color.from_gradient(b_c, 0.5, 1, col_mid, col_up) : color.from_gradient(b_c, 0, 0.5, col_dn, col_mid))
plotcandle(open, high, low, close,
title = "Bar HeatMap",
color = bar_color,
wickcolor = bar_color,
bordercolor = bar_color
)
barcolor(bar_color)
- 计算当前收盘价在上下轨之间的位置,用于生成颜色梯度,形成热力图效果。
- 根据
b_c的值,从预定义的颜色中获取对应的颜色,涂抹在K线上,使其颜色随着价格位置而变化。
5.6 绘制通道突破标记
condition1 = bands and channel ? na : bands ? ta.pivotlow(3, 3) and close < lower : na
condition2 = bands and channel ? na : bands ? ta.pivothigh(3, 3) and close > upper: na
plotchar(condition1, "", "◆", size=size.tiny, location=location.belowbar, color = col_up)
plotchar(condition2, "", "◆", size=size.tiny, location=location.abovebar, color = col_dn)
condition1:当价格在低点并且收盘价低于下轨时,条件为真。
condition2:当价格在高点并且收盘价高于上轨时,条件为真。
- 使用
plotchar函数在图表上绘制菱形标记,指示通道的突破。
三、我的想法
这个指标结合了传统的线性回归通道和一些扩展功能,例如未来预测通道和箭头方向指示,具有以下优点:
- 多功能性:用户可以根据需要选择绘制线性回归带、通道、未来预测通道和箭头方向,灵活性高。
- 直观性:通过颜色填充和热力图,使价格在通道中的位置一目了然。
- 实用性:通道突破标记和箭头方向有助于捕捉交易信号。
一些改进建议:
-
代码优化:可以将重复的代码封装成函数,提高代码的可读性和维护性。
-
参数调整:默认参数可能并不适用于所有品种和周期,建议用户根据具体的交易标的和时间框架调整window和devlen等参数。
-
信号验证:在实际交易中,应结合其他技术指标或分析方法,对通道突破和趋势预测信号进行验证,避免盲目跟随。
-
加入报警功能:可以增加当价格突破通道时的报警提醒,方便用户及时采取行动。
总的来说,这个指标为用户提供了一个多层次的价格分析工具,但在使用过程中,仍需注意参数设定和风险控制。
希望我的解释能帮助您更好地理解这个指标。如有其他问题,欢迎提问!