Critical Levels Mixing Price Action, Volatility and Volume
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pascorp

//@version=5
indicator('Critical Levels Mixing Price Action, Volatility and Volume', shorttitle='CLM', overlay=true)
// ————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>  Inputs  <<<<<<<<<<<<<<<<<<<<<<<<<<<
// ————————————————————————————————————————————————————————————
cutLen          = input(true, title='Analize all', group='Time & Alerts', inline='time')
lenBckTst       = input.int(2880,title='/minutes', minval=1, group='Time & Alerts', group='Time & Alerts', inline='time')
alNew           = input(true, title='Alert for new', group='Time & Alerts', inline='time')


coreMax         = input.float(defval=50, title='max Core size %', inline='1', group='Price Action')
coreMin         = input.float(defval=25, title='min Core size %', inline='1', group='Price Action')
maxLenMinorTail = input.float(defval=12, title='Maximum length for shorter tail %', group='Price Action')
lenVol          = input(defval=120, title='Period MA Volume', inline='2', group='Volume')
multVol         = input.float(defval=1, title='Multiplier MA Volume', inline='2', group='Volume')
atrLen          = input.int(defval=21, title='ATR Period', group='Volatility', inline='1')
atrPerc         = input.int(defval=100, title='ATR Percentage %', group='Volatility', inline='1')


// ————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>  Core  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ————————————————————————————————————————————————————————————
startBckTst = time > timenow - lenBckTst * 60000
shw = false

if  startBckTst or cutLen
    core = math.abs(close-open)
    total = high-low
    upperTail = close>open? high-close : high-open
    lowerTail = close>open? open-low : close-low
    
    maVol = ta.ema(volume, lenVol)*multVol
    
    // ------ Conditions -------
    condPA          = core<((total/100)*coreMax) and core>((total/100)*coreMin) and(lowerTail<((total/100)*maxLenMinorTail) or upperTail<((total/100)*maxLenMinorTail))
    condVolatility  = ta.tr(true)>((ta.atr(atrLen)/100)*atrPerc)
    condVolume      =  volume>maVol
    
    // ------ Box & Alert ------
    if  condPA and condVolatility and condVolume
        shw := true
        if alNew
            alert('New Levels', alert.freq_once_per_bar)
        if close>open
            if upperTail > lowerTail
                box.new(time, close, time + 1, high, xloc = xloc.bar_time, extend = extend.right,border_color=color(na), bgcolor =color.new(color.red,80))
            else
                box.new(time, open, time + 1, low, xloc = xloc.bar_time, extend = extend.right,border_color=color(na), bgcolor =color.new(color.red,80))
        else
            if upperTail > lowerTail
                box.new(time, open, time + 1, high, xloc = xloc.bar_time, extend = extend.right,border_color=color(na), bgcolor =color.new(color.red,80))
            else
                box.new(time, close, time + 1, low, xloc = xloc.bar_time, extend = extend.right,border_color=color(na), bgcolor =color.new(color.red,80))

plotshape(shw, location=location.bottom)