Buy/Sell Box
// 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 Box", shorttitle="Buy/Sell Box", overlay=true)

//================================================================================================}
// Input for the number of horizontal candles in the box
boxWidth            =       input.int       (5, title="Box Width", minval=1)

// Input for the box lines color
boxLineColor        =       input.color     (color.rgb(85, 59, 255), title="Box Line Color")

// Input for the box lines width
boxLine             =       input.int       (2, title="Box Lines", minval=1)

// Input for the doji size 
sizeDoji            =       input.float     (0.15, title="Doji Size", minval=0.05)
//================================================================================================}

// Calculate the size of the doji's body
dojiSize = math.abs(open - close)

// Identify a doji if the body size is very small compared to the entire range
isDoji = dojiSize < (high - low) * sizeDoji
//================================================================================================}

// Calculate rectangle coordinates
rectTop = math.max(high[1], low[1])
rectBottom = math.min(high[1], low[1])
rectLeft = bar_index[1]
rectRight = bar_index + boxWidth
//================================================================================================}

// Draw rectangle lines if it's a doji
if isDoji
    line.new(x1=rectLeft, y1=rectTop, x2=rectRight, y2=rectTop, width=boxLine, color=boxLineColor)
    line.new(x1=rectRight, y1=rectTop, x2=rectRight, y2=rectBottom, width=boxLine, color=boxLineColor)
    line.new(x1=rectLeft, y1=rectBottom, x2=rectRight, y2=rectBottom, width=boxLine, color=boxLineColor)
    line.new(x1=rectLeft, y1=rectTop, x2=rectLeft, y2=rectBottom, width=boxLine, color=boxLineColor)
//================================================================================================}

// Calculate the 200-period Exponential Moving Average (EMA)
ema150 = ta.ema(close, 150)

// Condition for "buy" signal
buy_condition = close > rectTop and close[1] < rectTop and close[1] > close[2] and close > close[1] and close > ema150
sell_condition = close <= rectBottom and close[1] > rectBottom and close[1] < close[2] and close < close[1] and close < ema150

buy_condition1 = buy_condition[1] ? (close[1] < close) : false
sell_condition1 = sell_condition[1] ? (close[1] > close) : false
//================================================================================================}
//================================================================================================}

// Plot signals on the chart
plotshape(series=sell_condition1, location=location.abovebar, color=color.rgb(204, 23, 135), style=shape.circle, title="Sell Signal", text="Sell", textcolor=color.black)
plotshape(series=buy_condition1, location=location.belowbar, color=color.rgb(18, 189, 205), style=shape.circle, title="Buy Signal", text="Buy", textcolor=color.white)
//================================================================================================}
//================================================================================================}

highestHigh = ta.highest(high, 20)[1]
lowestLow = ta.lowest(low, 20)[1]
//================================================================================================}

// Plot the extreme prices for confirmation
plot(highestHigh, color=color.rgb(222, 13, 233), linewidth=1)
plot(lowestLow, color=color.rgb(15, 195, 198), linewidth=1)
//================================================================================================}

alertcondition(buy_condition, title="Buy Out_of_box", message="Buy out")
alertcondition(sell_condition, title="Sell Out_of_box", message="Sell out")
//================================================================================================}
