[KVA]Body Percentage Counter 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kamvia


//@version=5
indicator("[KVA]Body Percentage Counter ")
 


isGreen(c,o) =>
	c > o

isRed(c,o) =>
	c < o

//enter values here
float[] greenPercents = array.from(0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.2,0.3, 0.4,0.5,0.6,0.7,0.8,0.9,1,1.3,1.5,2,3,4,5,6,7,8,9,10,15,20)





// helper functions
percent_change(new, old) => 100 * (new - old) / old
calc_price_diff(entry_amt, price_change_percent) => entry_amt + (price_change_percent * entry_amt)

formatPercent(n) => str.format("{0,number,percent}", n)
formatNumber(n) => str.format("{0,number}", n)
formatCurrency(n) => str.format("{0,number,currency}", n) 

// table functions
buildBodyRows(pb,  greenPercents, greenCountByThreshold , num_entries,r) =>
   
    for i = 0 to num_entries-1
        curr_amt = array.get(greenCountByThreshold , i)
        
        table.cell(table_id = pb, column = i, row = r, text = str.tostring(curr_amt), text_color = color.black, text_halign=text.align_left)
        
buildTotalRow(pb, greenCountByThreshold , redCountByThreshold , greenPercents) =>
    num_entriesp = array.size(greenPercents)
    for i = 0 to num_entriesp-1
        curr_amtp = array.get(greenPercents, i)
        total = array.get(greenCountByThreshold , i) + array.get(redCountByThreshold , i)
        table.cell(table_id = pb, column = i, row = 3, text = str.tostring(total), text_color = color.black)

buildHeaderRow(pb,greenPercents) =>
    num_entriesp = array.size(greenPercents)
    for i = 0 to num_entriesp-1
        curr_amtp = array.get(greenPercents, i)

        table.cell(table_id = pb, column = i, row = 0, text = str.tostring(curr_amtp), text_color = color.black)
    
buildTable(num_entries,  greenPercents, greenCountByThreshold ,redCountByThreshold ) =>
    num_columns = array.size(greenPercents)
    var pb = table.new(position = position.top_right, columns = num_columns, rows = num_entries+2)
    buildHeaderRow(pb,greenPercents)
	buildBodyRows(pb,  greenPercents, greenCountByThreshold , num_entries,1)
    buildBodyRows(pb,  greenPercents, redCountByThreshold , num_entries,2)
    buildTotalRow(pb,  greenCountByThreshold , redCountByThreshold , greenPercents)


///////////////////////////////////////////////////////////////////////////////
// main
num_entries = array.size(greenPercents)

// get current amounts
greenCountByThreshold  = array.new_float(num_entries,0)
redCountByThreshold  = array.new_float(num_entries,0)

  







// create table
if barstate.islast
    for barindex=0 to 5000
        price_change_percent =  math.abs(percent_change(close[barindex], open[barindex]))
        if isGreen(close[barindex],open[barindex])
            for i = 0 to num_entries-1
                lv = array.get(greenCountByThreshold , i)
              
                if  i==0

                    if price_change_percent < array.get(greenPercents, i)
                        
                        array.set(greenCountByThreshold , i, lv+1)
                else 
              
                    if (price_change_percent >=array.get(greenPercents, i-1)) and (price_change_percent < array.get(greenPercents, i))
                      
                        array.set(greenCountByThreshold , i, lv+1)
        if isRed(close[barindex],open[barindex])
            for i = 0 to num_entries-1
                lv = array.get(redCountByThreshold , i)
               
                if  i==0

                    if price_change_percent < array.get(greenPercents, i)
                        
                        array.set(redCountByThreshold , i, lv+1)
                else 
               
                    if (price_change_percent >=array.get(greenPercents, i-1)) and (price_change_percent < array.get(greenPercents, i))
                        //lv1 = array.get(greenCountByThreshold , i)
                        array.set(redCountByThreshold , i, lv+1)


    buildTable(num_entries,  greenPercents, greenCountByThreshold ,redCountByThreshold )    
	
printDebug(txt) => 
  var table t = table.new(position.bottom_right, 1, 1),table.cell(t, 0, 0, str.tostring(txt), bgcolor = color.yellow)
