engineportf's picture
Upload folder using huggingface_hub
558db1e verified
Raw
History Blame Contribute Delete
2.48 kB
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"<tr><td><strong>{t}</strong></td>"
f"<td>{curr}{m['avg_cost']:.2f}</td><td>{curr}{m['cur_price']:.2f}</td>"
f"<td style='color:{g_col}'>{curr}{m['unreal_gain']:+,.0f}</td>"
f"<td>{m['days_held']}d</td>"
f"<td style='color:{r_col}'>{m['rate']:.0%}</td>"
f"<td style='color:#f85149'>{curr}{m['tax_if_sold']:,.0f}</td></tr>\n"
)
cb_table = (
'<p class="st">Cost Basis &amp; Embedded Tax Liability</p>'
'<div class="cc" style="overflow-x:auto"><table><thead><tr>'
'<th>Ticker</th><th>Avg Cost</th><th>Current</th>'
'<th>Unrealised P&amp;L</th><th>Held</th><th>Rate</th>'
f'<th>Tax if Sold</th></tr></thead><tbody>{cb_rows}</tbody></table></div>'
)
else:
cb_table = ""
tax_html = (
'<p class="st">Tax Analysis</p><div class="mg">'
f'<div class="mc"><div class="ml">LT / ST Rate</div>'
f'<div class="mv yellow">{cfg.get("tax_rate_lt",0):.0%} / {cfg.get("tax_rate_st",0):.0%}</div></div>'
f'<div class="mc"><div class="ml">Pre-Tax Return</div><div class="mv">{pre_tax:+.2%}</div></div>'
f'<div class="mc"><div class="ml">Tax Drag</div><div class="mv red">-{drag_bp:.0f} bp/yr</div></div>'
f'<div class="mc"><div class="ml">After-Tax Return</div><div class="mv green">{after_tax:+.2%}</div></div>'
+ (f'<div class="mc"><div class="ml">Liquidation Tax</div>'
f'<div class="mv red">{curr}{total_tt:,.0f}</div></div>' if tax_meta else "")
+ f'</div>{cb_table}'
)
return tax_html