本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=3
study(title="Linear Range", shorttitle="Linear Range", overlay = false)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//INPUTS
p = hl2
//sigma = input(1, type = float, minval = 0.5, title = "Noise Reduction")
//periods = input(20, type = integer, title = "Length (periods / length of the average swing high/low to the mean, typically 5 - 10 bars)")
//smooth = input(true, type = bool, title = "Sigma Smooth or adaptive? (Average Range or Standard Deviation)")
//mult = input(1, type = float, title = "Sigma")
sigma = 1
periods = 20
smooth = true
mult = input(1, type = float, title = "Sigma / Standard Deviation")
spec = input(false, type = bool, title = "Speculative Arrows based on candlesticks?")
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//MEAN
//-------------------------------------------------------------------------------------------------------------------------------------------------
//Kalman Filter for mean
value1 = 0.0
value2 = 0.0
tFilterRes = 0.0
value1 := 0.2 * (p - p[1]) + 0.8 * nz(value1[1])
value2 := 0.1 * (high - low) * sigma + 0.8 * nz(value2[1])
lambda = abs(value1 / value2)
alpha = (-lambda * lambda + sqrt(lambda * lambda * lambda * lambda + 16 * lambda * lambda)) / 8
tFilterRes := alpha * p + (1 - alpha)*nz(tFilterRes[1])
mean = tFilterRes
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//SIGMA
//-------------------------------------------------------------------------------------------------------------------------------------------------
//NOISE
varianceH = 0.0
varianceL = 0.0
squaredH = abs(high - mean)
squaredL = abs(low - mean)
//.................................................................................................................................................
//Cycle Calculation
barcount = periods * sigma
for i = 1 to barcount
varianceH := varianceH + (squaredH[i] * squaredH[i])
varianceL := varianceL + (squaredL[i] * squaredL[i])
variance1 = varianceH / barcount
variance2 = varianceL / barcount
stDevRes = sqrt(variance1)
stDevSup = sqrt(variance2)
stDev = ((stDevRes + stDevSup) / 2 ) * mult
drift = 0
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//PLOTTING
percChange = 0.0
if close > mean
if not smooth
stDev := stDevRes * mult
percChange := (1 + (- (mean - close) - stDev) / stDev) * stDev
if close < mean
if not smooth
stDev := stDevSup * mult
percChange := (1 + (- (mean - close) - stDev) / stDev) * stDev
down1 = percChange[1] > stDev and percChange < percChange[1] and close < open
down2 = crossunder(percChange, stDev)
up1 = percChange[1] < -stDev and percChange > percChange[1] and close > open
up2 = crossover(percChange, -stDev)
if not spec
down1 := false
up1 := false
//plot(stDev, color = black)
resistance = black
support = black
sigmaUp1 = plot(stDev, color = resistance, linewidth = 1, transp = 0, title = "+ 1 Sigma")
sigmaMean = plot(0, color = black, linewidth = 1, transp = 0)
sigmaDn1 = plot(-stDev, color = support, linewidth = 1, transp = 0, title = "- 1 Sigma")
change = plot(percChange, color = percChange > 0 ? green : red, style = columns, transp = 0)
fill(change, sigmaMean, color = black, title = "Price", transp = 100)
plotshape(down1, style = shape.triangledown, size = size.tiny, location = location.top, transp = 60, color = down1 ? fuchsia : na)
plotshape(down2, style = shape.triangledown, size = size.tiny, location = location.top, transp = 0, color = down2 ? black : na)
plotshape(up1, style = shape.triangleup, size = size.tiny, location = location.bottom, transp = 75, color = up1 ? fuchsia : na)
plotshape(up2, style = shape.triangleup, size = size.tiny, location = location.bottom, transp = 00, color = up2 ? black : na)