Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © LeafAlgo
- //@version=5
- indicator("Moving Average Contrarian Indicator", overlay=false)
- length = input(40, "Moving Average Length")
- src = input(close)
- // Calculate moving average
- ma = ta.sma(src, length)
- // Calculate distance from price to moving average
- distance = src - ma
- // Calculate normalized MACI
- distance_min = ta.lowest(distance, length)
- distance_max = ta.highest(distance, length)
- maci = ((distance - distance_min) / (distance_max - distance_min)) * 100
- // Determine barcolor and background color conditions
- maci_sma = ta.sma(maci, length)
- barC = maci > maci_sma and maci > 30 ? color.lime : maci < maci_sma and maci < 70 ? color.fuchsia : color.yellow
- 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)
- // Color
- barcolor(barC)
- bgcolor(backC, transp=70)
- // Plotting
- plot(maci, title="MACI", color=barC, style=plot.style_histogram, linewidth=4)
- plot(maci_sma, title='MACI SMA', color=color.maroon, linewidth=2)
- // Overbought and oversold levels
- hline(70, "Overbought", color=color.fuchsia)
- hline(30, "Oversold", color=color.lime)
- //end of this part
- //@version=5
- //indicator("Moving Average Reversals", shorttitle = 'Moving Average Reversals', precision = 2)
- //inputs
- var g1 = 'Moving Average'
- imatype = input.string(defval = 'EMA', title = 'MA Type', options = ['EMA', 'SMA', 'HMA', 'WMA', 'VWMA'], inline = '1', group = g1)
- imalength = input.int(21, 'MA Length', minval = 1, maxval = 300, step = 1, inline = '1', group = g1)
- 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')
- extColor = input.color(color.rgb(255,0,0), 'Extended Color', inline = '3', group = g1)
- inColor = input.color(color.rgb(0,255,0), 'In Range Color', inline = '3', group = g1)
- var g2 = 'Extensions'
- showExt = input.bool(true, 'Highlight Highest Extensions Up and Down', group = g2)
- extDown = input.color(color.new(color.aqua,70), 'Highest Extension Down', group = g2)
- extUp = input.color(color.new(color.maroon,70), 'Highest Extension Up', group = g2)
- var g3 = 'Moving Average of Extensions'
- showMa2 = input.bool(true, 'Show Moving Average of Extensions', inline = '1', group = g3)
- ma2Len = input.int(10, 'Length', inline = '2', group = g3)
- ma2Color = input.color(color.orange, 'Color', inline = '2', group = g3)
- // get the ma
- maz = switch imatype
- 'EMA' => ta.ema(close, imalength)
- 'SMA' => ta.sma(close, imalength)
- 'HMA' => ta.hma(close, imalength)
- 'WMA' => ta.wma(close, imalength)
- 'VWMA' => ta.vwma(close, imalength)
- // calculate the distance from the source to the moving average
- distancez = ((isrc / maz) - 1) * 100
- extensionMa = ta.sma(distancez, ma2Len)
- //find the highest and lowest extensions from the selected MA over the past year
- lowest = ta.lowest(distancez,252)
- highest = ta.highest(distancez,252)
- //create the gradient color
- gradient = distancez > 0 ? color.from_gradient(distancez, 0, highest, inColor, extColor) : color.from_gradient(distancez, lowest, 0, extColor, inColor)
- //plots
- plot(distancez, "Distance From Ma", style= plot.style_columns, linewidth = 4, color = gradient)
- plot(showMa2 ? extensionMa : na, 'Extension MA', color = ma2Color)
- bgcolor(showExt and distancez == highest ? extUp : showExt and distancez == lowest ? extDown : na)
Advertisement
Add Comment
Please, Sign In to add comment