Advertisement
xmd79

Dynamic Linear Regression Oscillator | Adulari

Jan 18th, 2023
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. // Base source is cloned from built-in technicals - "Linear Regression Channel", v26
  2. // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
  3. // ©Adulari
  4. // @version=5
  5.  
  6. indicator("Dynamic Linear Regression Oscillator | Adulari", overlay=false,precision=2)
  7.  
  8. // Inputs
  9. length = input.int(5,minval=1,title='Length',group='General Settings')
  10. smoothing = input.int(10,minval=2,title='Smoothing',group='General Settings')
  11. upperMultiplier = input.float(2.0,step=0.1, title="Upper Multiplier",group='General Settings')
  12. lowerMultiplier = input.float(2.0,step=0.1, title="Lower Multiplier",group='General Settings')
  13. maType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='MA Type',group='General Settings')
  14. smoothingType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='Smoothing Type',group='General Settings')
  15. signals = input.bool(false,title='Signals',tooltip='Show signals when value crosses below oversold line or crosses above overbought line.',group='General Settings')
  16. rescaleBarsBack = input.int(200,minval=20,title='Rescale Bars Back',group='Advanced Settings')
  17.  
  18. // Functions
  19. rescale(float value, float oldMin, float oldMax, float newMin, float newMax) =>
  20. newMin + (newMax - newMin) * (value - oldMin) / math.max(oldMax - oldMin, 10e-10)
  21. ma(float source, simple int length, string maType) =>
  22. switch maType
  23. 'SMA' => ta.sma(source, length)
  24. 'EMA' => ta.ema(source, length)
  25. 'RMA' => ta.rma(source, length)
  26. 'WMA' => ta.wma(source, length)
  27. 'VWMA' => ta.vwma(source, length)
  28. 'HMA' => ta.hma(source, length)
  29. slope(source, length) =>
  30. max_bars_back(source, 5000)
  31. if barstate.isfirst or length <= 1
  32. [float(na), float(na), float(na)]
  33. else
  34. sumX = 0.0
  35. sumY = 0.0
  36. sumXSqr = 0.0
  37. sumXY = 0.0
  38. for i = 0 to length - 1 by 1
  39. val = source[i]
  40. per = i + 1.0
  41. sumX += per
  42. sumY += val
  43. sumXSqr += per * per
  44. sumXY += val * per
  45. slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
  46. average = sumY / length
  47. intercept = average - slope * sumX / length + slope
  48. [slope, average, intercept]
  49. var start_index = 1
  50. lengthInput = bar_index - start_index + 1
  51.  
  52. inp = input(close)
  53. [s, a, i] = slope(inp, lengthInput)
  54. startPrice = i + s * (lengthInput - 1)
  55. endPrice = i
  56. deviation(source, length, slope, average, intercept) =>
  57. if barstate.isfirst or length <= 1
  58. [float(na), float(na), float(na), float(na)]
  59. else
  60. upDev = 0.0
  61. dnDev = 0.0
  62. stdDevAcc = 0.0
  63. dsxx = 0.0
  64. dsyy = 0.0
  65. dsxy = 0.0
  66. periods = length - 1
  67. daY = intercept + slope * periods / 2
  68. val = intercept
  69. for j = 0 to periods by 1
  70. price = high[j] - val
  71. if price > upDev
  72. upDev := price
  73. price := val - low[j]
  74. if price > dnDev
  75. dnDev := price
  76. price := source[j]
  77. dxt = price - average
  78. dyt = val - daY
  79. price -= val
  80. stdDevAcc += price * price
  81. dsxx += dxt * dxt
  82. dsyy += dyt * dyt
  83. dsxy += dxt * dyt
  84. val += slope
  85. stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
  86. pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
  87. [stdDev, pearsonR, upDev, dnDev]
  88. [stdDev, pearsonR, upDev, dnDev] = deviation(inp, lengthInput, s, a, i)
  89. upperStartPrice = startPrice + upperMultiplier * stdDev
  90. upperEndPrice = endPrice + upperMultiplier * stdDev
  91. lowerStartPrice = startPrice - lowerMultiplier * stdDev
  92. lowerEndPrice = endPrice - lowerMultiplier * stdDev
  93. if (inp > upperEndPrice or inp < lowerEndPrice) and (not barstate.islast or barstate.isconfirmed)
  94. start_index := bar_index
  95.  
  96. // Calculations
  97. value = ma(inp>startPrice ? high/startPrice : low/startPrice,length,maType)
  98. value := rescale(value,ta.lowest(value,rescaleBarsBack),ta.highest(value,rescaleBarsBack),0,100)
  99. signal = ma(value,smoothing,smoothingType)
  100.  
  101. // Colors
  102. beColor = #675F76
  103. buColor = #a472ff
  104.  
  105. // Plots
  106. pValue = plot(value,color=buColor,title='Value',linewidth=1)
  107. pSignal = plot(signal,color=beColor,title='Signal',linewidth=1)
  108. fill(pValue,pSignal,color=value>signal ? color.new(buColor,95) : color.new(beColor,95),title='Trend Fill')
  109. hline(80,title='Upper Line',color=beColor)
  110. hline(50,title='Middle Line',color=color.new(color.gray,50))
  111. hline(20,title='Lower Line',color=buColor)
  112. plotshape(signals ? ta.crossunder(value,20) ? 20 : na : na,'Bullish Signal',style=shape.circle,location=location.absolute,size=size.tiny,color=buColor)
  113. plotshape(signals ? ta.crossover(value,80) ? 80 : na : na,'Bearish Signal',style=shape.xcross,location=location.absolute,size=size.tiny,color=beColor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement