User:TedSwippet/Interactive calculator

 "use strict";

var twr_descr = { l1tw: {label: 'L1TW',  init: '3.9'}, l2tw: {label: 'L2TW',  init: '30'}, yield: {label: 'Yield', init: '2'}, ter:  {label: 'TER',   init: '0.08'} }; var twr_ids = ['l1tw', 'l2tw', 'yield', 'ter'];

function generate_twr_table(namespace, obj) { if (!obj.twr_counter) obj.twr_counter = 0;

var prefix = namespace + '-' + ++obj.twr_counter + '-'; var table = ' '; return table; } document.write(generate_twr_table('twr', window));

function calculate_twr(prefix) { function get_values(ids) { var values = {}; for (var id of ids) { var value = parseFloat(document.getElementById(prefix + id).value); if (isNaN(value)) value = 0; values[id] = value; }   return values; } function set_values(values) { for (var id in values) { document.getElementById(prefix + id).value = values[id] + '%'; } }  function compute(v) { return (v.yield * v.l1tw/100) + ((v.yield * (1 - v.l1tw/100) - v.ter) * v.l2tw/100); }

var values = get_values(twr_ids); values.result = compute(values).toFixed(4); set_values(values); }

(Documentation)
This template calculates a Tax Withholding Ratio (TWR) for an ETF when held by a US nonresident alien, from values entered into the page by the user. The definition of TWR is:

TWR = (Yield × L1TW) + ((Yield × (1 - L1TW) - TER) × L2TW)

Where:
 * L1TW: Percentage of tax withholding by a security's home country on dividends distributed by that security to the fund (Level 1).
 * L2TW: Percentage of tax withholding by the country where the fund is domiciled on the dividends distributed to the investor by the fund (Level 2).
 * Yield: Annual yield of the fund.
 * TER: The fund's Total Expense Ratio.

This template uses JavaScript to provide live in-page calculations. The computed TWR is updated on pressing the 'Calculate' button, or when the user changes any of the input values.

Usage
Invoke with:

This template takes no arguments. The default input values are currently hardcoded.