//@version=5 indicator("Trend Identification Indicator", overlay=true, shorttitle="Trend ID Indicator [birdsongs]") // User inputs to allow customization of moving averages lengthSMA = input(50, title="Simple Moving Average Length") lengthEMA = input(21, title="Exponential Moving Average Length") src = input(close, title="Source") // Calculate moving averages sma = ta.sma(src, lengthSMA) // Simple Moving Average ema = ta.ema(src, lengthEMA) // Exponential Moving Average // Plot moving averages on the chart plot(sma, color=color.blue, title="Simple Moving Average") plot(ema, color=color.red, title="Exponential Moving Average") // Generate buy and sell signals when moving averages cross buySignal = ta.crossover(ema, sma) // EMA crosses above SMA sellSignal = ta.crossunder(ema, sma) // EMA crosses below SMA // State variable to track if the background should be colored var inLongPosition = false // Initialize as false, meaning no position is open // Update state based on signals if (buySignal) inLongPosition := true // Set to true on a buy signal if (sellSignal) inLongPosition := false // Set to false on a sell signal // Plot buy and sell signals on the chart plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal", text="Buy") plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal", text="Close") // Color the background green when in a long position bgcolor(inLongPosition ? color.new(color.green, 90) : na) // Alert conditions alertcondition(buySignal, title="Buy Alert", message="EMA has crossed above SMA, consider a buy.") alertcondition(sellSignal, title="Close Alert", message="EMA has crossed below SMA, consider closing the buy.")