From 5aed55665ca4b8a87457706124833ba91c515e1f Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 26 Feb 2026 15:51:55 -0800 Subject: [PATCH] ai: add illiquid assets --- src/cache/store.zig | 1 + src/cli/main.zig | 46 +++++++ src/format.zig | 42 ++++++ src/models/portfolio.zig | 12 ++ src/providers/openfigi.zig | 271 +++++++++++++++++++++++++++++++++++++ src/tui/main.zig | 73 +++++++++- 6 files changed, 443 insertions(+), 2 deletions(-) create mode 100644 src/providers/openfigi.zig diff --git a/src/cache/store.zig b/src/cache/store.zig index 55b2956..a0ab1c4 100644 --- a/src/cache/store.zig +++ b/src/cache/store.zig @@ -824,6 +824,7 @@ pub fn serializePortfolio(allocator: std.mem.Allocator, lots: []const Lot) ![]co .option => "option", .cd => "cd", .cash => "cash", + .illiquid => "illiquid", .watch => "watch", .stock => unreachable, }; diff --git a/src/cli/main.zig b/src/cli/main.zig index 56cae8a..5353db5 100644 --- a/src/cli/main.zig +++ b/src/cli/main.zig @@ -1545,6 +1545,52 @@ fn cmdPortfolio(allocator: std.mem.Allocator, config: zfin.Config, svc: *zfin.Da try reset(out, color); } + // Illiquid assets section + if (portfolio.hasType(.illiquid)) { + try out.print("\n", .{}); + try setBold(out, color); + try out.print(" Illiquid Assets\n", .{}); + try reset(out, color); + try setFg(out, color, CLR_MUTED); + var il_hdr_buf: [80]u8 = undefined; + try out.print("{s}\n", .{fmt.fmtIlliquidHeader(&il_hdr_buf)}); + var il_sep_buf1: [80]u8 = undefined; + try out.print("{s}\n", .{fmt.fmtIlliquidSep(&il_sep_buf1)}); + try reset(out, color); + + for (portfolio.lots) |lot| { + if (lot.lot_type != .illiquid) continue; + var il_row_buf: [160]u8 = undefined; + try out.print("{s}\n", .{fmt.fmtIlliquidRow(&il_row_buf, lot.symbol, lot.shares, lot.note)}); + } + // Illiquid total + var il_sep_buf2: [80]u8 = undefined; + try setFg(out, color, CLR_MUTED); + try out.print("{s}\n", .{fmt.fmtIlliquidSep(&il_sep_buf2)}); + try reset(out, color); + var il_total_buf: [80]u8 = undefined; + try setBold(out, color); + try out.print("{s}\n", .{fmt.fmtIlliquidTotal(&il_total_buf, portfolio.totalIlliquid())}); + try reset(out, color); + } + + // Net Worth (if illiquid assets exist) + if (portfolio.hasType(.illiquid)) { + const illiquid_total = portfolio.totalIlliquid(); + const net_worth = summary.total_value + illiquid_total; + var nw_buf: [24]u8 = undefined; + var liq_buf: [24]u8 = undefined; + var il_buf: [24]u8 = undefined; + try out.print("\n", .{}); + try setBold(out, color); + try out.print(" Net Worth: {s} (Liquid: {s} Illiquid: {s})\n", .{ + fmt.fmtMoney(&nw_buf, net_worth), + fmt.fmtMoney(&liq_buf, summary.total_value), + fmt.fmtMoney(&il_buf, illiquid_total), + }); + try reset(out, color); + } + // Watchlist (from watch lots in portfolio + separate watchlist file) { var any_watch = false; diff --git a/src/format.zig b/src/format.zig index f709dfd..265878a 100644 --- a/src/format.zig +++ b/src/format.zig @@ -121,6 +121,48 @@ pub fn fmtCashTotal(buf: []u8, total: f64) []const u8 { return buf[0..pos]; } +/// Format the illiquid section column header: " Asset Value Note" +pub fn fmtIlliquidHeader(buf: []u8) []const u8 { + const w = cash_acct_width; + var pos: usize = 0; + @memcpy(buf[0..2], " "); + pos += 2; + const asset_label = "Asset"; + @memcpy(buf[pos..][0..asset_label.len], asset_label); + @memset(buf[pos + asset_label.len ..][0 .. w - asset_label.len], ' '); + pos += w; + buf[pos] = ' '; + pos += 1; + const val_label = "Value"; + const val_pad = if (val_label.len < 14) 14 - val_label.len else 0; + @memset(buf[pos..][0..val_pad], ' '); + pos += val_pad; + @memcpy(buf[pos..][0..val_label.len], val_label); + pos += val_label.len; + @memcpy(buf[pos..][0..2], " "); + pos += 2; + const note_label = "Note"; + @memcpy(buf[pos..][0..note_label.len], note_label); + pos += note_label.len; + return buf[0..pos]; +} + +/// Format an illiquid asset row: " House $1,200,000.00 Primary residence" +/// Returns a slice of `buf`. +pub fn fmtIlliquidRow(buf: []u8, name: []const u8, value: f64, note: ?[]const u8) []const u8 { + return fmtCashRow(buf, name, value, note); +} + +/// Format the illiquid total separator line. +pub fn fmtIlliquidSep(buf: []u8) []const u8 { + return fmtCashSep(buf); +} + +/// Format the illiquid total row. +pub fn fmtIlliquidTotal(buf: []u8, total: f64) []const u8 { + return fmtCashTotal(buf, total); +} + // ── Number formatters ──────────────────────────────────────── /// Format a dollar amount with commas and 2 decimals: $1,234.56 diff --git a/src/models/portfolio.zig b/src/models/portfolio.zig index c9fd3b5..933c2f4 100644 --- a/src/models/portfolio.zig +++ b/src/models/portfolio.zig @@ -7,6 +7,7 @@ pub const LotType = enum { option, // option contracts cd, // certificates of deposit cash, // cash/money market + illiquid, // illiquid assets (real estate, vehicles, etc.) watch, // watchlist item (no position, just track price) pub fn label(self: LotType) []const u8 { @@ -15,6 +16,7 @@ pub const LotType = enum { .option => "Option", .cd => "CD", .cash => "Cash", + .illiquid => "Illiquid", .watch => "Watch", }; } @@ -23,6 +25,7 @@ pub const LotType = enum { if (std.mem.eql(u8, s, "option")) return .option; if (std.mem.eql(u8, s, "cd")) return .cd; if (std.mem.eql(u8, s, "cash")) return .cash; + if (std.mem.eql(u8, s, "illiquid")) return .illiquid; if (std.mem.eql(u8, s, "watch")) return .watch; return .stock; } @@ -283,6 +286,15 @@ pub const Portfolio = struct { return total; } + /// Total illiquid asset value across all accounts. + pub fn totalIlliquid(self: Portfolio) f64 { + var total: f64 = 0; + for (self.lots) |lot| { + if (lot.lot_type == .illiquid) total += lot.shares; + } + return total; + } + /// Total CD face value across all accounts. pub fn totalCdFaceValue(self: Portfolio) f64 { var total: f64 = 0; diff --git a/src/providers/openfigi.zig b/src/providers/openfigi.zig new file mode 100644 index 0000000..2a187b6 --- /dev/null +++ b/src/providers/openfigi.zig @@ -0,0 +1,271 @@ +//! OpenFIGI API provider -- maps CUSIPs (and other identifiers) to ticker symbols. +//! API docs: https://www.openfigi.com/api/documentation +//! +//! Free tier: 25 requests/minute with API key (each request can contain up to 100 jobs). +//! No API key required for basic use (lower rate limits). +//! +//! Note: OpenFIGI does NOT cover most mutual funds (especially institutional/401(k) share +//! classes). For those, use the `ticker::` alias field in the portfolio SRF file. + +const std = @import("std"); + +const api_url = "https://api.openfigi.com/v3/mapping"; + +/// Result of a CUSIP lookup. +pub const FigiResult = struct { + ticker: ?[]const u8, + name: ?[]const u8, + security_type: ?[]const u8, + /// Whether the API returned a valid response (even if no match was found) + found: bool, +}; + +/// Look up a single CUSIP via OpenFIGI. Caller must free returned strings. +/// Returns null ticker if not found. Uses system curl for the POST request. +pub fn lookupCusip( + allocator: std.mem.Allocator, + cusip: []const u8, + api_key: ?[]const u8, +) !FigiResult { + const results = try lookupCusips(allocator, &.{cusip}, api_key); + defer { + for (results) |r| { + if (r.ticker) |t| allocator.free(t); + if (r.name) |n| allocator.free(n); + if (r.security_type) |s| allocator.free(s); + } + allocator.free(results); + } + if (results.len == 0) return .{ .ticker = null, .name = null, .security_type = null, .found = false }; + + // Copy results since we're freeing the batch + const r = results[0]; + return .{ + .ticker = if (r.ticker) |t| try allocator.dupe(u8, t) else null, + .name = if (r.name) |n| try allocator.dupe(u8, n) else null, + .security_type = if (r.security_type) |s| try allocator.dupe(u8, s) else null, + .found = r.found, + }; +} + +/// Look up multiple CUSIPs in a single batch request. Caller owns all returned slices. +/// Results array is parallel to the input cusips array (same length, same order). +pub fn lookupCusips( + allocator: std.mem.Allocator, + cusips: []const []const u8, + api_key: ?[]const u8, +) ![]FigiResult { + if (cusips.len == 0) return try allocator.alloc(FigiResult, 0); + + // Build JSON request body: [{"idType":"ID_CUSIP","idValue":"..."},...] + var body_buf: std.ArrayList(u8) = .empty; + defer body_buf.deinit(allocator); + + try body_buf.append(allocator, '['); + for (cusips, 0..) |cusip, i| { + if (i > 0) try body_buf.append(allocator, ','); + try body_buf.appendSlice(allocator, "{\"idType\":\"ID_CUSIP\",\"idValue\":\""); + try body_buf.appendSlice(allocator, cusip); + try body_buf.appendSlice(allocator, "\"}"); + } + try body_buf.append(allocator, ']'); + + const body_str = body_buf.items; + + // Build curl command + var argv_buf: [12][]const u8 = undefined; + var argc: usize = 0; + argv_buf[argc] = "curl"; + argc += 1; + argv_buf[argc] = "-sS"; + argc += 1; + argv_buf[argc] = "-f"; + argc += 1; + argv_buf[argc] = "--max-time"; + argc += 1; + argv_buf[argc] = "30"; + argc += 1; + argv_buf[argc] = "-X"; + argc += 1; + argv_buf[argc] = "POST"; + argc += 1; + argv_buf[argc] = "-H"; + argc += 1; + argv_buf[argc] = "Content-Type: application/json"; + argc += 1; + if (api_key) |key| { + // Build header string: "X-OPENFIGI-APIKEY: " + const hdr = try std.fmt.allocPrint(allocator, "X-OPENFIGI-APIKEY: {s}", .{key}); + defer allocator.free(hdr); + argv_buf[argc] = "-H"; + argc += 1; + argv_buf[argc] = hdr; + argc += 1; + argv_buf[argc] = "-d"; + argc += 1; + + // Need to add body and URL — but we've used all slots for the -H key header. + // Restructure: use a dynamic list. + var argv_list: std.ArrayList([]const u8) = .empty; + defer argv_list.deinit(allocator); + try argv_list.appendSlice(allocator, &.{ + "curl", "-sS", "--max-time", "30", + "-X", "POST", + "-H", "Content-Type: application/json", + "-H", hdr, + "-d", body_str, + api_url, + }); + + const result = std.process.Child.run(.{ + .allocator = allocator, + .argv = argv_list.items, + .max_output_bytes = 1 * 1024 * 1024, + }) catch return error.RequestFailed; + defer allocator.free(result.stderr); + + const success = switch (result.term) { + .Exited => |code| code == 0, + else => false, + }; + if (!success) { + allocator.free(result.stdout); + return error.RequestFailed; + } + return parseResponse(allocator, result.stdout, cusips.len); + } else { + // No API key + var argv_list: std.ArrayList([]const u8) = .empty; + defer argv_list.deinit(allocator); + try argv_list.appendSlice(allocator, &.{ + "curl", "-sS", "--max-time", "30", + "-X", "POST", + "-H", "Content-Type: application/json", + "-d", body_str, + api_url, + }); + + const result = std.process.Child.run(.{ + .allocator = allocator, + .argv = argv_list.items, + .max_output_bytes = 1 * 1024 * 1024, + }) catch return error.RequestFailed; + defer allocator.free(result.stderr); + + const success = switch (result.term) { + .Exited => |code| code == 0, + else => false, + }; + if (!success) { + allocator.free(result.stdout); + return error.RequestFailed; + } + return parseResponse(allocator, result.stdout, cusips.len); + } +} + +fn parseResponse(allocator: std.mem.Allocator, body: []const u8, expected_count: usize) ![]FigiResult { + defer allocator.free(body); + + const parsed = std.json.parseFromSlice(std.json.Value, allocator, body, .{}) catch + return error.ParseError; + defer parsed.deinit(); + + const root = parsed.value; + const arr = switch (root) { + .array => |a| a, + else => return error.ParseError, + }; + + var results = try allocator.alloc(FigiResult, expected_count); + for (results) |*r| r.* = .{ .ticker = null, .name = null, .security_type = null, .found = false }; + + for (arr.items, 0..) |item, i| { + if (i >= expected_count) break; + + const obj = switch (item) { + .object => |o| o, + else => continue, + }; + + // Check for error/warning (no match) + if (obj.get("warning") != null or obj.get("error") != null) { + results[i].found = true; // API responded, just no match + continue; + } + + // Get the data array + const data = switch (obj.get("data") orelse continue) { + .array => |a| a, + else => continue, + }; + + if (data.items.len == 0) { + results[i].found = true; + continue; + } + + // Use the first result, preferring exchCode "US" if available + var best: ?std.json.ObjectMap = null; + for (data.items) |entry| { + const entry_obj = switch (entry) { + .object => |o| o, + else => continue, + }; + if (best == null) best = entry_obj; + // Prefer US exchange + if (entry_obj.get("exchCode")) |ec| { + if (ec == .string and std.mem.eql(u8, ec.string, "US")) { + best = entry_obj; + break; + } + } + } + + if (best) |b| { + results[i].found = true; + if (b.get("ticker")) |t| { + if (t == .string and t.string.len > 0) { + results[i].ticker = try allocator.dupe(u8, t.string); + } + } + if (b.get("name")) |n| { + if (n == .string and n.string.len > 0) { + results[i].name = try allocator.dupe(u8, n.string); + } + } + if (b.get("securityType")) |st| { + if (st == .string and st.string.len > 0) { + results[i].security_type = try allocator.dupe(u8, st.string); + } + } + } + } + + return results; +} + +/// Check if a string looks like a CUSIP (9 alphanumeric characters). +/// CUSIPs have 6 alphanumeric issuer chars + 2 issue chars + 1 check digit. +/// This is a heuristic — it won't catch all CUSIPs and may have false positives. +pub fn isCusipLike(s: []const u8) bool { + if (s.len != 9) return false; + // Must contain at least one digit (all-alpha would be a ticker) + var has_digit = false; + for (s) |c| { + if (!std.ascii.isAlphanumeric(c)) return false; + if (std.ascii.isDigit(c)) has_digit = true; + } + return has_digit; +} + +test "isCusipLike" { + try std.testing.expect(isCusipLike("02315N600")); // Vanguard Target 2035 + try std.testing.expect(isCusipLike("02315N709")); // Vanguard Target 2040 + try std.testing.expect(isCusipLike("459200101")); // IBM + try std.testing.expect(isCusipLike("06051XJ45")); // CD CUSIP + try std.testing.expect(!isCusipLike("AAPL")); // Too short + try std.testing.expect(!isCusipLike("ABCDEFGHI")); // No digits + try std.testing.expect(isCusipLike("NON40OR52")); // Looks cusip-like (has digits, 9 chars) + try std.testing.expect(!isCusipLike("12345")); // Too short +} diff --git a/src/tui/main.zig b/src/tui/main.zig index 32ee930..4d6f217 100644 --- a/src/tui/main.zig +++ b/src/tui/main.zig @@ -165,7 +165,7 @@ const PortfolioRow = struct { drip_date_first: ?zfin.Date = null, drip_date_last: ?zfin.Date = null, - const Kind = enum { position, lot, watchlist, section_header, option_row, cd_row, cash_row, cash_total, drip_summary }; + const Kind = enum { position, lot, watchlist, section_header, option_row, cd_row, cash_row, cash_total, illiquid_row, illiquid_total, drip_summary }; }; /// Styled line for rendering @@ -224,6 +224,7 @@ const App = struct { cursor: usize = 0, // selected row in portfolio view expanded: [64]bool = [_]bool{false} ** 64, // which positions are expanded cash_expanded: bool = false, // whether cash section is expanded to show per-account + illiquid_expanded: bool = false, // whether illiquid section is expanded to show per-asset portfolio_rows: std.ArrayList(PortfolioRow) = .empty, portfolio_header_lines: usize = 0, // number of styled lines before data rows portfolio_line_to_row: [256]usize = [_]usize{0} ** 256, // maps styled line index -> portfolio_rows index @@ -712,11 +713,15 @@ const App = struct { self.rebuildPortfolioRows(); } }, - .lot, .option_row, .cd_row, .cash_row, .section_header, .drip_summary => {}, + .lot, .option_row, .cd_row, .cash_row, .illiquid_row, .section_header, .drip_summary => {}, .cash_total => { self.cash_expanded = !self.cash_expanded; self.rebuildPortfolioRows(); }, + .illiquid_total => { + self.illiquid_expanded = !self.illiquid_expanded; + self.rebuildPortfolioRows(); + }, .watchlist => { self.setActiveSymbol(row.symbol); self.active_tab = .quote; @@ -1400,6 +1405,31 @@ const App = struct { } } } + + // Illiquid assets section (similar to cash: total row, expandable) + if (pf.hasType(.illiquid)) { + self.portfolio_rows.append(self.allocator, .{ + .kind = .section_header, + .symbol = "Illiquid Assets", + }) catch {}; + // Total illiquid row + self.portfolio_rows.append(self.allocator, .{ + .kind = .illiquid_total, + .symbol = "ILLIQUID", + }) catch {}; + // Per-asset rows (expanded when illiquid_total is toggled) + if (self.illiquid_expanded) { + for (pf.lots) |lot| { + if (lot.lot_type == .illiquid) { + self.portfolio_rows.append(self.allocator, .{ + .kind = .illiquid_row, + .symbol = lot.symbol, + .lot = lot, + }) catch continue; + } + } + } + } } } @@ -1644,6 +1674,7 @@ const App = struct { self.freePortfolioSummary(); self.expanded = [_]bool{false} ** 64; self.cash_expanded = false; + self.illiquid_expanded = false; self.cursor = 0; self.scroll_offset = 0; self.portfolio_rows.clearRetainingCapacity(); @@ -1956,6 +1987,22 @@ const App = struct { const asof_text = try std.fmt.allocPrint(arena, " (as of close on {s})", .{d.format(&asof_buf)}); try lines.append(arena, .{ .text = asof_text, .style = th.mutedStyle() }); } + + // Net Worth line (only if portfolio has illiquid assets) + if (self.portfolio) |pf| { + if (pf.hasType(.illiquid)) { + const illiquid_total = pf.totalIlliquid(); + const net_worth = s.total_value + illiquid_total; + var nw_buf: [24]u8 = undefined; + var il_buf: [24]u8 = undefined; + const nw_text = try std.fmt.allocPrint(arena, " Net Worth: {s} (Liquid: {s} Illiquid: {s})", .{ + fmt.fmtMoney(&nw_buf, net_worth), + val_str, + fmt.fmtMoney(&il_buf, illiquid_total), + }); + try lines.append(arena, .{ .text = nw_text, .style = th.headerStyle() }); + } + } } else if (self.portfolio != null) { try lines.append(arena, .{ .text = " No cached prices. Run 'zfin perf ' for each holding.", .style = th.mutedStyle() }); } else { @@ -2232,6 +2279,28 @@ const App = struct { try lines.append(arena, .{ .text = text, .style = row_style5 }); } }, + .illiquid_total => { + if (self.portfolio) |pf| { + const total_illiquid = pf.totalIlliquid(); + var illiquid_buf: [24]u8 = undefined; + const arrow4: []const u8 = if (self.illiquid_expanded) "v " else "> "; + const text = try std.fmt.allocPrint(arena, " {s}Total Illiquid {s:>14}", .{ + arrow4, + fmt.fmtMoney(&illiquid_buf, total_illiquid), + }); + const row_style6 = if (is_cursor) th.selectStyle() else th.contentStyle(); + try lines.append(arena, .{ .text = text, .style = row_style6 }); + } + }, + .illiquid_row => { + if (row.lot) |lot| { + var illiquid_row_buf: [160]u8 = undefined; + const row_text = fmt.fmtIlliquidRow(&illiquid_row_buf, row.symbol, lot.shares, lot.note); + const text = try std.fmt.allocPrint(arena, " {s}", .{row_text}); + const row_style7 = if (is_cursor) th.selectStyle() else th.mutedStyle(); + try lines.append(arena, .{ .text = text, .style = row_style7 }); + } + }, .drip_summary => { const label_str: []const u8 = if (row.drip_is_lt) "LT" else "ST"; var drip_avg_buf: [24]u8 = undefined;