xmd79

Strongest Trendline

May 18th, 2023
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. // © Julien_Eche
  2. //@version=5
  3.  
  4. indicator("Strongest Trendline", shorttitle="Strongest Trendline", overlay=true)
  5.  
  6. lengthInput1 = input.int(200, title="Length 1")
  7. lengthInput2 = input.int(400, title="Length 2")
  8. lengthInput3 = input.int(600, title="Length 3")
  9. lengthInput4 = input.int(800, title="Length 4")
  10. lengthInput5 = input.int(1000, title="Length 5")
  11. lengthInput6 = input.int(1200, title="Length 6")
  12. lengthInput7 = input.int(1400, title="Length 7")
  13. lengthInput8 = input.int(1600, title="Length 8")
  14. lengthInput9 = input.int(1800, title="Length 9")
  15. lengthInput10 = input.int(2000, title="Length 10")
  16.  
  17. sourceInput = input.source(close, title="Source")
  18.  
  19. group1 = "Display Settings"
  20. showPearsonInput = input.bool(true, "Show Confidence level")
  21. extendLeftInput = input.bool(false, "Extend TrendLine Left")
  22. extendRightInput = input.bool(true, "Extend TrendLine Right")
  23. extendStyle = switch
  24. extendLeftInput and extendRightInput => extend.both
  25. extendLeftInput => extend.left
  26. extendRightInput => extend.right
  27. => extend.none
  28.  
  29. group2 = "Line Settings"
  30. lineWidth = input.int(1, "Line Width")
  31. lineStyle = input.string("Solid", "Line Style", options=["Solid", "Dotted", "Dashed"])
  32.  
  33. // Function to calculate slope
  34. calcSlope(source, length) =>
  35. max_bars_back(source, 5000)
  36. if not barstate.islast or length <= 1
  37. [float(na), float(na), float(na)]
  38. else
  39. sumX = 0.0
  40. sumY = 0.0
  41. sumXSqr = 0.0
  42. sumXY = 0.0
  43. for i = 0 to length - 1 by 1
  44. val = math.log(source[i])
  45. per = i + 1.0
  46. sumX += per
  47. sumY += val
  48. sumXSqr += per * per
  49. sumXY += val * per
  50. slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
  51. average = sumY / length
  52. intercept = average - slope * sumX / length + slope
  53. [slope, average, intercept]
  54.  
  55. // Function to calculate deviation
  56. calcDev(source, length, slope, average, intercept) =>
  57. upDev = 0.0
  58. dnDev = 0.0
  59. stdDevAcc = 0.0
  60. dsxx = 0.0
  61. dsyy = 0.0
  62. dsxy = 0.0
  63. periods = length - 1
  64. daY = intercept + slope * periods / 2
  65. val = intercept
  66. for j = 0 to periods by 1
  67. price = math.log(high[j]) - val
  68. if price > upDev
  69. upDev := price
  70. price := val - math.log(low[j])
  71. if price > dnDev
  72. dnDev := price
  73. price := math.log(source[j])
  74. dxt = price - average
  75. dyt = val - daY
  76. price -= val
  77. stdDevAcc += price * price
  78. dsxx += dxt * dxt
  79. dsyy += dyt * dyt
  80. dsxy += dxt * dyt
  81. val += slope
  82. stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
  83. pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
  84. [stdDev, pearsonR, upDev, dnDev]
  85.  
  86. // Calculate slopes and deviations for each length
  87. [s1, a1, i1] = calcSlope(sourceInput, lengthInput1)
  88. [s2, a2, i2] = calcSlope(sourceInput, lengthInput2)
  89. [s3, a3, i3] = calcSlope(sourceInput, lengthInput3)
  90. [s4, a4, i4] = calcSlope(sourceInput, lengthInput4)
  91. [s5, a5, i5] = calcSlope(sourceInput, lengthInput5)
  92. [s6, a6, i6] = calcSlope(sourceInput, lengthInput6)
  93. [s7, a7, i7] = calcSlope(sourceInput, lengthInput7)
  94. [s8, a8, i8] = calcSlope(sourceInput, lengthInput8)
  95. [s9, a9, i9] = calcSlope(sourceInput, lengthInput9)
  96. [s10, a10, i10] = calcSlope(sourceInput, lengthInput10)
  97.  
  98. // Calculate deviations for each length
  99. [stdDev1, pearsonR1, upDev1, dnDev1] = calcDev(sourceInput, lengthInput1, s1, a1, i1)
  100. [stdDev2, pearsonR2, upDev2, dnDev2] = calcDev(sourceInput, lengthInput2, s2, a2, i2)
  101. [stdDev3, pearsonR3, upDev3, dnDev3] = calcDev(sourceInput, lengthInput3, s3, a3, i3)
  102. [stdDev4, pearsonR4, upDev4, dnDev4] = calcDev(sourceInput, lengthInput4, s4, a4, i4)
  103. [stdDev5, pearsonR5, upDev5, dnDev5] = calcDev(sourceInput, lengthInput5, s5, a5, i5)
  104. [stdDev6, pearsonR6, upDev6, dnDev6] = calcDev(sourceInput, lengthInput6, s6, a6, i6)
  105. [stdDev7, pearsonR7, upDev7, dnDev7] = calcDev(sourceInput, lengthInput7, s7, a7, i7)
  106. [stdDev8, pearsonR8, upDev8, dnDev8] = calcDev(sourceInput, lengthInput8, s8, a8, i8)
  107. [stdDev9, pearsonR9, upDev9, dnDev9] = calcDev(sourceInput, lengthInput9, s9, a9, i9)
  108. [stdDev10, pearsonR10, upDev10, dnDev10] = calcDev(sourceInput, lengthInput10, s10, a10, i10)
  109.  
  110. // Find the highest Pearson's R value
  111. highestPearsonR = math.max(pearsonR1, pearsonR2, pearsonR3, pearsonR4, pearsonR5, pearsonR6, pearsonR7, pearsonR8, pearsonR9, pearsonR10)
  112.  
  113. 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))))))))
  114. 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))))))))
  115. 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))))))))
  116. 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))))))))
  117. 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))))))))
  118. 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))))))))
  119.  
  120. // Select the corresponding values for the highest Pearson's R
  121. startPrice = math.exp(selectedI + selectedS * (selectedLength - 1))
  122. endPrice = math.exp(selectedI)
  123.  
  124.  
  125. // Store start and end prices in variables
  126. StartPrice = startPrice
  127. EndPrice = endPrice
  128. if na(StartPrice)
  129. StartPrice := startPrice
  130. if na(EndPrice)
  131. EndPrice := endPrice
  132.  
  133. // Define confidence levels
  134. var string confidenceLevel = ""
  135. if highestPearsonR >= 0.75 and highestPearsonR < 0.85
  136. confidenceLevel := "Weak Trend"
  137. else if highestPearsonR < 0.75
  138. confidenceLevel := "Very Weak Trend"
  139. else if highestPearsonR >= 0.85 and highestPearsonR < 0.9
  140. confidenceLevel := "Moderate Trend"
  141. else if highestPearsonR >= 0.9 and highestPearsonR < 0.95
  142. confidenceLevel := "Strong Trend"
  143. else if highestPearsonR >= 0.95 and highestPearsonR < 0.98
  144. confidenceLevel := "Very Strong Trend"
  145. else if highestPearsonR >= 0.98
  146. confidenceLevel := "Ultra Strong Trend"
  147.  
  148. // Color the line based on direction
  149. lineColor = StartPrice > EndPrice ? color.red : color.green
  150.  
  151. // Create or update the line
  152. var line baseLine = na
  153. if na(baseLine) and not na(startPrice)
  154. 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))
  155. else
  156. line.set_xy1(baseLine, bar_index - selectedLength + 1, startPrice)
  157. line.set_xy2(baseLine, bar_index, endPrice)
  158. line.set_width(baseLine, lineWidth)
  159. line.set_extend(baseLine, extendStyle)
  160. line.set_color(baseLine, lineColor)
  161. line.set_style(baseLine, lineStyle == "Dotted" ? line.style_dotted : (lineStyle == "Dashed" ? line.style_dashed : line.style_solid))
  162.  
  163. // Pearson's R label
  164. var label confLevelLabel = na
  165. label.delete(confLevelLabel[1])
  166. if showPearsonInput and not na(highestPearsonR)
  167. 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)
  168.  
  169. label.delete(confLevelLabel[1])
Advertisement
Add Comment
Please, Sign In to add comment