Engulfing Box & Lines
// 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="Engulfing Box & Lines", shorttitle="Engulfing Box&Lines", overlay=true)

ema200         =     ta.ema            (close, length=200)
bodySize       =     math.abs          (close - open)
bodySizePrev   =     math.abs          (close[1] - open[1])

Linelenght     =     input.int         (40, "Line length")
lineColorBull  =     input.color       (color.green, "Line color Engufing bullish")
lineColorBear  =     input.color       (color.red, "Line color Engufing bullish")
BoxColorBull   =     input.color       (color.green, "Box color Engufing bullish")
BoxColorBear   =     input.color       (color.rgb(175, 76, 76), "Box color Engufing bearish")
StyleOption    =     input.string      ("dotted (┈)", title="Line Style",
     options   =                       ["solid (─)", "dotted (┈)", "dashed (╌)"], group="Display")

// STEP 2. Convert the input to a proper line style value
lineStyle2 = StyleOption == "dotted (┈)" ? line.style_dotted : StyleOption == "dashed (╌)" ? line.style_dashed : line.style_solid

engulfingBullish = close > high[1] and open < open[1] and bodySize > bodySizePrev
engulfingBearish = close < low[1] and open > open[1] and bodySize > bodySizePrev

lineStyle = engulfingBullish ? lineStyle2 : engulfingBearish ? lineStyle2 : lineStyle2
lineColor = engulfingBullish ? lineColorBull : engulfingBearish ? lineColorBear : na

var line[] linesBullish = array.new_line()
var line[] linesBearish = array.new_line()
// Store the history of Engulfing patterns
var box[] boxesBullish = array.new_box()
var box[] boxesBearish = array.new_box()

if engulfingBullish and close > ema200
    // Create and store the new bullish box
    var line aiji = na
// Lines for  engulfing
    aiji := line.new(x1 = bar_index[1], y1 = high, x2 = bar_index + Linelenght, y2 = high, color = lineColor, width = 2, style = lineStyle)
    array.push(linesBullish, aiji)
    

if engulfingBearish and close < ema200
    // Create and store the new bullish box
    var line aiji2 = na
// Lines for engulfing
    aiji2 := line.new(x1 = bar_index[1], y1 = low, x2 = bar_index + Linelenght, y2 = low, color = lineColor, width = 2, style = lineStyle)
    array.push(linesBullish, aiji2)
    
// Draw rectangles using box.new for Engulfing bars
if engulfingBullish and close > ema200
    // Create and store the new bullish box
    var box boxObj = na
    boxObj := box.new(left = bar_index[1], right = bar_index, top = high, bottom = low, bgcolor=BoxColorBull, border_width=1, border_color=color.black)
    array.push(boxesBullish, boxObj)
    // Add a label
    label.new(x = bar_index, y = high, text = "HiE", textcolor = color.black, color = color.rgb(243, 240, 240), style = label.style_label_center, textalign = text.align_center, size = size.tiny)


    
    // Remove the oldest box if there are more than 10 bullish boxes in the array
    if array.size(boxesBullish) > 10
        box.delete(array.shift(boxesBullish))

if engulfingBearish and close < ema200
    // Create and store the new bearish box
    var box boxObj = na
    boxObj := box.new(left = bar_index[1], right = bar_index, top = high, bottom = low, bgcolor=BoxColorBear, border_width=1, border_color=color.black)
    array.push(boxesBearish, boxObj)
    label.new(x = bar_index, y = low, text = "LoE", textcolor = color.black, color = color.rgb(243, 240, 240), style = label.style_label_center, textalign = text.align_center, size = size.tiny)


    
    // Remove the oldest box if there are more than 10 bearish boxes in the array
    if array.size(boxesBearish) > 10
        box.delete(array.shift(boxesBearish))
        
