//@version=5
indicator("Adaptive EMA Indicator", overlay=true)
// Input settings
length = input.int(50, minval=1, title="Smoothing Length")
baseSensitivity = input.float(1.5, minval=0.1, title="Base Sensitivity")
trendStrengthFilter = input.float(0.5, minval=0.0, title="Trend Strength Filter
(Slope Threshold)")
volatilityFactor = input.float(1.0, minval=0.1, title="Volatility Factor (Higher =
More Adaptive)")
uptrendColor = input.color(color.blue, title="Uptrend Line Color")
downtrendColor = input.color(color.red, title="Downtrend Line Color")
labelTextColor = input.color(color.white, title="Label Text Color")
// Adaptive length calculation
adaptiveSensitivity = baseSensitivity * (1 + ta.stdev(close, length) /
volatilityFactor)
adaptiveLength = math.max(1, math.round(length / adaptiveSensitivity[1]))
// Custom EMA function
customEMA(source, length) =>
alpha = 2 / (length + 1)
var float emaValue = na
emaValue := na(emaValue[1]) ? source : alpha * source + (1 - alpha) *
nz(emaValue[1])
emaFast = customEMA(close, adaptiveLength)
emaSlow = ta.ema(close, length)
// Trend detection
isUptrend = emaFast > emaSlow
isDowntrend = emaFast < emaSlow
// Assign colors based on the trend
trendLineColor = isUptrend ? uptrendColor : downtrendColor
// Plot EMA lines
plot(emaFast, color=trendLineColor, linewidth=2, title="Fast EMA")
plot(emaSlow, color=color.gray, linewidth=1, title="Slow EMA")
// Signal logic
var label lastBuyLabel = na
var label lastSellLabel = na
if ta.crossover(emaFast, emaSlow)
label.new(bar_index, low, "Buy", style=label.style_label_up,
color=color.new(uptrendColor, 0), textcolor=labelTextColor)
if ta.crossunder(emaFast, emaSlow)
label.new(bar_index, high, "Sell", style=label.style_label_down,
color=color.new(downtrendColor, 0), textcolor=labelTextColor)