Candle Classifier
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SamRecio

//@version=5
indicator("Candle Classifier", shorttitle = "🕯",overlay = true)

high_wick_per = input.float(20,minval = 0,title = "High Wick %", group = "Candle Classification") 
body_per = input.float(60,minval = 0,title = "Body %", group = "Candle Classification") 
low_wick_per = input.float(20,minval = 0,title = "Low Wick %", group = "Candle Classification") 
variance = input.float(10,minval = 0, title = "Variance <->", group = "Candle Classification")
candle_color = input.string("All", title = "Candle Color", options = ["All","Green","Red"])
tl1 = input.string("bottom", title = "Table Location", options = ["top", "middle", "bottom"], group = "Display Settings", inline = "1")
tl2 = input.string("right", title = "", options = ["left", "center", "right"], group = "Display Settings", inline = "1")
def_color = input.color(color.white, title = "Default Display Color", group = "Display Settings", inline = "2")
tsize = input.string("normal", title = "Size", options = ["small", "normal", "large", "huge"], group = "Display Settings", inline = "2")
tab_loc = tl1+"_"+tl2

c_range = (high - low) 
c_high = (high - math.max(open,close))/c_range * 100
c_body = (math.max(open,close) - math.min(open,close))/c_range * 100
c_low = (math.min(open,close) - low)/c_range * 100
c_color = math.max(open,close) == close?1:0

var_check(_src,_lvl) =>
    var bool output = na
    if variance > 0
        output := _src > (_lvl - variance) and _src < (_lvl + variance)?true:false
    else
        output := _src == _lvl?true:false

color_check(_c) =>
    _c == "All"?true:
     _c == "Green" and c_color == 1?true:
     _c == "Red" and c_color == 0? true:
     false

high_check = var_check(c_high,high_wick_per)
body_check = var_check(c_body,body_per)
low_check = var_check(c_low,low_wick_per)
fire = high_check and body_check and low_check and color_check(candle_color)
o_color = candle_color == "All" ? def_color:candle_color == "Green"?color.green:candle_color == "Red"?color.red:na

plotshape(fire, style = shape.diamond,size = size.small, color = color.new(def_color,20), editable = false)
plot(c_high, color = color.new(def_color,100), editable = false)
plot(c_body, color = color.new(def_color,100), editable = false)
plot(c_low, color = color.new(def_color,100), editable = false)

t1 = table.new(tab_loc,2,1)
if variance > 0
    table.cell(t1,0,0,text =
     "|\n" + 
     "█\n" +            
     "|\n" 
     , text_color = o_color, text_size = tsize, text_halign = text.align_center) 
    table.cell(t1,1,0,text = 
     str.tostring(high_wick_per - variance < 0?0:high_wick_per - variance) + "% - " + str.tostring(high_wick_per + variance) + "%\n" +
     str.tostring(body_per - variance < 0?0:body_per - variance) + "% - " + str.tostring(body_per + variance) + "%\n" +              
     str.tostring(low_wick_per - variance < 0?0:low_wick_per - variance) + "% - " + str.tostring(low_wick_per + variance) + "%"
     , text_color = o_color, text_size = tsize, text_halign = text.align_left)
if variance == 0
    table.cell(t1,0,0,text =
     "|\n" + 
     "█\n" +            
     "|\n" 
     , text_color = o_color, text_size = tsize, text_halign = text.align_center)        
    table.cell(t1,1,0,text = 
     str.tostring(high_wick_per) + "%\n" +
     str.tostring(body_per) + "%\n" +              
     str.tostring(low_wick_per) + "%"
     , text_color = o_color, text_size = tsize, text_halign = text.align_left)    

alertcondition(fire, title = "Candle Found", message = "Candle Found!")