EMA Envelope - Signal with Stoploss and Takeprofit Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hellomuthu23

//@version=5
indicator("EMA Envelope Indicator with Buy/Sell Signal with Stoploss and Takeprofit Levels","EMA Envelope Signal",true)

// Input Parameters
EMA_Length=input(title="EMA Length", defval=20) // Short-term Exponential Moving Average length
EMA_LongTerm_Length = input(title =  "Long Term EMA length",defval = 200) // Long-term Exponential Moving Average length
TakeProfit_Ratio = input.float(title="Take Profit Ratio", defval = 2.0, minval = 0.1) // Take Profit ratio for calculating take-profit levels
Filter_Signal_On_LongTerm_EMA = input(title="Filter Signal on Long Term EMA", defval =  true) // Option to filter signals using long-term EMA
Show_Only_Recent_Signals = input(title = "Show only recent signal", defval = false) // Option to show only recent buy/sell signals

// Data Setup
src = close // Source data for calculations
e = ta.ema(close, EMA_Length) // Short-term Exponential Moving Average
eu = ta.ema(high, EMA_Length) // Upper EMA (based on high prices)
el = ta.ema(low, EMA_Length) // Lower EMA (based on low prices)

LongTermEMA=ta.ema(close, EMA_LongTerm_Length) // Long-term Exponential Moving Average

// Plotting EMAs
plot(e, style=plot.style_circles, color=color.gray)
plot1 = plot(eu, title="Upper EMA", color=color.lime, linewidth=2 )
plot2 = plot(el, title="Lower EMA", color=color.aqua, linewidth=2 )
fill(plot1=plot1, plot2=plot2, color=color.gray, transp=50)

plot(LongTermEMA, title = "LongTerm EMA", color=color.new(color.blue, 30), linewidth=3)

// Buy and Sell Signal Conditions
longCond = low[0] > eu and low[1] < eu[1] 
if Filter_Signal_On_LongTerm_EMA
    longCond := longCond and close > LongTermEMA

shortCond =  high[0] < el and high[1] > el[1]
if Filter_Signal_On_LongTerm_EMA
    shortCond := shortCond and close < LongTermEMA

// Plot Buy and Sell Signals
plotshape(series=longCond, title="Buy signal", show_last = Show_Only_Recent_Signals? 100: 10000, style=shape.triangleup, location=location.belowbar, color=color.green, text="Buy", textcolor = color.lime, size=size.tiny)
plotshape(series=shortCond, title="Sell signal",show_last =  Show_Only_Recent_Signals? 100: 10000, style=shape.triangledown, location=location.abovebar, color=color.red, text="Sell", textcolor = color.red, size=size.tiny)

// Calculate Stop-Loss and Take-Profit Levels
longStopLoss = el // Long position stop-loss at the lower EMA
longTakProfit = ((low - longStopLoss) * TakeProfit_Ratio) + low // Long position take-profit calculation

if longCond == false // If not in a long position, set stop-loss and take-profit to 'na' (not available)
    longStopLoss := na
    longTakProfit := na

shortStopLoss = eu // Short position stop-loss at the upper EMA
shortTakProfit = high - (( shortStopLoss - high) * TakeProfit_Ratio) // Short position take-profit calculation

if shortCond == false // If not in a short position, set stop-loss and take-profit to 'na'
    shortStopLoss := na
    shortTakProfit := na

// Plot Stop-Loss and Take-Profit Levels
plot(longTakProfit , "Long TP", color.green, 3, plot.style_linebr,  show_last =  Show_Only_Recent_Signals? 100: 10000)
plot(longStopLoss , "Long SL", color.purple, 5, plot.style_linebr, show_last =  Show_Only_Recent_Signals? 100: 10000)

plot(shortTakProfit , "Short TP", color.green, 3, plot.style_linebr,show_last =  Show_Only_Recent_Signals? 100: 10000 )
plot(shortStopLoss , "Short SL", color.purple, 5, plot.style_linebr, show_last =  Show_Only_Recent_Signals? 100: 10000)
