EWMA Implied Volatility based on Historical Volatility
// 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("EWMA Implied Volatility", overlay=true)

// Exponentially Weighted Volatility
ewma_function(source, length, annual_volatility) =>
    var lambda = (length - 1) / (length + 1)
    squared = math.pow(source, 2)
    float variance = na
    variance := lambda * nz(variance[1], squared) + (1.0 - lambda) * squared
    annual_volatility * math.sqrt(variance)

close_daily  = request.security(syminfo.tickerid, "D", close)
close_weekly = request.security(syminfo.tickerid, "W", close)
close_monthly = request.security(syminfo.tickerid, "M", close)

logarithm_daily = math.log(close_daily / close_daily[1]) 
logarithm_weekly = math.log(close_weekly / close_weekly[1]) 
logarithm_monthly = math.log(close_monthly / close_monthly[1]) 


var sqrt_daily = math.sqrt(252) * 100
var sqrt_weekly = math.sqrt(52) * 100
var sqrt_monthly = math.sqrt(12) * 100

yearly_volatility = ewma_function(logarithm_daily, 21, sqrt_daily)/100 
daily_volatility = (yearly_volatility / math.sqrt(252) ) 

weekly_volatility = (yearly_volatility / math.sqrt(52) ) 
monthly_volatility = (yearly_volatility / math.sqrt(12) ) 



////////////////////////////////////////////////////////////////////////////
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_vol = table.new(pos, 15, 15, border_width=1)


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, 2, text)


if barstate.islast
    table.cell(table_vol, 0, 0, 'EWMA Volatility', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table.cell(table_vol, 0, 4, 'Yearly Volatility', text_color=color.white, text_size=size.normal, bgcolor=color.aqua)
    table.cell(table_vol, 0, 1, 'Daily Volatility', text_color=color.white, text_size=size.normal, bgcolor=color.green)
    table.cell(table_vol, 0, 2, 'Weekly Volatility', text_color=color.white, text_size=size.normal, bgcolor=color.blue)
    table.cell(table_vol, 0, 3, 'Monthly Volatility', text_color=color.white, text_size=size.normal, bgcolor=color.orange)

    table_fillCell(table_vol, 1, 4, yearly_volatility , 'Text', color.white)
    table_fillCell(table_vol, 1, 1, daily_volatility , 'Text', color.white)
    table_fillCell(table_vol, 1, 2, weekly_volatility, 'Text', color.white)
    table_fillCell(table_vol, 1, 3, monthly_volatility, 'Text', color.white)



