xmd79

Moving Average reversals combo osc

May 26th, 2023
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LeafAlgo
  3.  
  4. //@version=5
  5. indicator("Moving Average Contrarian Indicator", overlay=false)
  6.  
  7. length = input(40, "Moving Average Length")
  8. src = input(close)
  9.  
  10. // Calculate moving average
  11. ma = ta.sma(src, length)
  12.  
  13. // Calculate distance from price to moving average
  14. distance = src - ma
  15.  
  16. // Calculate normalized MACI
  17. distance_min = ta.lowest(distance, length)
  18. distance_max = ta.highest(distance, length)
  19. maci = ((distance - distance_min) / (distance_max - distance_min)) * 100
  20.  
  21. // Determine barcolor and background color conditions
  22. maci_sma = ta.sma(maci, length)
  23. barC = maci > maci_sma and maci > 30 ? color.lime : maci < maci_sma and maci < 70 ? color.fuchsia : color.yellow
  24. backC = maci > maci_sma and maci > 30 ? color.new(color.lime, 80) : maci < maci_sma and maci < 70 ? color.new(color.fuchsia, 80) : color.new(color.yellow, 80)
  25.  
  26. // Color
  27. barcolor(barC)
  28. bgcolor(backC, transp=70)
  29.  
  30. // Plotting
  31. plot(maci, title="MACI", color=barC, style=plot.style_histogram, linewidth=4)
  32. plot(maci_sma, title='MACI SMA', color=color.maroon, linewidth=2)
  33.  
  34. // Overbought and oversold levels
  35. hline(70, "Overbought", color=color.fuchsia)
  36. hline(30, "Oversold", color=color.lime)
  37.  
  38. //end of this part
  39.  
  40. //@version=5
  41. //indicator("Moving Average Reversals", shorttitle = 'Moving Average Reversals', precision = 2)
  42.  
  43. //inputs
  44. var g1 = 'Moving Average'
  45. imatype = input.string(defval = 'EMA', title = 'MA Type', options = ['EMA', 'SMA', 'HMA', 'WMA', 'VWMA'], inline = '1', group = g1)
  46. imalength = input.int(21, 'MA Length', minval = 1, maxval = 300, step = 1, inline = '1', group = g1)
  47. isrc = input.source(defval = close, title ='Source To Meassure Distance From ', inline = '2', group = g1, tooltip = 'Select the source to calculate it\'s distance from the moving average')
  48. extColor = input.color(color.rgb(255,0,0), 'Extended Color', inline = '3', group = g1)
  49. inColor = input.color(color.rgb(0,255,0), 'In Range Color', inline = '3', group = g1)
  50.  
  51. var g2 = 'Extensions'
  52. showExt = input.bool(true, 'Highlight Highest Extensions Up and Down', group = g2)
  53. extDown = input.color(color.new(color.aqua,70), 'Highest Extension Down', group = g2)
  54. extUp = input.color(color.new(color.maroon,70), 'Highest Extension Up', group = g2)
  55.  
  56. var g3 = 'Moving Average of Extensions'
  57. showMa2 = input.bool(true, 'Show Moving Average of Extensions', inline = '1', group = g3)
  58. ma2Len = input.int(10, 'Length', inline = '2', group = g3)
  59. ma2Color = input.color(color.orange, 'Color', inline = '2', group = g3)
  60.  
  61. // get the ma
  62. maz = switch imatype
  63. 'EMA' => ta.ema(close, imalength)
  64. 'SMA' => ta.sma(close, imalength)
  65. 'HMA' => ta.hma(close, imalength)
  66. 'WMA' => ta.wma(close, imalength)
  67. 'VWMA' => ta.vwma(close, imalength)
  68.  
  69. // calculate the distance from the source to the moving average
  70. distancez = ((isrc / maz) - 1) * 100
  71. extensionMa = ta.sma(distancez, ma2Len)
  72.  
  73. //find the highest and lowest extensions from the selected MA over the past year
  74. lowest = ta.lowest(distancez,252)
  75. highest = ta.highest(distancez,252)
  76.  
  77. //create the gradient color
  78. gradient = distancez > 0 ? color.from_gradient(distancez, 0, highest, inColor, extColor) : color.from_gradient(distancez, lowest, 0, extColor, inColor)
  79.  
  80. //plots
  81. plot(distancez, "Distance From Ma", style= plot.style_columns, linewidth = 4, color = gradient)
  82. plot(showMa2 ? extensionMa : na, 'Extension MA', color = ma2Color)
  83. bgcolor(showExt and distancez == highest ? extUp : showExt and distancez == lowest ? extDown : na)
Advertisement
Add Comment
Please, Sign In to add comment