0% found this document useful (0 votes)
113 views2 pages

HTF Dynamic Linear Regression Channels

This document defines an indicator to display dynamic linear regression channels on a chart. It takes in user inputs for timeframe, upper and lower deviation levels. It calculates the slope, average, intercept of the regression line and upper and lower price levels based on the deviations. Lines are drawn between the data points and filled to show the channel. If the close price breaches the channel, it redraws the lines from the current bar.

Uploaded by

aa bb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views2 pages

HTF Dynamic Linear Regression Channels

This document defines an indicator to display dynamic linear regression channels on a chart. It takes in user inputs for timeframe, upper and lower deviation levels. It calculates the slope, average, intercept of the regression line and upper and lower price levels based on the deviations. Lines are drawn between the data points and filled to show the channel. If the close price breaches the channel, it redraws the lines from the current bar.

Uploaded by

aa bb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Script Analysis - Page 1
  • Script Analysis - Page 2

//@version=5

indicator("[AA] HTF Dynamic Linear Regression Channels", overlay=true,


max_lines_count=500, max_boxes_count=500,max_bars_back = 5000)

upperMultInput = [Link](2.0, title="Upper Deviation", inline = "Upper


Deviation")
colorUpper = [Link]([Link]([Link], 85), "", inline = "Upper Deviation")
lowerMultInput = [Link](2.0, title="Lower Deviation", inline = "Lower
Deviation")
colorLower = [Link]([Link]([Link], 85), "", inline = "Lower Deviation")

tf = [Link]('15' , "Higher timeframe")


[c,h,l,b] = [Link]([Link],tf,[close,high,low,bar_index])
calcSlope(source, length) =>
max_bars_back(source, 5000)
if [Link] or length <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to length - 1 by 1
val = source[i]
per = i + 1.0
sumX += per
sumY += val
sumXSqr += per * per
sumXY += val * per
slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
average = sumY / length
intercept = average - slope * sumX / length + slope
[slope, average, intercept]

var start_index = 1
lengthInput = bar_index - start_index + 1
[s, a, i] = calcSlope(c, lengthInput)
startPrice = i + s * (lengthInput - 1)
endPrice = i

calcDev(source, length, slope, average, intercept,h,l) =>


if [Link] or length <= 1
[float(na), float(na), float(na), float(na)]
else
upDev = 0.0
dnDev = 0.0
stdDevAcc = 0.0
dsxx = 0.0
dsyy = 0.0
dsxy = 0.0
periods = length - 1
daY = intercept + slope * periods / 2
val = intercept
for j = 0 to periods by 1
price = h[j] - val
if price > upDev
upDev := price
price := val - l[j]
if price > dnDev
dnDev := price
price := source[j]
dxt = price - average
dyt = val - daY
price -= val
stdDevAcc += price * price
dsxx += dxt * dxt
dsyy += dyt * dyt
dsxy += dxt * dyt
val += slope
stdDev = [Link](stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / [Link](dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]

[stdDev, pearsonR, upDev, dnDev] = calcDev(close, lengthInput, s, a, i,h,l)


upperStartPrice = startPrice + upperMultInput * stdDev
upperEndPrice = endPrice + upperMultInput * stdDev
lowerStartPrice = startPrice - lowerMultInput * stdDev
lowerEndPrice = endPrice - lowerMultInput * stdDev

var baseLine = [Link](na, na, na, na, width=1, color=[Link](colorLower, 0))


var upper = [Link](na, na, na, na, width=1, color=[Link](colorUpper, 0))
var lower = [Link](na, na, na, na, width=1, color=[Link](colorUpper, 0))
[Link](upper, baseLine, color = colorUpper)
[Link](baseLine, lower, color = colorLower)

if (c > upperEndPrice or c < lowerEndPrice) and (not [Link] or


[Link])
_baseLine = [Link](bar_index - lengthInput + 1, startPrice[1], bar_index - 1,
endPrice[1], width=1, color=[Link](colorLower, 0))
_upper = [Link](bar_index - lengthInput + 1, upperStartPrice[1], bar_index -
1, upperEndPrice[1], width=1, color=[Link](colorUpper, 0))
_lower = [Link](bar_index - lengthInput + 1, lowerStartPrice[1], bar_index -
1, lowerEndPrice[1], width=1, color=[Link](colorUpper, 0))
[Link](_upper, _baseLine, color = colorUpper)
[Link](_baseLine, _lower, color = colorLower)
start_index := bar_index
else if [Link]
j = c > upperEndPrice or c < lowerEndPrice ? 1: 0
line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice[j])
line.set_xy2(baseLine, bar_index - j, endPrice[j])
line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice[j])
line.set_xy2(upper, bar_index - j, upperEndPrice[j])
line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice[j])
line.set_xy2(lower, bar_index - j, lowerEndPrice[j])

You might also like