Buy/Sell EMA Candle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AleSaira


//@version=5        
indicator(title="Buy/Sell EMA Candle", shorttitle="Buy/SellEMA211", overlay=true)


ema1 = ta.ema(close, length=13)
ema2 = ta.ema(close, length=21)
ema3 = ta.ema(close, length=55)
ema4 = ta.ema(close, length=90)
ema5 = ta.ema(close, length=200)

//out = 3 * (ema1 - ema2) + ema3 nooo
plot(ema3, "EMA 55", color=#ff9e03)
plot(ema4, "EMA 90", color=#f910ed)
plot(ema5, "EMA 200", color=#2b00ff)

// Calculate the body of the candle
bodySize = close > open ? close - open : open - close

// Calculate the body of the previous and next candles using a loop
bodySizeSurrounding = 0.0
for i = 1 to 4
    bodySizeSurrounding := bodySizeSurrounding + (close[i] > open[i] ? close[i] - open[i] : open[i] - close[i])

// Check if the current candle encompasses the previous and next four candles
engulfing = (close > open[3] and open < close[3]) and (bodySize > bodySizeSurrounding)

// Highlight the area of the engulfing candle
engulfingB = (close < open[4] and open > close[4]) and (bodySize > bodySizeSurrounding)

//plotshape(engulfing, style=shape.triangleup, location=location.belowbar, color=color.rgb(1, 1, 1), size=size.small, text = "Sell")
plotshape(engulfingB, style=shape.triangledown, location=location.abovebar, color=color.rgb(4, 4, 4), size=size.small, text = "Sell")



engulfingC = (close > open[2] and open < close[2]) and (bodySize > bodySizeSurrounding)

engulfingD = (close > open[1] and open < close[1]) and (bodySize > bodySizeSurrounding)


// Highlight the area of the engulfing candle bearish
//bgcolor(engulfingC ? color.rgb(77, 77, 77) : na, title="Engulfing Candles", transp=100)

//bgcolor(engulfingD ? color.rgb(77, 77, 77) : na, title="Engulfing Candles", transp=100)

// Highlight the zone where ema1 crosses ema2
//bgcolor((ema1 > ema2) and (ema1[1] < ema2[1]) ? color.green : na, title="EMA1 crosses above EMA2", transp=80)
//bgcolor((ema1 < ema2) and (ema1[1] > ema2[1]) ? color.red : na, title="EMA1 crosses below EMA2", transp=80)

// Place markers at engulfing candle bar indices
plotshape(engulfingC, style=shape.triangleup, location=location.belowbar, color=color.rgb(1, 1, 1), size=size.small, text = "Buy")
plotshape(engulfingD, style=shape.triangledown, location=location.abovebar, color=color.rgb(4, 4, 4), size=size.small, text = "Sell")


plot(ema1, "EMA 13", color=#f3f4f7, linewidth=2)
plot(ema2, "EMA 21", color=#00aa06, linewidth=2)

emaCrossUp = (ema1 > ema2) and (ema1[1] < ema2[1])
emaCrossDown = (ema1 < ema2) and (ema1[1] > ema2[1])

// plot function to set a circle that highlights the intersections between ema1 and ema2
plot(emaCrossUp ? ema1 : na, style=plot.style_circles, color=color.green, linewidth=14, transp=50)
plot(emaCrossDown ? ema1 : na, style=plot.style_circles, color=color.red, linewidth=14, transp=50)
plotshape(emaCrossDown ? ema1 : false, title = "short", text = 'short', style = shape.labelup, location = location.abovebar, color= color.rgb(19, 19, 19),textcolor = color.white, transp = 0, size = size.tiny)
plotshape(emaCrossUp ? ema1 : false, title = "long", text = 'long', style = shape.labelup, location = location.belowbar, color= color.rgb(0, 0, 0),textcolor = color.white, transp = 0, size = size.tiny)


// Determine the trend direction for the last candle
lastCandleL = close > ta.ema(open,21) 
lastCandleS = close < ta.ema(open,21)
last2L = close > open 
last2S = close < open
currentCandleDirection = close[1] > open[1] ? "L" : (close[1] < open[1] ? "S" : "N/A")
// Calculate RSI
// Calculate RSI
rsiLength = input(14, title="RSI Length")
rsiThreshold = input(70, title="RSI Threshold (%)")
rsiValue = ta.rsi(close, rsiLength)

// Determine RSI trend direction
rsiTrendDirection = rsiValue > rsiThreshold ? "OB" : (rsiValue < (100 - rsiThreshold) ? "OS" : "NT")

last3L = close < ta.rsi(close,20)
last3S = close > ta.rsi(close,80)
tabfancy = input.string(defval="bottomR", title = "Position", options = ["bottomR","bottomC","bottomL","topR","topC","topL"])

// Define trend labels for each time frame
monthlyTrend = lastCandleL ? "L" : (lastCandleS ? "S" : "N/A")
weeklyTrend = request.security(syminfo.tickerid, "W", lastCandleL ? "L" : (lastCandleS ? "S" : "N/A"))
dailyTrend = request.security(syminfo.tickerid, "D", lastCandleL ? "L" : (lastCandleS ? "S" : "N/A"))
fourHourTrend = request.security(syminfo.tickerid, "240", lastCandleL ? "L" : (lastCandleS ? "S" : "N/A"))
oneHourTrend = request.security(syminfo.tickerid, "60", lastCandleL ? "L" : (lastCandleS ? "S" : "N/A"))


monthlyTrend1 = currentCandleDirection
weeklyTrend1 = request.security(syminfo.tickerid, "W", currentCandleDirection)
dailyTrend1 = request.security(syminfo.tickerid, "D", currentCandleDirection)
fourHourTrend1 = request.security(syminfo.tickerid, "240", currentCandleDirection)
oneHourTrend1 = request.security(syminfo.tickerid, "60", currentCandleDirection)

monthlyTrend2 = rsiTrendDirection
weeklyTrend2 = request.security(syminfo.tickerid, "W", rsiTrendDirection)
dailyTrend2 = request.security(syminfo.tickerid, "D", rsiTrendDirection)
fourHourTrend2 = request.security(syminfo.tickerid, "240", rsiTrendDirection)
oneHourTrend2 = request.security(syminfo.tickerid, "60", rsiTrendDirection)

// Display the table

tblpos(add) => 
    switch add
        "bottomR"=> position.bottom_right
        "bottomC" => position.bottom_center
        "bottomL" => position.bottom_left
        "topR" => position.top_right
        "topC" => position.top_center
        "topL" => position.top_left
        =>position.middle_center

tableData = table.new(position = tblpos(tabfancy), columns = 4, rows = 7, bgcolor = color.white)
table.cell(tableData, 0, 0, "", bgcolor=color.yellow)
table.cell(tableData, 1, 0, "T", bgcolor=color.yellow)

table.cell(tableData, 2, 0, "C", bgcolor=color.yellow)
table.cell(tableData, 3, 0, "R", bgcolor=color.yellow)

table.cell(tableData, 0, 1, "M", bgcolor=color.blue, text_color = color.white)
table.cell(tableData, 0, 2, "W", bgcolor=color.blue,text_color = color.white)
table.cell(tableData, 0, 3, "D", bgcolor=color.blue, text_color = color.white)
table.cell(tableData, 0, 4, "4h", bgcolor=color.blue, text_color = color.white)
table.cell(tableData, 0, 5, "1h", bgcolor=color.blue, text_color = color.white)

table.cell(tableData, 1, 1, monthlyTrend)
table.cell(tableData, 1, 2, weeklyTrend)
table.cell(tableData, 1, 3, dailyTrend)
table.cell(tableData, 1, 4, fourHourTrend)
table.cell(tableData, 1, 5, oneHourTrend)

table.cell(tableData, 2, 1, monthlyTrend1)
table.cell(tableData, 2, 2, weeklyTrend1)
table.cell(tableData, 2, 3, dailyTrend1)
table.cell(tableData, 2, 4, fourHourTrend1)
table.cell(tableData, 2, 5, oneHourTrend1)

table.cell(tableData, 3, 1, monthlyTrend2)
table.cell(tableData, 3, 2, weeklyTrend2)
table.cell(tableData, 3, 3, dailyTrend2)
table.cell(tableData, 3, 4, fourHourTrend2)
table.cell(tableData, 3, 5, oneHourTrend2)





hvbBullColor = input.color(defval=color.green, title='Bullish HVB Color', inline='Set Custom Color', group='High Volume Bar')
hvbBearColor = input.color(defval=color.red, title='Bearish HVB Color', inline='Set Custom Color', group='High Volume Bar')
hvbEMAPeriod = input.int(defval=50, minval=1, title='Volume EMA Period', group='High Volume Bar')
hvbMultiplier = input.float(defval=1.5, title='Volume Multiplier', minval=1, maxval=100, group='High Volume Bar')

//Functions
isUp(index) =>
    close[index] > open[index]

isDown(index) =>
    close[index] < open[index]

volEma = ta.ema(volume, hvbEMAPeriod)
isHighVolume = volume > (hvbMultiplier * volEma)
barcolor(isUp(0) and isHighVolume ? hvbBullColor : na, title="Bullish HVB")
barcolor(isDown(0) and isHighVolume ? hvbBearColor : na, title="Bearish HVB")


attention = input.float(0.15, minval=0.010, title='Doji')
release = input(false, title='Above') 
barcolor(math.abs(open - close) <= (high - low) * attention ? color.rgb(58, 214, 249) : na)
doji = math.abs(open - close) <= (high - low) * attention ? 1 : 0
plotshape(release ? doji :na, style=shape.labeldown, color=color.new(#ffffff, 0), location=location.top, size=size.huge, title='Doji')
plot(release ? doji :na, style=plot.style_stepline, color=color.rgb(0, 206, 196), linewidth=2, transp=20)
plotshape(doji, title = "Doji", text = '', style = shape.cross, location = location.belowbar, color= color.rgb(249, 249, 249),textcolor = color.white, transp = 90, size = size.tiny)



// parameters definition
rsiLength1 = input(14, "Periodi RSI")
overboughtThreshold1 = input(80, "Soglia overbought")
oversoldThreshold1 = input(20, "Soglia oversold")

// Calculate ofRSI
rsiValue1 = ta.rsi(open, rsiLength1)

// candle color calculation based on RSI
candleColor1 = rsiValue1 >= overboughtThreshold1 or rsiValue1 <= oversoldThreshold1 ? color.yellow : na


barcolor(candleColor1)

