'Create MACD band on Tradingview

My idea is to make the MACD histogram oscillating in a range from 0-100 just like the RSI.

I did it successfully, but compared to normal MACD histogram it is too low and hard to see because most values just fluctuate slightly above or below 50.

Comparison image of MACD Band (MACD.B - above) and classic MACD (below). enter image description here

I want the MACD line and histogram to fluctuate in a larger range (from 0 to 100), please tell me how. Thanks very much.

Below is the current code.

//@version=5
//NhanTam
indicator(title='MACD in BANDS', shorttitle='MACD.B', timeframe='')

// Getting inputs
fast_len = input(title='Fast Length', defval=12, inline='1')
slow_len = input(title='Slow Length', defval=26, inline='1')

signal_length = input.int(title='Signal Len:', minval=1, maxval=200, defval=9, inline='3')
src = input(title='Input source', defval=close, inline='3')
ma_type = input.string(title='Type MA : :', defval='E-MA', options=['SMA', 'E-MA'], inline='4')
signal_type = input.string(title='Signal : : : : :', defval='E-MA', options=['SMA', 'E-MA'], inline='4')

// MACD Plot colors
height = input.int(50, 'Col height', options=[50,60,70,80,90], group='Histogram', tooltip='Column height, default value is 50')
colmacd = input.color(#2962FF, 'MACD Line:', group='Histogram', inline='ma')
colsign = input.color(#FF6D00, 'Sig:', group='Histogram', inline='ma', tooltip='MACD Line and Signal Line')

col1 = input.color(#26A69A, 'Above Grow', group='Histogram', inline='ab')
col2 = input.color(#B2DFDB, 'Fall', group='Histogram', inline='ab')
col3 = input.color(#FFCDD2, 'Below Grow', group='Histogram', inline='be')
col4 = input.color(#FF5252, 'Fall', group='Histogram', inline='be')

// MACD Calculating
fast_ma = ma_type == 'SMA' ? ta.sma(src, fast_len) : ta.ema(src, fast_len)
slow_ma = ma_type == 'SMA' ? ta.sma(src, slow_len) : ta.ema(src, slow_len)

macd = (fast_ma*height)/slow_ma
sign = signal_type == 'SMA' ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = (macd*height)/sign

// Plot
plot(hist, title='Histogram', style=plot.style_columns, histbase=50, color=hist >= height ? hist[1] < hist ? col1 : col2 : hist[1] < hist ? col3 : col4)
plot(macd, title='MACD', color=colmacd)
plot(sign, title='Signal', color=colsign)

band1 = hline(70, 'Upper Band', color=#787B86)
bandm = hline(50, 'Middle Band', color=color.new(#787B86, 50))
band0 = hline(30, 'Lower Band', color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title='Background')


Solution 1:[1]

I finished my code.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NhanTam
// @version=5

indicator(title='MACD BAND', shorttitle='MACD Band', precision=3, timeframe='', timeframe_gaps=true)

// Getting inputs
fast_length = input.int(12, 'Fast Length', minval=6, maxval=19, tooltip='Default 12')
slow_length = input.int(26, 'Slow Length', minval=19, maxval=39, tooltip='Default 26')
src = input.source(close, 'Source')
signal_length = input.int(9, 'Signal Smoothing', minval=1, maxval=50)
ma_source = input.string('EMA', 'Oscillator MA Type', options=['SMA', 'EMA', 'WMA', 'RMA', 'HMA', 'ALMA', 'VWMA'], tooltip='Normally uses EMA')
ma_signal = input.string('EMA', 'Signal Line MA Type', options=['SMA', 'EMA', 'WMA', 'RMA', 'HMA', 'ALMA', 'VWMA'], tooltip='Normally uses EMA')

// Input color
col_macd = input.color(#2962FF, 'MACD Line', group='Histogram', inline='1')
showMACD = input.bool(true, 'Show', group='Histogram', inline='1')
col_sign = input.color(#FF6D00, 'Signal Line:', group='Histogram', inline='2')
showMACDSignal = input.bool(true, 'Show', group='Histogram', inline='2')
col_grow_above = input.color(#26A69A, 'Above grow', group='Histogram', inline='3')
col_fall_above = input.color(#B2DFDB, 'Fall', group='Histogram', inline='3')
col_grow_below = input.color(#FFCDD2, 'Below grow', group='Histogram', inline='4')
col_fall_below = input.color(#FF5252, 'Fall', group='Histogram', inline='4')

showMACDHistogram = input.bool(true, 'Show Histogram', group='Histogram')
histo_trans = input.int(30, 'Hist transparent', minval=0, maxval=100, group='Histogram', tooltip='Histogram transparency, Default 30')
lowcolumn = input.int(10, 'Columns Low', minval=2, maxval=20, group='Histogram', tooltip='Default 10. Min = 2, max = 20. The larger the number, the lower the Histogram height.')
lowline = input.int(5, 'Line Low', minval=2, maxval=20, group='Histogram', tooltip='Default 5. Min = 2, max = 20. The larger the number, the lower the height of the MACD line and the Signal line.')

// MACD Calculating
shortMA = ma_source == 'SMA' ? ta.sma(src, fast_length) : ma_source == 'EMA' ? ta.ema(src, fast_length) : ma_source == 'WMA' ? ta.wma(src, fast_length) : ma_source == 'RMA' ? ta.rma(src, fast_length) : ma_source == 'HMA' ? ta.hma(src, fast_length) : ma_source == 'ALMA' ? ta.alma(src, fast_length, 0.85, 6) : ma_source == 'VWMA' ? ta.vwma(src, fast_length) : ta.ema(src, fast_length)

longMA = ma_source == 'SMA' ? ta.sma(src, slow_length) : ma_source == 'EMA' ? ta.ema(src, slow_length) : ma_source == 'WMA' ? ta.wma(src, slow_length) : ma_source == 'RMA' ? ta.rma(src, slow_length) : ma_source == 'HMA' ? ta.hma(src, slow_length) : ma_source == 'VWMA' ? ta.vwma(src, slow_length) : ma_source == 'ALMA' ? ta.alma(src, slow_length, 0.85, 6) : ta.ema(src, slow_length)

macdLine = shortMA - longMA

signalLine = ma_signal == 'SMA' ? ta.sma(macdLine, signal_length) : ma_signal == 'EMA' ? ta.ema(macdLine, signal_length) : ma_signal == 'WMA' ? ta.wma(macdLine, signal_length) : ma_signal == 'RMA' ? ta.rma(macdLine, signal_length) : ma_signal == 'HMA' ? ta.hma(macdLine, signal_length) : ma_source == 'VWMA' ? ta.vwma(macdLine, signal_length) : ma_signal == 'ALMA' ? ta.alma(macdLine, signal_length, 0.85, 6) : ta.ema(macdLine, signal_length)

hist = macdLine - signalLine

// Re-Calculating MACD
midline = 50

sd_hist = ta.stdev(hist, 500)
basis_hist = ta.ema(hist, 500)
up_hist = basis_hist + sd_hist * lowcolumn
dn_hist = basis_hist - sd_hist * lowcolumn
re_hist = (hist - dn_hist) / (up_hist - dn_hist) * 100

sd_sig = ta.stdev(signalLine, 500)
basis_sig = ta.ema(signalLine, 500)
up_sig = basis_sig + sd_sig * lowline
dn_sig = basis_sig - sd_sig * lowline
rescaled_sig = (signalLine - dn_sig) / (up_sig - dn_sig) * 100

sd_mac = ta.stdev(macdLine, 500)
basis_mac = ta.ema(macdLine, 500)
up_mac = basis_mac + sd_mac * lowline
dn_mac = basis_mac - sd_mac * lowline
rescaled_mac = (macdLine - dn_mac) / (up_mac - dn_mac) * 100

// Plot
histLineCol = hist >= 0 ? hist[1] < hist ? color.new(col_grow_above,histo_trans) : color.new(col_fall_above,histo_trans) : hist[1] < hist ? color.new(col_grow_below,histo_trans) : color.new(col_fall_below,histo_trans)

plot(showMACD ? rescaled_mac : na, title='MACD', color=col_macd, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDSignal ? rescaled_sig : na, title='Signal', color=col_sign, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDHistogram ? re_hist : na, title='Histogram', color=histLineCol, style=plot.style_columns, linewidth=1, histbase=midline)

band1 = hline(70, 'Upper Band', color=#787B86)
bandm = hline(50, 'Middle Band', color=color.new(#787B86, 50))
band0 = hline(30, 'Lower Band', color=#787B86)
fill(band1, band0, color=color.new(#7e575e, 95), title='Background')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NqHai