from tulip.core import TulipLibrary
import pandas as pd
from arcticdb import QueryBuilder
import ipywidgets as widgets
from IPython.display import display, HTML, Markdown
risk_lib = TulipLibrary.risk()
# Load data
last_date = risk_lib.tail("counterparty_dashboard", 1).data.index[0]
q = QueryBuilder()
q = q[q["pricing_date"] == last_date]
df = risk_lib.read("counterparty_dashboard", query_builder=q).data.reset_index()# Split into Equities and CDS
equities = df[df["security"].str.contains("EQUITY")].copy()
cds = df[df["security"].str.contains("CDS")].copy()
# Clean up column names for display
display_cols = [
"SHORT_NAME",
"PX_LAST",
"CHG_PCT_1D",
"CHG_PCT_5D",
"CHG_PCT_1M",
"CHG_PCT_1YR",
]
col_names = {
"SHORT_NAME": "Name",
"PX_LAST": "Price",
"CHG_PCT_1D": "1D %",
"CHG_PCT_5D": "5D %",
"CHG_PCT_1M": "1M %",
"CHG_PCT_1YR": "1Y %",
}
def style_changes(val):
"""Color code positive/negative changes"""
if pd.isna(val):
return ""
color = "#2e7d32" if val > 0 else "#c62828" if val < 0 else "#666"
return f"color: {color}; font-weight: 500"
def format_table(data, title, invert_colors=False):
"""Create a styled table for display"""
table = data[display_cols].copy()
table.columns = [col_names.get(c, c) for c in table.columns]
change_cols = ["1D %", "5D %", "1M %", "1Y %"]
styled = (
table.style.format(
{
"Price": "{:,.2f}",
"1D %": "{:+.2f}%",
"5D %": "{:+.2f}%",
"1M %": "{:+.2f}%",
"1Y %": "{:+.2f}%",
}
)
.map(lambda v: style_changes(-v if invert_colors else v), subset=change_cols)
.set_properties(**{"text-align": "right"}, subset=["Price"] + change_cols)
.set_properties(**{"text-align": "left"}, subset=["Name"])
.set_table_styles(
[
{
"selector": "th",
"props": [
("background-color", "#000000"),
("color", "white"),
("padding", "10px"),
("font-weight", "600"),
],
},
{"selector": "td", "props": [("padding", "8px 12px")]},
{"selector": "tr:hover", "props": [("background-color", "#f5f5f5")]},
{
"selector": "caption",
"props": [
("caption-side", "top"),
("font-size", "1.1em"),
("font-weight", "bold"),
("padding", "10px"),
("color", "#000000"),
],
},
]
)
.hide(axis="index")
.set_caption(title)
)
return styledCounterparties¶
# Dashboard Header
pricing_dt = last_date.strftime("%B %d, %Y at %H:%M")
Markdown(f"_As of {pricing_dt}_")
# Equity Prices - Green = stock up (positive for counterparty health)
display(format_table(equities, "Bank Equity Prices"))# CDS Spreads - Colors inverted: spread widening (positive) = bad, tightening (negative) = good
display(format_table(cds, "CDS Spreads (5Y Senior)", invert_colors=True))# Legend
legend_html = """
<div style="margin-top: 20px; padding: 12px 16px; background: #f8f9fa;
border-left: 4px solid #1a237e; border-radius: 4px; font-size: 0.9em;">
<strong>Color Guide:</strong>
<span style="color: #2e7d32; margin-left: 15px;">▲ Green = Favorable</span>
<span style="color: #c62828; margin-left: 15px;">▼ Red = Adverse</span>
<span style="color: #666; margin-left: 20px; font-style: italic;">
(For CDS: spread tightening is favorable, widening is adverse)
</span>
</div>
"""
display(HTML(legend_html))from tulip_mania.notebook_related import notebook_updated
notebook_updated()