Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // © Julien_Eche
- //@version=5
- indicator("Strongest Trendline", shorttitle="Strongest Trendline", overlay=true)
- lengthInput1 = input.int(200, title="Length 1")
- lengthInput2 = input.int(400, title="Length 2")
- lengthInput3 = input.int(600, title="Length 3")
- lengthInput4 = input.int(800, title="Length 4")
- lengthInput5 = input.int(1000, title="Length 5")
- lengthInput6 = input.int(1200, title="Length 6")
- lengthInput7 = input.int(1400, title="Length 7")
- lengthInput8 = input.int(1600, title="Length 8")
- lengthInput9 = input.int(1800, title="Length 9")
- lengthInput10 = input.int(2000, title="Length 10")
- sourceInput = input.source(close, title="Source")
- group1 = "Display Settings"
- showPearsonInput = input.bool(true, "Show Confidence level")
- extendLeftInput = input.bool(false, "Extend TrendLine Left")
- extendRightInput = input.bool(true, "Extend TrendLine Right")
- extendStyle = switch
- extendLeftInput and extendRightInput => extend.both
- extendLeftInput => extend.left
- extendRightInput => extend.right
- => extend.none
- group2 = "Line Settings"
- lineWidth = input.int(1, "Line Width")
- lineStyle = input.string("Solid", "Line Style", options=["Solid", "Dotted", "Dashed"])
- // Function to calculate slope
- 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 = math.log(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]
- // Function to calculate deviation
- 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 = math.log(high[j]) - val
- if price > upDev
- upDev := price
- price := val - math.log(low[j])
- if price > dnDev
- dnDev := price
- price := math.log(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]
- // Calculate slopes and deviations for each length
- [s1, a1, i1] = calcSlope(sourceInput, lengthInput1)
- [s2, a2, i2] = calcSlope(sourceInput, lengthInput2)
- [s3, a3, i3] = calcSlope(sourceInput, lengthInput3)
- [s4, a4, i4] = calcSlope(sourceInput, lengthInput4)
- [s5, a5, i5] = calcSlope(sourceInput, lengthInput5)
- [s6, a6, i6] = calcSlope(sourceInput, lengthInput6)
- [s7, a7, i7] = calcSlope(sourceInput, lengthInput7)
- [s8, a8, i8] = calcSlope(sourceInput, lengthInput8)
- [s9, a9, i9] = calcSlope(sourceInput, lengthInput9)
- [s10, a10, i10] = calcSlope(sourceInput, lengthInput10)
- // Calculate deviations for each length
- [stdDev1, pearsonR1, upDev1, dnDev1] = calcDev(sourceInput, lengthInput1, s1, a1, i1)
- [stdDev2, pearsonR2, upDev2, dnDev2] = calcDev(sourceInput, lengthInput2, s2, a2, i2)
- [stdDev3, pearsonR3, upDev3, dnDev3] = calcDev(sourceInput, lengthInput3, s3, a3, i3)
- [stdDev4, pearsonR4, upDev4, dnDev4] = calcDev(sourceInput, lengthInput4, s4, a4, i4)
- [stdDev5, pearsonR5, upDev5, dnDev5] = calcDev(sourceInput, lengthInput5, s5, a5, i5)
- [stdDev6, pearsonR6, upDev6, dnDev6] = calcDev(sourceInput, lengthInput6, s6, a6, i6)
- [stdDev7, pearsonR7, upDev7, dnDev7] = calcDev(sourceInput, lengthInput7, s7, a7, i7)
- [stdDev8, pearsonR8, upDev8, dnDev8] = calcDev(sourceInput, lengthInput8, s8, a8, i8)
- [stdDev9, pearsonR9, upDev9, dnDev9] = calcDev(sourceInput, lengthInput9, s9, a9, i9)
- [stdDev10, pearsonR10, upDev10, dnDev10] = calcDev(sourceInput, lengthInput10, s10, a10, i10)
- // Find the highest Pearson's R value
- highestPearsonR = math.max(pearsonR1, pearsonR2, pearsonR3, pearsonR4, pearsonR5, pearsonR6, pearsonR7, pearsonR8, pearsonR9, pearsonR10)
- selectedLength = highestPearsonR == pearsonR1 ? lengthInput1 : (highestPearsonR == pearsonR2 ? lengthInput2 : (highestPearsonR == pearsonR3 ? lengthInput3 : (highestPearsonR == pearsonR4 ? lengthInput4 : (highestPearsonR == pearsonR5 ? lengthInput5 : (highestPearsonR == pearsonR6 ? lengthInput6 : (highestPearsonR == pearsonR7 ? lengthInput7 : (highestPearsonR == pearsonR8 ? lengthInput8 : (highestPearsonR == pearsonR9 ? lengthInput9 : lengthInput10))))))))
- selectedS = highestPearsonR == pearsonR1 ? s1 : (highestPearsonR == pearsonR2 ? s2 : (highestPearsonR == pearsonR3 ? s3 : (highestPearsonR == pearsonR4 ? s4 : (highestPearsonR == pearsonR5 ? s5 : (highestPearsonR == pearsonR6 ? s6 : (highestPearsonR == pearsonR7 ? s7 : (highestPearsonR == pearsonR8 ? s8 : (highestPearsonR == pearsonR9 ? s9 : s10))))))))
- selectedI = highestPearsonR == pearsonR1 ? i1 : (highestPearsonR == pearsonR2 ? i2 : (highestPearsonR == pearsonR3 ? i3 : (highestPearsonR == pearsonR4 ? i4 : (highestPearsonR == pearsonR5 ? i5 : (highestPearsonR == pearsonR6 ? i6 : (highestPearsonR == pearsonR7 ? i7 : (highestPearsonR == pearsonR8 ? i8 : (highestPearsonR == pearsonR9 ? i9 : i10))))))))
- selectedStdDev = highestPearsonR == pearsonR1 ? stdDev1 : (highestPearsonR == pearsonR2 ? stdDev2 : (highestPearsonR == pearsonR3 ? stdDev3 : (highestPearsonR == pearsonR4 ? stdDev4 : (highestPearsonR == pearsonR5 ? stdDev5 : (highestPearsonR == pearsonR6 ? stdDev6 : (highestPearsonR == pearsonR7 ? stdDev7 : (highestPearsonR == pearsonR8 ? stdDev8 : (highestPearsonR == pearsonR9 ? stdDev9 : stdDev10))))))))
- selectedUpDev = highestPearsonR == pearsonR1 ? upDev1 : (highestPearsonR == pearsonR2 ? upDev2 : (highestPearsonR == pearsonR3 ? upDev3 : (highestPearsonR == pearsonR4 ? upDev4 : (highestPearsonR == pearsonR5 ? upDev5 : (highestPearsonR == pearsonR6 ? upDev6 : (highestPearsonR == pearsonR7 ? upDev7 : (highestPearsonR == pearsonR8 ? upDev8 : (highestPearsonR == pearsonR9 ? upDev9 : upDev10))))))))
- selectedDnDev = highestPearsonR == pearsonR1 ? dnDev1 : (highestPearsonR == pearsonR2 ? dnDev2 : (highestPearsonR == pearsonR3 ? dnDev3 : (highestPearsonR == pearsonR4 ? dnDev4 : (highestPearsonR == pearsonR5 ? dnDev5 : (highestPearsonR == pearsonR6 ? dnDev6 : (highestPearsonR == pearsonR7 ? dnDev7 : (highestPearsonR == pearsonR8 ? dnDev8 : (highestPearsonR == pearsonR9 ? dnDev9 : dnDev10))))))))
- // Select the corresponding values for the highest Pearson's R
- startPrice = math.exp(selectedI + selectedS * (selectedLength - 1))
- endPrice = math.exp(selectedI)
- // Store start and end prices in variables
- StartPrice = startPrice
- EndPrice = endPrice
- if na(StartPrice)
- StartPrice := startPrice
- if na(EndPrice)
- EndPrice := endPrice
- // Define confidence levels
- var string confidenceLevel = ""
- if highestPearsonR >= 0.75 and highestPearsonR < 0.85
- confidenceLevel := "Weak Trend"
- else if highestPearsonR < 0.75
- confidenceLevel := "Very Weak Trend"
- else if highestPearsonR >= 0.85 and highestPearsonR < 0.9
- confidenceLevel := "Moderate Trend"
- else if highestPearsonR >= 0.9 and highestPearsonR < 0.95
- confidenceLevel := "Strong Trend"
- else if highestPearsonR >= 0.95 and highestPearsonR < 0.98
- confidenceLevel := "Very Strong Trend"
- else if highestPearsonR >= 0.98
- confidenceLevel := "Ultra Strong Trend"
- // Color the line based on direction
- lineColor = StartPrice > EndPrice ? color.red : color.green
- // Create or update the line
- var line baseLine = na
- if na(baseLine) and not na(startPrice)
- baseLine := line.new(bar_index - selectedLength + 1, startPrice, bar_index, endPrice, width=lineWidth, extend=extendStyle, color=lineColor, style=lineStyle == "Dotted" ? line.style_dotted : (lineStyle == "Dashed" ? line.style_dashed : line.style_solid))
- else
- line.set_xy1(baseLine, bar_index - selectedLength + 1, startPrice)
- line.set_xy2(baseLine, bar_index, endPrice)
- line.set_width(baseLine, lineWidth)
- line.set_extend(baseLine, extendStyle)
- line.set_color(baseLine, lineColor)
- line.set_style(baseLine, lineStyle == "Dotted" ? line.style_dotted : (lineStyle == "Dashed" ? line.style_dashed : line.style_solid))
- // Pearson's R label
- var label confLevelLabel = na
- label.delete(confLevelLabel[1])
- if showPearsonInput and not na(highestPearsonR)
- confLevelLabel := label.new(bar_index - selectedLength + 1, StartPrice, confidenceLevel, color=color.new(color.white, 100), textcolor=lineColor, size=size.normal, style=label.style_label_up)
- label.delete(confLevelLabel[1])
Advertisement
Add Comment
Please, Sign In to add comment