//@version=6
indicator(title="Merged EMA Indicator with Buy/Sell Signals", shorttitle="EMA (High & Low with Signals)", overlay=true, timeframe="", timeframe_gaps=true)
// Input parameters for first EMA (High source)
len1 = input.int(44, minval=1, title="Length for High EMA")
src1 = input(high, title="Source for High EMA")
offset1 = input.int(title="Offset for High EMA", defval=0, minval=-500, maxval=500, display=display.data_window)
emaHigh = ta.ema(src1, len1)
plot(emaHigh, title="High EMA", color=color.green, offset=offset1)
// Input parameters for second EMA (Low source)
len2 = input.int(44, minval=1, title="Length for Low EMA")
src2 = input(low, title="Source for Low EMA")
offset2 = input.int(title="Offset for Low EMA", defval=0, minval=-500, maxval=500, display=display.data_window)
emaLow = ta.ema(src2, len2)
plot(emaLow, title="Low EMA", color=color.red, offset=offset2)
// Smoothing MA inputs
GRP = "Smoothing"
TT_BB = "Only applies when 'SMA + Bollinger Bands' is selected. Determines the distance between the SMA and the bands."
maTypeInput = input.string("None", "Type", options=["None", "SMA", "SMA + Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=GRP, display=display.data_window)
maLengthInput = input.int(14, "Length", group=GRP, display=display.data_window)
bbMultInput = input.float(2.0, "BB StdDev", minval=0.001, maxval=50, step=0.5, tooltip=TT_BB, group=GRP, display=display.data_window)
var enableMA = maTypeInput != "None"
var isBB = maTypeInput == "SMA + Bollinger Bands"
// Smoothing MA Calculation
ma(source, length, MAtype) =>
switch MAtype
"SMA" => ta.sma(source, length)
"SMA + Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
// Smoothing MA plots for High EMA
smoothingHighMA = enableMA ? ma(emaHigh, maLengthInput, maTypeInput) : na
smoothingHighStDev = isBB ? ta.stdev(emaHigh, maLengthInput) * bbMultInput : na
plot(smoothingHighMA, "High EMA-based MA", color=color.yellow, display=enableMA ? display.all : display.none, editable=enableMA)
plot(smoothingHighMA + smoothingHighStDev, title="High Upper Bollinger Band", color=color.green, display=isBB ? display.all : display.none, editable=isBB)
plot(smoothingHighMA - smoothingHighStDev, title="High Lower Bollinger Band", color=color.green, display=isBB ? display.all : display.none, editable=isBB)
// Smoothing MA plots for Low EMA
smoothingLowMA = enableMA ? ma(emaLow, maLengthInput, maTypeInput) : na
smoothingLowStDev = isBB ? ta.stdev(emaLow, maLengthInput) * bbMultInput : na
plot(smoothingLowMA, "Low EMA-based MA", color=color.purple, display=enableMA ? display.all : display.none, editable=enableMA)
plot(smoothingLowMA + smoothingLowStDev, title="Low Upper Bollinger Band", color=color.orange, display=isBB ? display.all : display.none, editable=isBB)
plot(smoothingLowMA - smoothingLowStDev, title="Low Lower Bollinger Band", color=color.orange, display=isBB ? display.all : display.none, editable=isBB)
// Buy/Sell Signal Logic
buySignal = ta.crossover(emaHigh, emaLow)
sellSignal = ta.crossunder(emaHigh, emaLow)
// Plot Buy/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")