Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @ Julien_Eche
- //@version=5
- indicator("Linear Regression Channel Ultimate", shorttitle = 'LRC', overlay=true, max_bars_back=5000)
- pI = input.int(500, "Periods", minval=2)
- uL = input.bool(true, "Log Scale")
- devMultiplier = input.float(2.0, "Deviation Multiplier", minval=0.1, step=0.1)
- nFills = input.int(23, "Number of Profile Fills", minval=2, maxval=23)
- activityMethod = input.string("Volume", "Activity calculation method", options=["Touches", "Volume"])
- numActivityLines= input.int(5, "Number of Most Active Levels to Display", minval=1, maxval=5)
- showProfile = input.bool(true, "Show Profile")
- regColor = input.color(color.new(color.gray, 0), "Regression Channel", inline="reg_channel")
- regLineStyle = input.string("Solid", "", options=["Solid", "Dotted", "Dashed"], inline="reg_channel")
- regLineWidth = input.int(1, "", minval=1, maxval=4, inline="reg_channel")
- fillColor = input.color(color.new(#909497, 95), "Channel Fill", inline="channel_fill")
- showRegLine = input.bool(false, "Show Regression Line", inline="reg_line")
- regLineColor = input.color(color.new(color.gray, 0), "", inline="reg_line")
- regLineStyleOpt = input.string("Dashed", "", options=["Solid", "Dotted", "Dashed"], inline="reg_line")
- regLineWidthOpt = input.int(1, "", minval=1, maxval=4, inline="reg_line")
- useCustomColor = input.bool(false, "Use custom Most Active Levels color")
- customColor = input.color(color.new(#00BBFF, 50), "Custom Color", inline="act_line")
- actLineStyle = input.string("Solid", "", options=["Solid", "Dotted", "Dashed"], inline="act_line")
- actLineWidth = input.int(1, "", minval=1, maxval=5, inline="act_line")
- loActColor = input.color(color.new(#00BBFF, 95), "Low Activity", inline="profile_color")
- hiActColor = input.color(color.new(#00BBFF, 25), "High Activity", inline="profile_color")
- eS = extend.right
- lI = math.min(bar_index + 1, pI)
- f_adjust(p) => uL ? math.log(p) : p
- f_unadjust(p) => uL ? math.exp(p) : p
- cS(len) =>
- if not barstate.islast or len <= 1
- [float(na), float(na), float(na)]
- else
- sX = 0.0, sY = 0.0, sXS = 0.0, sXY = 0.0
- for i = 0 to len - 1
- v = f_adjust(close[i])
- p = i + 1.0
- sX += p, sY += v, sXS += p * p, sXY += v * p
- sl = (len * sXY - sX * sY) / (len * sXS - sX * sX)
- av = sY / len
- ic = av - sl * sX / len + sl
- [sl, av, ic]
- [s, a, i] = cS(lI)
- sP = f_unadjust(i + s * (lI - 1))
- eP = f_unadjust(i)
- var float uSP = na
- var float uEP = na
- var float lSP = na
- var float lEP = na
- var line bL = na
- if showRegLine
- if na(bL) and not na(sP)
- bL := line.new(bar_index - lI + 1, sP, bar_index, eP,
- width=regLineWidthOpt, extend=eS, color=regLineColor,
- style=regLineStyleOpt == "Solid" ? line.style_solid : regLineStyleOpt == "Dotted" ? line.style_dotted : line.style_dashed)
- else
- line.set_xy1(bL, bar_index - lI + 1, sP)
- line.set_xy2(bL, bar_index, eP)
- line.set_color(bL, regLineColor)
- line.set_style(bL, regLineStyleOpt == "Solid" ? line.style_solid : regLineStyleOpt == "Dotted" ? line.style_dotted : line.style_dashed)
- line.set_width(bL, regLineWidthOpt)
- else
- line.delete(bL)
- cD(len, sl, av, ic) =>
- uD = 0.0, dD = 0.0, sDA = 0.0
- dxx = 0.0, dyy = 0.0, dxy = 0.0
- per = len - 1
- dY = ic + sl * per / 2
- v = ic
- for j = 0 to per
- pr = f_adjust(high[j]) - v
- if pr > uD
- uD := pr
- pr := v - f_adjust(low[j])
- if pr > dD
- dD := pr
- pr := f_adjust(close[j])
- dx = pr - av
- dy = v - dY
- pr -= v
- sDA += pr * pr
- dxx += dx * dx
- dyy += dy * dy
- dxy += dx * dy
- v += sl
- sD = math.sqrt(sDA / (per == 0 ? 1 : per))
- pR = dxx == 0 or dyy == 0 ? 0 : dxy / math.sqrt(dxx * dyy)
- [sD, pR, uD, dD]
- [sD, pR, uD, dD] = cD(lI, s, a, i)
- applyDeviation(baseValue, deviation) => f_unadjust(f_adjust(baseValue) + deviation)
- uSP := applyDeviation(sP, devMultiplier * sD)
- uEP := applyDeviation(eP, devMultiplier * sD)
- lSP := applyDeviation(sP, -devMultiplier * sD)
- lEP := applyDeviation(eP, -devMultiplier * sD)
- var line u = na
- var line l = na
- if na(u) and not na(uSP)
- u := line.new(bar_index - lI + 1, uSP, bar_index, uEP, width=regLineWidth, extend=eS, color=regColor,
- style=regLineStyle == "Solid" ? line.style_solid : regLineStyle == "Dotted" ? line.style_dotted : line.style_dashed)
- else
- line.set_xy1(u, bar_index - lI + 1, uSP)
- line.set_xy2(u, bar_index, uEP)
- line.set_color(u, regColor)
- line.set_style(u, regLineStyle == "Solid" ? line.style_solid : regLineStyle == "Dotted" ? line.style_dotted : line.style_dashed)
- line.set_width(u, regLineWidth)
- if na(l) and not na(lSP)
- l := line.new(bar_index - lI + 1, lSP, bar_index, lEP, width=regLineWidth, extend=eS, color=regColor,
- style=regLineStyle == "Solid" ? line.style_solid : regLineStyle == "Dotted" ? line.style_dotted : line.style_dashed)
- else
- line.set_xy1(l, bar_index - lI + 1, lSP)
- line.set_xy2(l, bar_index, lEP)
- line.set_color(l, regColor)
- line.set_style(l, regLineStyle == "Solid" ? line.style_solid : regLineStyle == "Dotted" ? line.style_dotted :line.style_dashed)
- line.set_width(l, regLineWidth)
- linefill.new(u, showRegLine ? bL : l, color=fillColor)
- if showRegLine
- linefill.new(bL, l, color=fillColor)
- var label pL = na
- label.delete(pL[1])
- if not na(pR)
- pL := label.new(bar_index - lI + 1, lSP, str.tostring(pR, "#.###"), color=color.new(color.white, 100), textcolor=color.gray, size=size.normal, style=label.style_label_up)
- calcLineValue(startY, endY, currentBar, totalBars) =>
- f_unadjust(f_adjust(startY) + (f_adjust(endY) - f_adjust(startY)) * currentBar / totalBars)
- color_from_gradient(percent, color1, color2) =>
- r = color.r(color1) + (color.r(color2) - color.r(color1)) * percent
- g = color.g(color1) + (color.g(color2) - color.g(color1)) * percent
- b = color.b(color1) + (color.b(color2) - color.b(color1)) * percent
- t = color.t(color1) + (color.t(color2) - color.t(color1)) * percent
- color.rgb(r, g, b, t)
- var counts = array.new_float(nFills, 0.0)
- var activityLines = array.new_line(numActivityLines)
- var profileFills = array.new_linefill(nFills)
- if barstate.islast
- array.clear(counts)
- for actLine in activityLines
- line.delete(actLine)
- array.clear(activityLines)
- for pf in profileFills
- linefill.delete(pf)
- array.clear(profileFills)
- for idx1 = 0 to nFills - 1
- y1 = calcLineValue(lSP, uSP, idx1, nFills)
- y2 = calcLineValue(lEP, uEP, idx1, nFills)
- count = 0.0
- for j = 0 to lI - 1
- lineValue = calcLineValue(y1, y2, j, lI - 1)
- if activityMethod == "Touches"
- if low[lI - 1 - j] <= lineValue and high[lI - 1 - j] >= lineValue
- count += 1.0
- else
- if low[lI - 1 - j] <= lineValue and high[lI - 1 - j] >= lineValue
- count += volume[lI - 1 - j]
- array.push(counts, count)
- maxCount = array.max(counts)
- sortedIndices = array.sort_indices(counts, order.descending)
- var float activitySlope = 0.0
- minActivityThreshold = maxCount * 0.1
- displayedActivityLines = 0
- for idx2 = 0 to nFills - 1
- if displayedActivityLines >= numActivityLines
- break
- index = array.get(sortedIndices, idx2)
- count = array.get(counts, index)
- if count >= minActivityThreshold
- actY1 = calcLineValue(lSP, uSP, index + 0.5, nFills)
- actY2 = calcLineValue(lEP, uEP, index + 0.5, nFills)
- if idx2 == 0
- activitySlope := (f_adjust(actY2) - f_adjust(actY1)) / (bar_index - (bar_index - lI + 1))
- percent = count / maxCount
- lineColor = useCustomColor ? customColor : color_from_gradient(percent, loActColor, hiActColor)
- lineStyle = actLineStyle == "Solid" ? line.style_solid : actLineStyle == "Dotted" ? line.style_dotted : line.style_dashed
- startX = showProfile ? math.min(bar_index, bar_index - lI + 1 + math.round((count / maxCount) * math.round(lI / 5))) : bar_index - lI + 1
- startY = showProfile ? f_unadjust(f_adjust(actY1) + activitySlope * (startX - (bar_index - lI + 1))) : actY1
- actLine = line.new(startX, startY, bar_index, actY2, color=lineColor, width=actLineWidth, style=lineStyle, extend=extend.right)
- array.push(activityLines, actLine)
- displayedActivityLines += 1
- if showProfile
- profileLength = math.round(lI / 5)
- for idx3 = 0 to nFills - 1
- y1_top = calcLineValue(lSP, uSP, idx3, nFills)
- y1_bottom = calcLineValue(lSP, uSP, idx3 + 1, nFills)
- y2_top = calcLineValue(lEP, uEP, idx3, nFills)
- y2_bottom = calcLineValue(lEP, uEP, idx3 + 1, nFills)
- count = array.get(counts, idx3)
- percent = count / maxCount
- fillColor := color_from_gradient(percent, loActColor, hiActColor)
- lineLength = math.round((count / maxCount) * profileLength)
- x2 = math.min(bar_index, bar_index - lI + 1 + lineLength)
- topLine = line.new(bar_index - lI + 1, y1_top, x2, f_unadjust(f_adjust(y1_top) + activitySlope * lineLength), color=color.new(fillColor, 100))
- bottomLine = line.new(bar_index - lI + 1, y1_bottom, x2, f_unadjust(f_adjust(y1_bottom) + activitySlope * lineLength), color=color.new(fillColor, 100))
- profileFill = linefill.new(topLine, bottomLine, color=fillColor)
- array.push(profileFills, profileFill)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement