RicardoSantos

[RS]MACD Divergence V0

EXPERIMENTAL:
MACD Divergence detection.
looks like macd is more prone for missing the extremes in price then the rsi due to lag.
Script de código abierto

Siguiendo el verdadero espíritu de TradingView, el autor de este script lo ha publicado en código abierto, para que los traders puedan entenderlo y verificarlo. ¡Un hurra por el autor! Puede utilizarlo de forma gratuita, aunque si vuelve a utilizar este código en una publicación, debe cumplir con lo establecido en las Normas internas. Puede añadir este script a sus favoritos y usarlo en un gráfico.

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.

¿Quiere utilizar este script en un gráfico?
study(title='[RS]MACD Divergence V0')
src = input(close)
fast = input(12)
slow = input(26)
smooth = input(9)

f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0

fast_ma = sma(src, fast)
slow_ma = sma(src, slow)
macd = fast_ma-slow_ma
signal = ema(macd, smooth)
hist = macd - signal

fractal_top = f_fractalize(hist) > 0 ? hist[2] : na
fractal_bot = f_fractalize(hist) < 0 ? hist[2] : na

high_prev = valuewhen(fractal_top, hist[2], 1) 
high_price = valuewhen(fractal_top, high[2], 1)
low_prev = valuewhen(fractal_bot, hist[2], 1) 
low_price = valuewhen(fractal_bot, low[2], 1)

regular_bearish_div = fractal_top and high[2] > high_price and hist[2] < high_prev
hidden_bearish_div = fractal_top and high[2] < high_price and hist[2] > high_prev
regular_bullish_div = fractal_bot and low[2] < low_price and hist[2] > low_prev
hidden_bullish_div = fractal_bot and low[2] > low_price and hist[2] < low_prev

plot(title='MACD', series=macd, color=gray)
plot(title='SIGNAL', series=signal, color=silver)
plot(title='HIST', series=hist, color=black)
plot(title='H F', series=fractal_top, color=black, offset=-2)
plot(title='L F', series=fractal_bot, color=black, offset=-2)
plot(title='H D', series=fractal_top, style=circles, color=regular_bearish_div or hidden_bearish_div ? maroon : gray, linewidth=3, offset=-2)
plot(title='L D', series=fractal_bot, style=circles, color=regular_bullish_div or hidden_bullish_div ? green : gray, linewidth=3, offset=-2)

plotshape(title='+RBD', series=regular_bearish_div ? hist[2] : na, text='Regular', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
plotshape(title='+HBD', series=hidden_bearish_div ? hist[2] : na, text='hidden', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
plotshape(title='-RBD', series=regular_bullish_div ? hist[2] : na, text='Regular', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
plotshape(title='-HBD', series=hidden_bullish_div ? hist[2] : na, text='hidden', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)