// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at…
- User
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ChartPrime
//@version=5 indicator('Linear Regression Channel', overlay = true, max_lines_count = 3)
// ---------------------------------------------------------------------------------------------------------------------} // 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎 // ---------------------------------------------------------------------------------------------------------------------{ 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")
// Define colors for up, down, and mid lines color col_dn = #f01313 color col_up = color.aqua color col_mid = color.yellow color gray = color.gray color fg_col = chart.fg_color
// Regression Channel Arrays Line var reglines = array.new_line(3) var reglines_ = array.new_line(3)
// ---------------------------------------------------------------------------------------------------------------------} // 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎 // ---------------------------------------------------------------------------------------------------------------------{
//@function linear_regression //@description Calculates linear regression coefficients for a given source and window. //@param src (series float) The data series on which linear regression is calculated. //@param window (int) The number of bars to use in the calculation. //@returns the intercept slope, Deviation, end of the channel. linear_regression(src, window) => sum_x = 0.0 sum_y = 0.0 sum_xy = 0.0 sum_x_sq = 0.0
// Calculate sums 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) // Calculate linear regression coefficients 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][y2, y1, dev, slope] = linear_regression(close, window)
[y2_, y1_, dev_, slope_] = linear_regression(close, window1)
[y2__, y1__, dev__, slope__] = linear_regression(close, window2)
// Linear Regression Channel Lines 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
// Returns True for window length period isAllowed = (last_bar_index - bar_index < window1)
// ---------------------------------------------------------------------------------------------------------------------} // 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉 // ---------------------------------------------------------------------------------------------------------------------{
// Plot upper, lower, and mid lines if channel is not enabled 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 areas between upper/mid and lower/mid lines 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)
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))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))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])// Bar Heat Map 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)
// Conditions for crossovers 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
// Plot markers for channel break outs plotchar(condition1, "", "◆", size=size.tiny, location=location.belowbar, color = col_up) plotchar(condition2, "", "◆", size=size.tiny, location=location.abovebar, color = col_dn)
// ---------------------------------------------------------------------------------------------------------------------} 我看不懂,帮我详细的了解这个指标,用中文回答,并给出你的想法
- Assistant
这个指标是一个用于绘制线性回归通道的Pine Script™代码,具体功能和逻辑如下:
1. 线性回归通道
线性回归通道是一种技术分析工具,旨在通过线性回归线和其上下的偏差带来可视化价格走势。它本质上提供了一种趋势判断的工具,帮助交易者识别价格的潜在支撑和阻力水平。
2. 用户输入参数
- 绘制线性回归带 (bands):选择是否绘制线性回归带,默认为
true。 - 窗口长度 (window):设置计算线性回归的长度,默认为150个条形。
- 带的偏差 (devlen_b):设置线性回归带的偏差倍数,默认为3。
- 绘制线性回归通道 (channel):选择是否绘制线性回归通道,默认为
false。 - 通道长度 (window1):设置线性回归通道的长度,默认为150个条形。
- 通道的偏差 (devlen1):设置线性回归通道的偏差倍数,默认为1。
- 绘制未来线性回归通道 (channel1): 选择是否绘制未来的线性回归通道,默认为
false。
3. 计算线性回归
该代码定义了一个函数
linear_regression,用来计算给定数据系列(如收盘价)在特定窗口内的线性回归系数(截距和斜率)。它会遍历数据系列并计算相关的和(例如sum_x和sum_y)。根据这些和,代码进一步计算趋势线的斜率和截距。4. 可视化
- 上、中、下线:通过调用
plot函数绘制上线、中线和下线。如果用户选择绘制通道,则生成通道的上、下线,并使用fill函数填充颜色以表示不同区域(如支撑和阻力)。 - 箭头指向:在未来通道的绘制中,如果启用了方向箭头功能,则会在图上显示表示价格走势的箭头。
- 条形图热图:通过计算每根K线相对于上下线的位置,生成颜色渐变的K线图,帮助直观理解价格相对于通道的相对位置。
- 交叉条件:定义了一些条件(如价格向下突破下线或者向上突破上线)来显示标记,以提示可能的买入或卖出信号。
5. 我的看法
这个指标结合了线性回归的数学模型与图形化展示,适合用于技术分析中判断趋势和反转的关键点。它能帮助交易者识别价格动态变化的模式,从而做出更加明智的交易决策。然而,交易者在使用该指标时,仍需结合其他技术分析工具和市场情况,以避免片面依赖。
进一步的话,用户可以根据个人的交易风格和偏好调整输入参数,以优化指标的表现。总之,该指标功能强大但需要用户具备一定的技术分析基础,才能理解和有效利用其中的信号。
- 绘制线性回归带 (bands):选择是否绘制线性回归带,默认为
- User
交叉条件,是怎么计算的
- Assistant
在这个指标中,交叉条件是用于识别价格与线性回归带或通道的相对位置,并作出交易信号的条件。具体来说,交叉条件的计算如下:
1. 交叉条件的定义
有两个主要的交叉条件变量:
- condition1:用于识别价格突破了线性回归带的下限(支持线)。
- condition2:用于识别价格突破了线性回归带的上限(阻力线)。
2. 条件的具体计算
condition1
condition1 = bands and channel ? na : bands ? ta.pivotlow(3, 3) and close < lower : na- 逻辑说明:
bands and channel ? na:如果同时启用了带和通道的绘制,则这个条件返回na,即不进行任何检查。bands ? ta.pivotlow(3, 3) and close < lower:如果仅启用了带的绘制,检查当前收盘价(close)是否低于下限(lower)。这里的ta.pivotlow(3, 3)用于找出3根K线的最低点,判断当前位置是否在局部低点处。这表明价格突破了支撑。
condition2
condition2 = bands and channel ? na : bands ? ta.pivothigh(3, 3) and close > upper : na- 逻辑说明:
- 同样地,
bands and channel ? na:如果同时启用了带和通道,则这个条件返回na,即不进行任何检查。 bands ? ta.pivothigh(3, 3) and close > upper:如果仅启用了带的绘制,检查当前收盘价是否高于上限(upper)。ta.pivothigh(3, 3)用来判定当前位置是否是局部高点。这表明价格突破了阻力。
- 同样地,
3. 交叉条件的输出
- 打印标记:
交叉条件用于控制在图表上绘制标记,指示潜在的买入或卖出信号:
plotchar(condition1, "", "◆", size=size.tiny, location=location.belowbar, color = col_up):如果condition1为真,则在K线下方绘制一个向上箭头,表示潜在的买入信号。plotchar(condition2, "", "◆", size=size.tiny, location=location.abovebar, color = col_dn):如果condition2为真,则在K线上方绘制一个向下箭头,表示潜在的卖出信号。
总结
交叉条件提供了一种有效的方式,通过判断价格突破通道的上下限,给予交易者视觉上的反应。这种方法有助于捕捉市场行情的转折点,提高决策的准确性。
- User
ta.pivotlow(3, 3) 我想改成找出5根K线的最高点
- Assistant
在 Pine Script 中,
ta.pivotlow和ta.pivothigh函数的参数用于寻找局部的最低点和最高点。当你想要找到5根K线的最高点时,你可以使用ta.pivothigh指令,并将参数改为5。修改代码
对于查找5根K线的最高点,代码应该更新为如下形式:
condition2 = bands and channel ? na : bands ? ta.pivothigh(5, 5) and close > upper : na解释此变化
ta.pivothigh(5, 5):这个函数将检查当前K线是否是过去5根K线的最高点。- 第一个参数(5)表示要查看的左侧K线数量,即在当前K线之前的5根K线。
- 第二个参数(5)表示要查看的右侧K线数量,即在当前K线之后的5根K线。
在这种情况下,if
condition2的逻辑会在收盘价高于通道上限制 (upper) 的同时,确认当前的K线是否是局部的最高点。这样,如果满足这个条件,就会在图表的适当位置绘制卖出信号标记。在
condition1部分,也是类似地,如果你也希望找到下方5根K线的最低点,可以将代码修改为:condition1 = bands and channel ? na : bands ? ta.pivotlow(5, 5) and close < lower : na完整修改示例
以下是修改后的相关代码片段:
// 监测价格突破下限条件 condition1 = bands and channel ? na : bands ? ta.pivotlow(5, 5) and close < lower : na // 监测价格突破上限条件 condition2 = bands and channel ? na : bands ? ta.pivothigh(5, 5) and close > upper : na通过这种方式,你可以根据最新的K线图数据找到更大的跨度,从而提高信号的准确性。
- User
//@version=4 study("Moving Average Channel", shorttitle="MA Channel", overlay=true) weeks = input(144) mult = input(10, step=0.1) lower = sma(close, weeks) weekly = security(syminfo.tickerid, 'W', lower) upper = lower * mult basis = plot(close, transp=100) fill0 = plot(lower, color=color.lime, linewidth=2, transp=0) fill1 = plot(upper, color=color.red, linewidth=2, transp=0) fill(basis,fill1,color=close >= upper ? color.red : #00000000, transp=40) fill(basis,fill0,color=lower > close ? color.lime : #00000000, transp=40) showcross = input(true,title="Show Buy/Sell") crossup = upper[1] < close[1] and upper > close crossdn = lower[1] > close[1] and lower < close plotshape(showcross and crossup and not crossup[1] ? close : na, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=#ffffff, transp=0, offset=-1) plotshape(showcross and crossdn and not crossdn[1] ? close : na, location=location.absolute, style=shape.labelup, color=color.lime, size=size.tiny, text="Buy", textcolor=#ffffff, transp=0, offset=-1) 帮我看看 代码好像出了问题
- Assistant
在你提供的 Pine Script 代码中,主要的目的是创建一个移动平均通道,并通过价格交叉来生成买入和卖出信号。以下是代码的分析和可能的问题:
代码分析
-
参数定义:
weeks = input(144):定义移动平均的窗口长度为144周。mult = input(10, step=0.1):定义上轨的倍数。
-
计算移动平均:
lower = sma(close, weeks):计算144周的简单移动平均。weekly = security(syminfo.tickerid, 'W', lower):获取周线数据,但此处没有被用来绘制上轨。
-
计算通道:
upper = lower * mult:根据上轨是下轨的mult倍来计算上轨。
-
绘制数据:
- 使用
plot函数绘制移动平均的下轨和上轨。 - 使用
fill函数在通道的上下轨之间填充颜色,填充的条件是根据当前价格相对于通道的上下轨。
- 使用
-
交叉计算:
crossup:判断是否从下方向上突破上轨。crossdn:判断是否从上方突破下轨。
-
绘制信号:
- 根据交叉条件显示买入和卖出信号。
可能存在的问题和修改意见
-
security函数的使用:- 在这段代码中,你有一个
weekly变量,它使用了security函数来获取周线数据,但它似乎没有被使用。如果你打算根据周线的移动平均进行计算,可以考虑直接使用它。
- 在这段代码中,你有一个
-
在使用前向参考的信号:
crossup和crossdn信号使用了上一个周期的值upper[1]和lower[1]。确保这与你的意图一致,因为在某些情况下可能会产生错误的信号。
-
信号绘制:
- 检查
plotshape的逻辑是否符合你的预期。如果希望在“卖出”信号的位置上展示“Sell”标签,确保它确实是由于上轨的突破。相应地,对于“买入”信号,也要确保它满足条件。
- 检查
代码修正建议
以下是可能的修改版本,主要是去掉了未使用的变量并且稍作清理:
//@version=4 study("Moving Average Channel", shorttitle="MA Channel", overlay=true) weeks = input(144) mult = input(10, step=0.1) // Calculate the lower band (moving average) lower = sma(close, weeks) upper = lower * mult // Plotting the basis and the channels basis = plot(close, transp=100) fill0 = plot(lower, color=color.lime, linewidth=2, transp=0) fill1 = plot(upper, color=color.red, linewidth=2, transp=0) // Filling the areas fill(basis, fill1, color=close >= upper ? color.red : na, transp=40) fill(basis, fill0, color=lower > close ? color.lime : na, transp=40) // Show buy/sell signals showcross = input(true, title="Show Buy/Sell") crossup = upper[1] < close[1] and upper > close crossdn = lower[1] > close[1] and lower < close // Plotting the signals plotshape(showcross and crossup and not crossup[1] ? close : na, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=#ffffff, transp=0, offset=-1) plotshape(showcross and crossdn and not crossdn[1] ? close : na, location=location.absolute, style=shape.labelup, color=color.lime, size=size.tiny, text="Buy", textcolor=#ffffff, transp=0, offset=-1)总结
请注意,根据各个使用场景,可能需要进一步验证交叉条件的逻辑是否符合预期。希望以上分析和修正能帮助你解决代码中的问题。
-