Custom EMA from X Days Ago
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © firos_meethal

//@version=5
indicator("Custom EMA from X Days Ago", overlay = true)

// Input options
daysAgo = input.int(4, title = "Days Ago", minval = 1)
emaPeriod = input.int(10, title = "EMA Period")

// Calculate the selected EMA
ema = ta.ema(close, emaPeriod)

// Get the value of the selected EMA from X days ago on the current timeframe
emaXDaysAgo = request.security(syminfo.tickerid, timeframe.period, ema[daysAgo])

// Plot the selected EMA value from X days ago on today's candle
plot(emaXDaysAgo, color = color.blue, title = "Custom EMA", style = plot.style_stepline, linewidth = 2)

var bool sellSignal = na

// Check if candle opens above and closes below the indicator, and plot a "SELL" label
if open > emaXDaysAgo and close < emaXDaysAgo
    label.new(x=bar_index, y=low, text="Sell", style=label.style_label_up, color=color.red, textcolor=color.black)
    sellSignal := true

