01-regression-trend-with-fibonacci.pine
Copy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syoungs
//@version=5
indicator("Regression Trend with Fibonacci", overlay = true)
group1 = "Linear Regression"
sourceInput = input.source(close, "", inline="1", group=group1)
lengthInput = input.int(200, "", 2, 500, 1, inline="1", group=group1)
useUpperDevInput = input.bool(true, title="Upper", inline = "2", group = group1)
upperMultInput = input.float(2.0, title="", inline = "2", group = group1)
useLowerDevInput = input.bool(true, title="Lower", inline = "2", group = group1)
lowerMultInput = input.float(2.0, title="", inline = "2", group = group1)
group2 = "Display Settings"
showPearsonInput = input.bool(true, "Pearson's R", inline = "1", group = group2)
showPearsonColor = input.color(color.yellow, "", inline = "1", group = group2)
extendLeftInput = input.bool(false, "Extend Left", inline = "1", group = group2)
extendRightInput = input.bool(false, "Extend Right", inline = "1", group = group2)
extendStyle = switch
extendLeftInput and extendRightInput => extend.both
extendLeftInput => extend.left
extendRightInput => extend.right
=> extend.none
group3 = "Color Settings"
colorUpper = input.color(color.white, "Upper", inline = "1", group = group3)
colorBase = input.color(color.white, "Base", inline = "1", group = group3)
colorLower = input.color(color.white, "Lower", inline = "1", group = group3)
calcSlope(source, length) =>
max_bars_back(source, 5000)
if not barstate.islast 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]
[s, a, i] = calcSlope(sourceInput, lengthInput)
startPrice = i + s * (lengthInput - 1)
endPrice = i
var line baseLine = na
if na(baseLine) and not na(startPrice)
baseLine := line.new(bar_index - lengthInput + 1, startPrice, bar_index, endPrice, width=1, extend=extendStyle, color=colorBase, style=line.style_dashed)
else
line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice)
line.set_xy2(baseLine, bar_index, endPrice)
na
uptrend = startPrice <= endPrice
calcDev(source, length, slope, average, intercept) =>
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 = high[j] - val
if price > upDev
upDev := price
price := val - low[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 = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]
[stdDev, pearsonR, upDev, dnDev] = calcDev(sourceInput, lengthInput, s, a, i)
upperStartPrice = startPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
upperEndPrice = endPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
var line upper = na
lowerStartPrice = startPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
lowerEndPrice = endPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
var line lower = na
if na(upper) and not na(upperStartPrice)
upper := line.new(bar_index - lengthInput + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extendStyle, color=colorUpper)
else
line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice)
line.set_xy2(upper, bar_index, upperEndPrice)
na
if na(lower) and not na(lowerStartPrice)
lower := line.new(bar_index - lengthInput + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extendStyle, color=colorLower)
else
line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice)
line.set_xy2(lower, bar_index, lowerEndPrice)
na
channel_width = upperStartPrice - lowerStartPrice
// Pearson's R
var label r = na
label.delete(r[1])
if showPearsonInput and not na(pearsonR)
r := label.new(bar_index - lengthInput + 1, uptrend ? lowerStartPrice : upperStartPrice, str.tostring(pearsonR, "R #.##"), color = color.new(color.white, 100), textcolor=showPearsonColor, size=size.normal, style=label.style_none)
// Fibonacci Ratio
group4 = "Fibonacci IN"
fib_in_1 = input.float(0.786, "1", minval=0.001, maxval = 0.999, step=0.001, inline="1", group=group4)
// fib_in_1_p = plot(lower+fib_in_1*dev*2, "IN1", #787b86)
fib_in_2 = input.float(0.618, "2", minval=0.001, maxval = 0.999, step=0.001, inline="1", group=group4)
// fib_in_2_p = plot(lower+fib_in_2*dev*2, "IN2", #787b86)
fib_in_3 = input.float(0.382, "3", minval=0.001, maxval = 0.999, step=0.001, inline="2", group=group4)
// fib_in_3_p = plot(lower+fib_in_3*dev*2, "IN3", #787b86)
fib_in_4 = input.float(0.236, "4", minval=0.001, maxval = 0.999, step=0.001, inline="2", group=group4)
// fib_in_4_p = plot(lower+fib_in_4*dev*2, "IN4", #787b86)
group5 = "Fibonacci EX"
fib_ex_1 = input.float(1.272, "1", minval=-9.999, maxval = 9.999, step=0.001, inline="1", group=group5)
// fib_ex_1_p = plot(lower+fib_ex_1*dev*2, "EX1", #f23645)
fib_ex_2 = input.float(-0.272, "2", minval=-9.999, maxval = 9.999, step=0.001, inline="1", group=group5)
// fib_ex_2_p = plot(lower+fib_ex_2*dev*2, "EX2", #ff9800)
fib_ex_3 = input.float(1.618, "3", minval=-9.999, maxval = 9.999, step=0.001, inline="2", group=group5)
// fib_ex_3_p = plot(lower+fib_ex_3*dev*2, "EX3", #ff9800)
fib_ex_4 = input.float(-0.618, "4", minval=-9.999, maxval = 9.999, step=0.001, inline="2", group=group5)
// fib_ex_4_p = plot(lower+fib_ex_4*dev*2, "EX4", #f23645)
group6 = "Fibonacci Color"
in_color = input.color(#787b86, "IN", inline="1", group=group6)
ex12_color = input.color(#ff9800, "EX1-2", inline="1", group=group6)
ex34_color = input.color(#f23645, "EX3-4", inline="1", group=group6)
var line fib_in_1_line = na
var label fib_in_1_label = na
var line fib_in_2_line = na
var label fib_in_2_label = na
var line fib_in_3_line = na
var label fib_in_3_label = na
var line fib_in_4_line = na
var label fib_in_4_label = na
var line fib_ex_1_line = na
var label fib_ex_1_label = na
var line fib_ex_2_line = na
var label fib_ex_2_label = na
var line fib_ex_3_line = na
var label fib_ex_3_label = na
var line fib_ex_4_line = na
var label fib_ex_4_label = na
if na(fib_in_1_line) and not na(startPrice)
if uptrend
fib_in_1_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_1 * channel_width, bar_index, lowerEndPrice + fib_in_1 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_1_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_1 * channel_width, text=str.tostring(fib_in_1, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_2_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_2 * channel_width, bar_index, lowerEndPrice + fib_in_2 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_2_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_2 * channel_width, text=str.tostring(fib_in_2, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_3_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_3 * channel_width, bar_index, lowerEndPrice + fib_in_3 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_3_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_3 * channel_width, text=str.tostring(fib_in_3, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_4_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_4 * channel_width, bar_index, lowerEndPrice + fib_in_4 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_4_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_in_4 * channel_width, text=str.tostring(fib_in_4, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_ex_1_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_1 * channel_width, bar_index, lowerEndPrice + fib_ex_1 * channel_width, extend=extendStyle, color=ex12_color, style=line.style_dashed, width=1)
fib_ex_1_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_1 * channel_width, text=str.tostring(fib_ex_1, "#.###"), style=label.style_none, textcolor = ex12_color, textalign = text.align_center)
fib_ex_2_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_2 * channel_width, bar_index, lowerEndPrice + fib_ex_2 * channel_width, extend=extendStyle, color=ex12_color, style=line.style_dashed, width=1)
fib_ex_2_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_2 * channel_width, text=str.tostring(fib_ex_2, "#.###"), style=label.style_none, textcolor = ex12_color, textalign = text.align_center)
fib_ex_3_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_3 * channel_width, bar_index, lowerEndPrice + fib_ex_3 * channel_width, extend=extendStyle, color=ex34_color, style=line.style_dashed, width=1)
fib_ex_3_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_3 * channel_width, text=str.tostring(fib_ex_3, "#.###"), style=label.style_none, textcolor = ex34_color, textalign = text.align_center)
fib_ex_4_line := line.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_4 * channel_width, bar_index, lowerEndPrice + fib_ex_4 * channel_width, extend=extendStyle, color=ex34_color, style=line.style_dashed, width=1)
fib_ex_4_label := label.new(bar_index - lengthInput + 1, lowerStartPrice + fib_ex_4 * channel_width, text=str.tostring(fib_ex_4, "#.###"), style=label.style_none, textcolor = ex34_color, textalign = text.align_center)
else
fib_in_1_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_1 * channel_width, bar_index, upperEndPrice - fib_in_1 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_1_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_1 * channel_width, text=str.tostring(fib_in_1, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_2_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_2 * channel_width, bar_index, upperEndPrice - fib_in_2 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_2_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_2 * channel_width, text=str.tostring(fib_in_2, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_3_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_3 * channel_width, bar_index, upperEndPrice - fib_in_3 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_3_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_3 * channel_width, text=str.tostring(fib_in_3, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_in_4_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_4 * channel_width, bar_index, upperEndPrice - fib_in_4 * channel_width, extend=extendStyle, color=in_color, style=line.style_dashed, width=1)
fib_in_4_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_in_4 * channel_width, text=str.tostring(fib_in_4, "#.###"), style=label.style_none, textcolor = in_color, textalign = text.align_center)
fib_ex_1_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_1 * channel_width, bar_index, upperEndPrice - fib_ex_1 * channel_width, extend=extendStyle, color=ex12_color, style=line.style_dashed, width=1)
fib_ex_1_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_1 * channel_width, text=str.tostring(fib_ex_1, "#.###"), style=label.style_none, textcolor = ex12_color, textalign = text.align_center)
fib_ex_2_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_2 * channel_width, bar_index, upperEndPrice - fib_ex_2 * channel_width, extend=extendStyle, color=ex12_color, style=line.style_dashed, width=1)
fib_ex_2_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_2 * channel_width, text=str.tostring(fib_ex_2, "#.###"), style=label.style_none, textcolor = ex12_color, textalign = text.align_center)
fib_ex_3_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_3 * channel_width, bar_index, upperEndPrice - fib_ex_3 * channel_width, extend=extendStyle, color=ex34_color, style=line.style_dashed, width=1)
fib_ex_3_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_3 * channel_width, text=str.tostring(fib_ex_3, "#.###"), style=label.style_none, textcolor = ex34_color, textalign = text.align_center)
fib_ex_4_line := line.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_4 * channel_width, bar_index, upperEndPrice - fib_ex_4 * channel_width, extend=extendStyle, color=ex34_color, style=line.style_dashed, width=1)
fib_ex_4_label := label.new(bar_index - lengthInput + 1, upperStartPrice - fib_ex_4 * channel_width, text=str.tostring(fib_ex_4, "#.###"), style=label.style_none, textcolor = ex34_color, textalign = text.align_center)
else
if uptrend
line.set_xy1(fib_in_1_line, bar_index - lengthInput + 1, lowerStartPrice + fib_in_1 * channel_width)
line.set_xy2(fib_in_1_line, bar_index, lowerEndPrice + fib_in_1 * channel_width)
label.set_xy(fib_in_1_label, bar_index - lengthInput + 1, lowerStartPrice + fib_in_1 * channel_width)
line.set_xy1(fib_in_2_line, bar_index - lengthInput + 1, lowerStartPrice + fib_in_2 * channel_width)
line.set_xy2(fib_in_2_line, bar_index, lowerEndPrice + fib_in_2 * channel_width)
label.set_xy(fib_in_2_label, bar_index - lengthInput + 1, lowerStartPrice + fib_in_2 * channel_width)
line.set_xy1(fib_in_3_line, bar_index - lengthInput + 1, lowerStartPrice + fib_in_3 * channel_width)
line.set_xy2(fib_in_3_line, bar_index, lowerEndPrice + fib_in_3 * channel_width)
label.set_xy(fib_in_3_label, bar_index - lengthInput + 1, lowerStartPrice + fib_in_3 * channel_width)
line.set_xy1(fib_in_4_line, bar_index - lengthInput + 1, lowerStartPrice + fib_in_4 * channel_width)
line.set_xy2(fib_in_4_line, bar_index, lowerEndPrice + fib_in_4 * channel_width)
label.set_xy(fib_in_4_label, bar_index - lengthInput + 1, lowerStartPrice + fib_in_4 * channel_width)
line.set_xy1(fib_ex_1_line, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_1 * channel_width)
line.set_xy2(fib_ex_1_line, bar_index, lowerEndPrice + fib_ex_1 * channel_width)
label.set_xy(fib_ex_1_label, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_1 * channel_width)
line.set_xy1(fib_ex_2_line, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_2 * channel_width)
line.set_xy2(fib_ex_2_line, bar_index, lowerEndPrice + fib_ex_2 * channel_width)
label.set_xy(fib_ex_2_label, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_2 * channel_width)
line.set_xy1(fib_ex_3_line, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_3 * channel_width)
line.set_xy2(fib_ex_3_line, bar_index, lowerEndPrice + fib_ex_3 * channel_width)
label.set_xy(fib_ex_3_label, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_3 * channel_width)
line.set_xy1(fib_ex_4_line, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_4 * channel_width)
line.set_xy2(fib_ex_4_line, bar_index, lowerEndPrice + fib_ex_4 * channel_width)
label.set_xy(fib_ex_4_label, bar_index - lengthInput + 1, lowerStartPrice + fib_ex_4 * channel_width)
else
line.set_xy1(fib_in_1_line, bar_index - lengthInput + 1, upperStartPrice - fib_in_1 * channel_width)
line.set_xy2(fib_in_1_line, bar_index, upperEndPrice - fib_in_1 * channel_width)
label.set_xy(fib_in_1_label, bar_index - lengthInput + 1, upperStartPrice - fib_in_1 * channel_width)
line.set_xy1(fib_in_2_line, bar_index - lengthInput + 1, upperStartPrice - fib_in_2 * channel_width)
line.set_xy2(fib_in_2_line, bar_index, upperEndPrice - fib_in_2 * channel_width)
label.set_xy(fib_in_2_label, bar_index - lengthInput + 1, upperStartPrice - fib_in_2 * channel_width)
line.set_xy1(fib_in_3_line, bar_index - lengthInput + 1, upperStartPrice - fib_in_3 * channel_width)
line.set_xy2(fib_in_3_line, bar_index, upperEndPrice - fib_in_3 * channel_width)
label.set_xy(fib_in_3_label, bar_index - lengthInput + 1, upperStartPrice - fib_in_3 * channel_width)
line.set_xy1(fib_in_4_line, bar_index - lengthInput + 1, upperStartPrice - fib_in_4 * channel_width)
line.set_xy2(fib_in_4_line, bar_index, upperEndPrice - fib_in_4 * channel_width)
label.set_xy(fib_in_4_label, bar_index - lengthInput + 1, upperStartPrice - fib_in_4 * channel_width)
line.set_xy1(fib_ex_1_line, bar_index - lengthInput + 1, upperStartPrice - fib_ex_1 * channel_width)
line.set_xy2(fib_ex_1_line, bar_index, upperEndPrice - fib_ex_1 * channel_width)
label.set_xy(fib_ex_1_label, bar_index - lengthInput + 1, upperStartPrice - fib_ex_1 * channel_width)
line.set_xy1(fib_ex_2_line, bar_index - lengthInput + 1, upperStartPrice - fib_ex_2 * channel_width)
line.set_xy2(fib_ex_2_line, bar_index, upperEndPrice - fib_ex_2 * channel_width)
label.set_xy(fib_ex_2_label, bar_index - lengthInput + 1, upperStartPrice - fib_ex_2 * channel_width)
line.set_xy1(fib_ex_3_line, bar_index - lengthInput + 1, upperStartPrice - fib_ex_3 * channel_width)
line.set_xy2(fib_ex_3_line, bar_index, upperEndPrice - fib_ex_3 * channel_width)
label.set_xy(fib_ex_3_label, bar_index - lengthInput + 1, upperStartPrice - fib_ex_3 * channel_width)
line.set_xy1(fib_ex_4_line, bar_index - lengthInput + 1, upperStartPrice - fib_ex_4 * channel_width)
line.set_xy2(fib_ex_4_line, bar_index, upperEndPrice - fib_ex_4 * channel_width)
label.set_xy(fib_ex_4_label, bar_index - lengthInput + 1, upperStartPrice - fib_ex_4 * channel_width)
Show all 257 lines