Volatility Calculator for Daily Top and Bottom Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=5
indicator("Volatility Calculator", overlay=true)
atr_length = input.int(14)
atr_logic = ta.atr(atr_length)
atr_percent = atr_logic / close * 100

top_channel = close + (close*atr_percent)/100
bot_channel = close - (close*atr_percent)/100

// plot(top_channel)
// plot(bot_channel)

data_top_cross = ta.crossover(high, top_channel[1])
data_bot_cross = ta.crossunder(low, bot_channel[1])

// plotshape(data_top_cross, style=shape.xcross, location=location.abovebar)
// plotshape(data_bot_cross, style=shape.xcross, location=location.belowbar)

var int count_top_crosses = 0
var int count_bot_crosses = 0

if(data_top_cross)
    count_top_crosses:=count_top_crosses+1

if(data_bot_cross)
    count_bot_crosses:=count_bot_crosses+1

var int count_candles = 0

if(close!=0)
    count_candles := count_candles+1
    
// plot(count_bot_crosses, color=color.red)
// plot(count_top_crosses, color=color.green)
//plot(count_candles, color=color.white)

ratio = (count_top_crosses + count_bot_crosses) / count_candles

////////////////////////////////////////////////////////////////////////////
posInput = input.string(title='Position', defval='Middle Right', options=['Bottom Left', 'Bottom Right', 'Top Left', 'Top Right', 'Middle Right'])
var pos = posInput == 'Bottom Left' ? position.bottom_left : posInput == 'Bottom Right' ? position.bottom_right : posInput == 'Top Left' ? position.top_left : posInput == 'Top Right' ? position.top_right : posInput == 'Middle Right' ? position.middle_right: na


var table table_atr = table.new(pos, 8, 8, border_width=3)


table_fillCell(_table, _column, _row, _value, _timeframe, _c_color) =>
    _transp = 70  
    _cellText = str.tostring(_value, '#.###')
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, width=5)
    table.cell_set_text_size(table_atr, 0, 0, size.normal)
    table.cell_set_text_size(table_atr, 0, 1, size.normal)
    table.cell_set_text_size(table_atr, 0, 2, size.normal)
    table.cell_set_text_size(table_atr, 0, 3, size.normal)
    table.cell_set_text_size(table_atr, 1, 0, size.normal)
    table.cell_set_text_size(table_atr, 1, 1, size.normal)
    table.cell_set_text_size(table_atr, 1, 2, size.normal)
    table.cell_set_text_size(table_atr, 1, 3, size.normal)
    table.cell_set_text_size(table_atr, 2, 0, size.normal)
    table.cell_set_text_size(table_atr, 2, 1, size.normal)
    table.cell_set_text_size(table_atr, 2, 2, size.normal)
    table.cell_set_text_size(table_atr, 2, 3, size.normal)
    //table.cell_set_text_size(table_atr, 0, 2, text)


if barstate.islast

    table.cell(table_atr, 0, 0, 'TOP Crosses', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 0, 1, count_top_crosses, 'Text', color.white)
    table.cell(table_atr, 1, 0, 'BOT Crosses', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 1, 1, count_bot_crosses, 'Text', color.white)
    table.cell(table_atr, 2, 0, 'Total Candles', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 2, 1, count_candles, 'Text', color.white)
    table.cell(table_atr, 3, 0, 'Occurance Ratio', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 3, 1, ratio, 'Text', color.white)

