Rsi and macd and ma
2025-01-03
Category: None

//@version=5
indicator("Hidden Divergence RSI + MACD + MA Signals", overlay=true)

// Input parameters
rsiLength = input.int(14, title="RSI Length")
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing")
maLength = input.int(50, title="Moving Average Length")

// RSI Calculation
rsi = ta.rsi(close, rsiLength)

// MACD Calculation
  = ta.macd(close, macdFastLength, macdSlowLength, macdSignalSmoothing)

// Moving Average Calculation
ma = ta.sma(close, maLength)

// Hidden Bullish Divergence (RSI)
hiddenBullishDivRSI = ta.lowest(rsi, 5) > ta.lowest(rsi, 14) and ta.lowest(close, 5) < ta.lowest(close, 14)

// Hidden Bearish Divergence (RSI)
hiddenBearishDivRSI = ta.highest(rsi, 5) < ta.highest(rsi, 14) and ta.highest(close, 5) > ta.highest(close, 14)

// Hidden Bullish Divergence (MACD)
hiddenBullishDivMACD = ta.lowest(macdLine, 5) > ta.lowest(macdLine, 14) and ta.lowest(close, 5) < ta.lowest(close, 14)

// Hidden Bearish Divergence (MACD)
hiddenBearishDivMACD = ta.highest(macdLine, 5) < ta.highest(macdLine, 14) and ta.highest(close, 5) > ta.highest(close, 14)

// Buy Signal (Hidden Bullish Divergence + Price above MA)
buySignal = (hiddenBullishDivRSI or hiddenBullishDivMACD) and close > ma

// Sell Signal (Hidden Bearish Divergence + Price below MA)
sellSignal = (hiddenBearishDivRSI or hiddenBearishDivMACD) and close < ma

// Plot Buy and Sell Signals
plotshape(series=buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Plot Moving Average
plot(ma, title="Moving Average", color=color.blue, linewidth=2)