0

这个由 midtownsk8rguy 创建的原始 pine 脚本。脚本是线性回归趋势通道,平均线显示相关值和 r 值。如果有人可以在这方面提供帮助并为大于或小于相关值警报添加功能,那将有很大帮助。

指标链接:https ://www.tradingview.com/script/CD7yUWRV-Linear-Regression-Trend-Channel/* *

Pine Script: `//@version=4
study("Linear Regression Trend Channel", "LRTC", true)
drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) => var line Line = na, Line := line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, 2), line.delete(Line[1]) // Basic Line Drawing Function on one Line
rsdcr2(PeriodMinusOne, Deviations, Estimate) => // Function Calculates Regression, Slope, Deviation, Correlation, and R2
    var period = PeriodMinusOne + 1, var devDenominator = Estimate=="Unbiased" ? PeriodMinusOne : period, Ex = 0.0, Ex2 = 0.0, Exy = 0.0, Ey = 0.0, for i=0 to PeriodMinusOne
        closeI = nz(close[i]), Ex := Ex + i, Ex2 := Ex2 + i * i, Exy := Exy + closeI * i, Ey := Ey + closeI
    ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx), linearRegression = (Ey - slope * Ex) / period, intercept = linearRegression + bar_index * slope, deviation = 0.0, for i=0 to PeriodMinusOne
        deviation := deviation + pow(nz(close[i]) - (intercept - bar_index[i] * slope), 2.0)
    deviation := Deviations * sqrt(deviation / devDenominator), correlate = correlation(close, bar_index, period), r2 = pow(correlate, 2.0), [linearRegression, slope, deviation, correlate, r2]
periodTrend    = input(        20,  "Trend Period", input.integer,  minval=  4)
deviationsAmnt = input(       2.0, " Deviation(s)", input.float  ,  minval=0.1, step=0.1)
estimatorType  = input("Unbiased",    " Estimator", input.string , options=["Biased","Unbiased"])
var extendType = input(   "Right", "Extend Method", input.string , options=[ "Right","Segment" ])=="Right" ? extend.right : extend.none
periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1, endPointY = linReg + slope * periodMinusOne
drawLine(endPointBar, endPointY + deviation, bar_index, linReg + deviation, extendType, #FF0000ff, line.style_solid )
drawLine(endPointBar, endPointY            , bar_index, linReg            , extendType, #CCCC00ff, line.style_dotted)
drawLine(endPointBar, endPointY - deviation, bar_index, linReg - deviation, extendType, #00FF00ff, line.style_solid )
var label Label = na, Label := label.new(max(0, endPointBar), endPointY, text=tostring(correlate, "#.####"), color=0.0>correlate ? #AA000080 : #00AA0080, textcolor=#FFFFFFff, style=label.style_label_right, tooltip="Correlation: " + tostring(correlate) + "\n            R2: " + tostring(r2)), label.delete(Label[1]) // Precise Correlation/R2 Label`
4

0 回答 0