def build_tax_html(cfg, w_risky, cash_w, rfr, opt_ret_val, tax_meta, curr, model_info, exp_rets): tax_html = "" if cfg.get('tax_enabled', False): _ptr = model_info.get('pre_tax_rets', exp_rets) pre_tax = float(w_risky @ _ptr.reindex(w_risky.index).fillna(0.0)) + (cash_w * rfr) after_tax = opt_ret_val drag_bp = (pre_tax - after_tax) * 10000 total_tt = 0.0; cb_rows = "" if tax_meta: for t, m in tax_meta.items(): total_tt += m['tax_if_sold'] g_col = "#3fb950" if m['unreal_gain'] >= 0 else "#f85149" r_col = "#f85149" if m['rate'] > 0.25 else "#e3b341" cb_rows += ( f"{t}" f"{curr}{m['avg_cost']:.2f}{curr}{m['cur_price']:.2f}" f"{curr}{m['unreal_gain']:+,.0f}" f"{m['days_held']}d" f"{m['rate']:.0%}" f"{curr}{m['tax_if_sold']:,.0f}\n" ) cb_table = ( '

Cost Basis & Embedded Tax Liability

' '
' '' '' f'{cb_rows}
TickerAvg CostCurrentUnrealised P&LHeldRateTax if Sold
' ) else: cb_table = "" tax_html = ( '

Tax Analysis

' f'
LT / ST Rate
' f'
{cfg.get("tax_rate_lt",0):.0%} / {cfg.get("tax_rate_st",0):.0%}
' f'
Pre-Tax Return
{pre_tax:+.2%}
' f'
Tax Drag
-{drag_bp:.0f} bp/yr
' f'
After-Tax Return
{after_tax:+.2%}
' + (f'
Liquidation Tax
' f'
{curr}{total_tt:,.0f}
' if tax_meta else "") + f'
{cb_table}' ) return tax_html