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

//@version=5

// @description ATR_Info: Calculates ATR without paranormal bars
library("ATR_Info", overlay = true)

// @function ATR_WPB: Calculates ATR without paranormal bars
// @param source ATR_WPB: (series float) The sequence of data on the basis of which the ATP calculation will be made
// @param period ATR_WPB: (int) Sequence size for ATR calculation
// @param psmall ATR_WPB: (float) Coefficient for paranormally small bar
// @param pbig ATR_WPB: (float) Coefficient for paranormally big bar
// @returns ATR_WPB: (float) ATR without paranormal bars
export ATR_WPB(series float source,  int period, float psmall, float pbig) =>
	MaxLoop = period * 2
	avg = ta.sma(source[1], MaxLoop)
	for n = 1 to 3 by 1
    	nsource = array.new_float(0)
    	for i = 1 to MaxLoop by 1
        	if source[i] / pbig < avg and source[i] * psmall < avg
				array.push(nsource, source[i])
            if array.size(nsource) >= period
                break
        navg = array.avg(nsource)
    	if navg == avg 
            break
        avg := navg
	avg


// Example code


var string GP1 = "ATR parametrs"
NumBars = input(5, title='Number of bars to calculate', group = GP1)
SizePrnlSmall = input(0.7, title='Size paranormally small bar', group = GP1)
SizePrnlLarge = input(1.8, title='Size paranormally large bar', group = GP1)

var string GP2 = "Display"
string  tableYposInput = input.string("top", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP2, display = display.none)
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"], group = GP2, display = display.none)
color   bullColorInput = input.color(color.new(color.green, 30), "Bull", inline = "12", group = GP2)
color   bearColorInput = input.color(color.new(color.red, 30), "Bear", inline = "12", group = GP2)

// If the last bar is considered closed, then we take it into account, otherwise we ignore
Offset = barstate.islast and barstate.isconfirmed ? 0 : 1


ATR = ATR_WPB(ta.tr[Offset], NumBars, SizePrnlSmall, SizePrnlLarge) // Get the ATR value
DailyATR = request.security(syminfo.tickerid, "1D", ATR) // Get daily the ATR value 
DailyClose = request.security(syminfo.tickerid, "1D", close) // Get daily the Close value 

PTR = math.round(ta.tr/DailyATR*100) //  Percent TrueRunge completed by ATR 

FilledATR_WPB =math.abs(DailyClose[1]-close) // Current value of ATR 
PFilledATR_WPB =math.round(100*FilledATR_WPB/DailyATR) // Percent

MaxFilledATR = math.abs(((close > close[1]) ? high : low) - close[1]) // How much did the ATR go to the maximum
PMaxFilledATR = math.round(100*MaxFilledATR/DailyATR)

// Plot table with data

row = 0
var table panel = table.new(tableYposInput + "_" + tableXposInput, 2, 5)
if barstate.islast
    // Table data
    row += 1
    table.cell(panel, 0, row, "ATR:", text_halign = text.align_left, text_size = size.normal)
    table.cell(panel, 1, row, str.tostring(DailyATR),  text_halign = text.align_left, text_size = size.normal)

    if (timeframe.isdwm)
        row += 1
        table.cell(panel, 0, row, "True Range:", text_halign = text.align_left, text_size = size.small)
        table.cell(panel, 1, row, str.tostring(ta.tr) + " (" + str.tostring(PTR) + "%)",  bgcolor = color.new(PTR < 50 ? bullColorInput : bearColorInput, 80), text_halign = text.align_left, text_size = size.small)

    row += 1
    table.cell(panel, 0, row, "Filled ATR:", text_halign = text.align_left, text_size = size.small)
    table.cell(panel, 1, row, str.tostring(FilledATR_WPB) + " (" + str.tostring(PFilledATR_WPB) + "%)",  bgcolor = color.new(PFilledATR_WPB < 50 ? bullColorInput : bearColorInput, 80), text_halign = text.align_left, text_size = size.small)

    if (timeframe.isdwm)
        row += 1
        table.cell(panel, 0, row, "Max filled ATR:", text_halign = text.align_left, text_size = size.small)
        table.cell(panel, 1, row, str.tostring(MaxFilledATR)  + " (" + str.tostring(PMaxFilledATR) + "%)",  bgcolor = color.new(MaxFilledATR < 50 ? bullColorInput : bearColorInput, 80), text_halign = text.align_left, text_size = size.small)
