{# Inputs: option_rows: list[dict] — each {"kind": "open"|"expired"|"closed", "row": } paginated; sorted open→expired→closed at the route. cash_secured_total / premium_received_total / long_cost_total: Decimal — always derived from truly-open. options_summary / option_counts: derived from truly-open so KPIs don't shift on toggle. expired_count: int — full pre-slice count of expired-pending-broker-close rows. today: date show: "open" | "all" — All adds expired-pending and closed rows to the same table. closed_options_count / closed_options_period_label / closed_realized_total. #} {% set _show = show|default('open') %} {% set _period = period|default('ytd') %} {% set _sort = sort|default('default') %} {# Direction toggles: clicking the active sort flips asc↔desc; clicking a different sort starts in asc. ↑/↓ glyphs label the active column. #} {% set _sort_ticker_next = 'ticker_desc' if _sort == 'ticker' else 'ticker' %} {% set _sort_expiry_next = 'expiry_desc' if _sort == 'expiry' else 'expiry' %} {% set _ticker_active = _sort in ('ticker', 'ticker_desc') %} {% set _expiry_active = _sort in ('expiry', 'expiry_desc') %} {% set _ticker_arrow = '↓' if _sort == 'ticker_desc' else ('↑' if _sort == 'ticker' else '') %} {% set _expiry_arrow = '↓' if _sort == 'expiry_desc' else ('↑' if _sort == 'expiry' else '') %} {% set _rows = option_rows|default([]) %} {% set _closed_count = closed_options_count|default(0) %} {% set _expanded = _rows|length > 0 or (option_counts is defined and (option_counts.long > 0 or option_counts.short > 0)) %} {% set _long_count = option_counts.long|default(0) %} {% set _short_count = option_counts.short|default(0) %} {% set _acct_qs_root %}{% for _a in selected_accounts | default([]) %}&account={{ _a|urlencode }}{% endfor %}{% endset %}

{% if _show == 'all' %}Options{% else %}Open options{% endif %}

{% if _long_count %}{{ _long_count }} long{% endif %} {% if _long_count and _short_count %} · {% endif %} {% if _short_count %}{{ _short_count }} short{% endif %} {% if cash_secured_total %} · ${{ "{:,.0f}".format(cash_secured_total) }} secured{% endif %} {% if premium_received_total %} · short premium +${{ "{:,.2f}".format(premium_received_total) }}{% endif %} {% if long_cost_total %} · long cost ${{ "{:,.2f}".format(long_cost_total) }}{% endif %} {% if _show == 'all' and expired_count is defined and expired_count > 0 %} · {{ expired_count }} expired (awaiting broker close){% endif %} {% if _show == 'all' and _closed_count %} · {{ _closed_count }} closed ({{ closed_options_period_label }}) {% if closed_realized_total is defined and closed_realized_total %} · realized {% if closed_realized_total >= 0 %}+{% endif %}${{ "{:,.2f}".format(closed_realized_total) }} {% endif %} {% endif %}
Show:
Sort:
{% if not _rows %}
{% if _show == 'all' %}No option positions in scope.{% else %}No open option positions.{% endif %}
{% else %} {% if options_summary is defined and options_summary %}
Open contracts
{{ fmt_quantity(options_summary.open_contracts) }}
Net premium
{{ fmt_currency(options_summary.net_premium) }}
Avg DTE
{{ options_summary.avg_dte|round(0)|int }}d
{% endif %}
{# Single grid for OPEN / EXPIRED / CLOSED rows. Same 6-column layout so columns line up across kinds: status pill | ticker | contract | lifecycle (bar OR dates) | value | trailing badge OPEN bar visualizes proximity to expiry on a fixed 0–365d scale: the COLORED fill represents elapsed time (longer = closer to expiry); the uncolored remainder is days left. Gradient runs safe-green (left, plenty of time) → amber → urgent-red (right edge, about to expire), sized to the full 365d track and clipped to fill width. #} {% set MAX_DTE = 365 %} {% for entry in _rows %} {% set kind = entry.kind %} {% if kind == 'open' or kind == 'expired' %} {% set s = entry.row %} {% set dte = (s.expiry - today).days %} {% if kind == 'expired' %}{% set dte_class = 'text-label-2' %} {% elif dte <= 7 %}{% set dte_class = 'text-warn' %} {% elif dte <= 21 %}{% set dte_class = 'text-label-1' %} {% else %}{% set dte_class = 'text-label-2' %}{% endif %} {% set _capped = dte if dte > 0 else 0 %} {% if _capped > MAX_DTE %}{% set _capped = MAX_DTE %}{% endif %} {% set fill_pct = ((MAX_DTE - _capped) / MAX_DTE * 100)|round(0, 'common') %} {% set is_long = s.side == 'long' %}
{% if kind == 'expired' %}
EXPIRED
{{ 'LONG' if is_long else 'SHORT' }}
{% else %}
{{ 'LONG' if is_long else 'SHORT' }}
{% endif %}
{% if s.call_put == 'C' %}CALL{% else %}PUT{% endif %} ${{ "%g"|format(s.strike) }}
{{ s.expiry.strftime('%m/%d/%y') }}
{% if s.opened_at %}opened {{ (today - s.opened_at).days }}d ago{% else %}— {% endif %}
{% set _bg_size = (10000 / fill_pct)|round(0, 'common') if fill_pct > 0 else 100 %}
{% if is_long %}-${{ "{:,.2f}".format(s.cash_basis) }}{% else %}+${{ "{:,.2f}".format(s.cash_basis) }}{% endif %}
{% if is_long %}cost paid{% elif s.call_put == 'P' %}${{ "{:,.0f}".format(s.cash_secured) }} secured{% else %}premium{% endif %}
{{ dte }}d
{% else %} {# kind == "closed" — historical realized lot. Reuse the same grid columns so OPEN and CLOSED rows line up; the 4th column carries opened→closed dates instead of a DTE bar, and the trailing badge switches to ST/LT term. #} {% set r = entry.row %} {% set _is_call = r.option_call_put == 'C' %} {% set _pl = r.realized_pl %} {% set _term_short = 'LT' if r.term == 'Long Term' else 'ST' %}
CLOSED
{# Reformat option_expiry (ISO YYYY-MM-DD straight from Schwab GL) into M/D/YY to match the open/expired rows above. #} {% set _exp_fmt = r.option_expiry %} {% if r.option_expiry and r.option_expiry|length >= 10 %} {% set _y2 = r.option_expiry[2:4] %} {% set _m = r.option_expiry[5:7]|int %} {% set _d = r.option_expiry[8:10]|int %} {% set _exp_fmt = "%d/%d/%s"|format(_m, _d, _y2) %} {% endif %}
{% if _is_call %}CALL{% else %}PUT{% endif %} ${{ "%g"|format(r.option_strike) }}
{{ _exp_fmt }}
opened {{ r.opened_date.strftime('%m/%d/%y') }} → closed {{ r.closed_date.strftime('%m/%d/%y') }} · qty {{ "%g"|format(r.qty) }} {% if r.wash_sale %}wash{% endif %}
{% if _pl >= 0 %}+{% endif %}${{ "{:,.2f}".format(_pl) }}
realized · cost ${{ "{:,.0f}".format(r.cost_basis) }}
{{ _term_short }}
{% endif %} {% endfor %}
{% set page_base_url = '/holdings/options' %} {% set page_extra_qs %}&show={{ _show }}&period={{ _period }}&sort={{ _sort }}{{ _acct_qs_root }}{% endset %} {% set page_target = '#holdings-options' %} {% set page_swap = 'innerHTML' %} {% include "_pagination_footer.html" %} {% endif %}