Advertisement
xmd79

Double Linear Regression Channel

Oct 28th, 2023
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. //@version=5
  2. indicator("Double Linear Regression Channel", shorttitle="Double LinReg", overlay=true)
  3.  
  4. lengthInput = input.int(100, title="Length", minval = 1, maxval = 5000)
  5. sourceInput = input.source(close, title="Source")
  6.  
  7. group1 = "Channel Settings 1"
  8. useUpperDevInput1 = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group1)
  9. upperMultInput1 = input.float(2.0, title="", inline = "Upper Deviation", group = group1)
  10. useLowerDevInput1 = input.bool(true, title="Lower Deviation", inline = "Lower Deviation", group = group1)
  11. lowerMultInput1 = input.float(2.0, title="", inline = "Lower Deviation", group = group1)
  12.  
  13. group2 = "Channel Settings 2"
  14. useUpperDevInput2 = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group2)
  15. upperMultInput2 = input.float(2.0, title="", inline = "Upper Deviation", group = group2)
  16. useLowerDevInput2 = input.bool(true, title="Lower Deviation", inline = "Lower Deviation", group = group2)
  17. lowerMultInput2 = input.float(2.0, title="", inline = "Lower Deviation", group = group2)
  18.  
  19. group3 = "Display Settings"
  20. showPearsonInput = input.bool(true, "Show Pearson's R", group = group3)
  21. extendLeftInput = input.bool(false, "Extend Lines Left", group = group3)
  22. extendRightInput = input.bool(true, "Extend Lines Right", group = group3)
  23. extendStyle = switch
  24. extendLeftInput and extendRightInput => extend.both
  25. extendLeftInput => extend.left
  26. extendRightInput => extend.right
  27. => extend.none
  28.  
  29. group4 = "Color Settings"
  30. colorUpper1 = input.color(color.new(color.blue, 85), "Upper Color 1", inline = group4, group = group4)
  31. colorLower1 = input.color(color.new(color.red, 85), "Lower Color 1", inline = group4, group = group4)
  32. colorUpper2 = input.color(color.new(color.green, 85), "Upper Color 2", inline = group4, group = group4)
  33. colorLower2 = input.color(color.new(color.orange, 85), "Lower Color 2", inline = group4, group = group4)
  34.  
  35. calcSlope(source, length) =>
  36. max_bars_back(source, 5000)
  37. if not barstate.islast or length <= 1
  38. [float(na), float(na), float(na)]
  39. else
  40. sumX = 0.0
  41. sumY = 0.0
  42. sumXSqr = 0.0
  43. sumXY = 0.0
  44. for i = 0 to length - 1 by 1
  45. val = source[i]
  46. per = i + 1.0
  47. sumX += per
  48. sumY += val
  49. sumXSqr += per * per
  50. sumXY += val * per
  51. slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
  52. average = sumY / length
  53. intercept = average - slope * sumX / length + slope
  54. [slope, average, intercept]
  55.  
  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 = high[j] - val
  68. if price > upDev
  69. upDev := price
  70. price := val - low[j]
  71. if price > dnDev
  72. dnDev := price
  73. price := 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. plotLinReg(lengthInput, useUpperDevInput, upperMultInput, useLowerDevInput, lowerMultInput, colorUpper, colorLower) =>
  87. [s, a, i] = calcSlope(sourceInput, lengthInput)
  88. startPrice = i + s * (lengthInput - 1)
  89. endPrice = i
  90. var line baseLine = na
  91. if na(baseLine) and not na(startPrice)
  92. baseLine := line.new(bar_index - lengthInput + 1, startPrice, bar_index, endPrice, width=1, extend=extendStyle, color=color.new(colorLower, 0))
  93. else
  94. line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice)
  95. line.set_xy2(baseLine, bar_index, endPrice)
  96. na
  97.  
  98. [stdDev, pearsonR, upDev, dnDev] = calcDev(sourceInput, lengthInput, s, a, i)
  99. upperStartPrice = startPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
  100. upperEndPrice = endPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev)
  101. var line upper = na
  102. lowerStartPrice = startPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
  103. lowerEndPrice = endPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev)
  104. var line lower = na
  105. if na(upper) and not na(upperStartPrice)
  106. upper := line.new(bar_index - lengthInput + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0))
  107. else
  108. line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice)
  109. line.set_xy2(upper, bar_index, upperEndPrice)
  110. na
  111. if na(lower) and not na(lowerStartPrice)
  112. lower := line.new(bar_index - lengthInput + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0))
  113. else
  114. line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice)
  115. line.set_xy2(lower, bar_index, lowerEndPrice)
  116. na
  117. linefill.new(upper, baseLine, color = colorUpper)
  118. linefill.new(baseLine, lower, color = colorLower)
  119.  
  120. // Pearson's R
  121. var label r = na
  122. label.delete(r[1])
  123. if showPearsonInput and not na(pearsonR)
  124. r := label.new(bar_index - lengthInput + 1, lowerStartPrice, str.tostring(pearsonR, "#.################"), color = color.new(color.white, 100), textcolor=color.new(colorUpper, 0), size=size.normal, style=label.style_label_up)
  125.  
  126. plotLinReg(lengthInput, useUpperDevInput1, upperMultInput1, useLowerDevInput1, lowerMultInput1, colorUpper1, colorLower1)
  127. plotLinReg(lengthInput, useUpperDevInput2, upperMultInput2, useLowerDevInput2, lowerMultInput2, colorUpper2, colorLower2)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement