Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

Add Tesouro Direto asset pricing (#340)

Romulo Gattoc6bc5a67a849
diff --git a/.env.example b/.env.example
index 1381d47d..4b498b5e 100644
--- a/.env.example
+++ b/.env.example
@@ -36,6 +36,10 @@ ENABLE_BANKING_OAUTH_REDIRECT_URI=http://localhost:${FRONTEND_PORT:-3000}/oauth/
# OIDC_ADMIN_ROLES=securo-admins
# OIDC_WORKSPACE_ROLE_MAP={"securo-owners":"owner","securo-editors":"editor","securo-viewers":"viewer"}
+# Brazilian Treasury bond lookup (Tesouro Direto). On by default; uncomment to
+# disable (e.g. to avoid the dependency on the Brazilian government CSV endpoint).
+# TESOURO_DIRETO_ENABLED=false
+
# Host ports (optional) — change these to avoid conflicts with other services
## Default values are set in docker-compose.yml and docker-compose.prod.yml.
## Example:
diff --git a/backend/.env.example b/backend/.env.example
index 9443aa6b..e5e24a43 100644
--- a/backend/.env.example
+++ b/backend/.env.example
@@ -6,6 +6,9 @@ PLUGGY_CLIENT_SECRET=
PLUGGY_OAUTH_REDIRECT_URI=http://localhost:5173/oauth/callback
FRONTEND_URL=http://localhost:5173
REDIS_URL=redis://localhost:6379/0
+# Brazilian Treasury bond prices. On by default; set to false to disable
+# (e.g. to avoid the dependency on the Brazilian government CSV endpoint).
+TESOURO_DIRETO_ENABLED=true
# Optional OIDC login (Authentik, Pocket ID, or standard OIDC providers)
OIDC_ENABLED=false
diff --git a/backend/app/api/assets.py b/backend/app/api/assets.py
index 34747c69..13645508 100644
--- a/backend/app/api/assets.py
+++ b/backend/app/api/assets.py
@@ -51,7 +51,9 @@ router = APIRouter(prefix="/api/assets", tags=["assets"])
@router.get("/market/search", response_model=list[MarketSymbolMatch])
async def market_search(
q: str = Query(..., min_length=1, max_length=64, description="Ticker or company name"),
- limit: int = Query(15, ge=1, le=30),
+ # Upper bound is generous so the Tesouro Direto dropdown can list every
+ # open bond (~60 and growing); ticker autocomplete still requests ~15.
+ limit: int = Query(15, ge=1, le=300),
_: User = Depends(current_active_user),
) -> list[MarketSymbolMatch]:
"""Autocomplete ticker symbols for the Add-Asset form.
@@ -95,6 +97,8 @@ async def market_quote(
return quote
+
+
@router.post("/{asset_id}/refresh-price", response_model=AssetRead)
async def refresh_asset_price(
asset_id: uuid.UUID,
@@ -122,7 +126,7 @@ async def refresh_asset_price(
if asset.valuation_method != "market_price":
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
- detail="Only market-priced assets can be refreshed via this endpoint",
+ detail="Only externally priced assets can be refreshed via this endpoint",
)
try:
@@ -135,7 +139,7 @@ async def refresh_asset_price(
if not ok:
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
- detail=f"Could not refresh quote for {asset.ticker}",
+ detail="Could not refresh price for this asset",
)
await session.commit()
diff --git a/backend/app/api/info.py b/backend/app/api/info.py
index da4a2c18..7e25b92c 100644
--- a/backend/app/api/info.py
+++ b/backend/app/api/info.py
@@ -19,5 +19,6 @@ async def get_app_info():
return {
"features": {
"agents": _flag("AGENTS_ENABLED"),
+ "tesouro_direto": _flag("TESOURO_DIRETO_ENABLED"),
},
}
diff --git a/backend/app/core/config.py b/backend/app/core/config.py
index f0f0b600..37056dd1 100644
--- a/backend/app/core/config.py
+++ b/backend/app/core/config.py
@@ -107,6 +107,14 @@ class Settings(BaseSettings):
# is what Google's favicon service caps at before upscaling.
logo_size: int = 128
+ # Brazilian Treasury bond prices (official Tesouro Transparente CSV).
+ # On by default since most users are Brazilian; the official CSV is only
+ # fetched when someone actually searches a bond, and the UI pre-warm is
+ # gated to Brazilian users, so non-Brazilian installs pay ~zero cost.
+ # Set TESOURO_DIRETO_ENABLED=false to fully disable (e.g. to avoid the
+ # external dependency on the Brazilian government endpoint).
+ tesouro_direto_enabled: bool = True
+
model_config = SettingsConfigDict(env_file=".env")
diff --git a/backend/app/main.py b/backend/app/main.py
index ab6f34a7..91581248 100644
--- a/backend/app/main.py
+++ b/backend/app/main.py
@@ -1,3 +1,4 @@
+import asyncio
import logging
import os
from contextlib import asynccontextmanager
@@ -48,6 +49,37 @@ logger = logging.getLogger(__name__)
settings = get_settings()
+async def _warm_tesouro_cache() -> None:
+ """Pre-load the Tesouro Direto price cache so the first bond search is
+ instant instead of waiting on the cold ~25s CSV download.
+
+ Gated to instances that actually serve Brazilian users (a workspace with
+ BRL as its default currency) so a non-Brazilian deployment never calls the
+ Brazilian government endpoint just because the feature ships on by default.
+ """
+ try:
+ if not get_settings().tesouro_direto_enabled:
+ return
+ from sqlalchemy import select
+
+ from app.core.database import async_session_maker
+ from app.models.workspace import Workspace
+
+ async with async_session_maker() as session:
+ has_brl = await session.scalar(
+ select(Workspace.id).where(Workspace.default_currency == "BRL").limit(1)
+ )
+ if not has_brl:
+ return
+
+ from app.providers.tesouro_direto import get_tesouro_direto_provider
+
+ await get_tesouro_direto_provider().get_available_bonds()
+ logger.info("Startup: warmed Tesouro Direto price cache")
+ except Exception:
+ logger.exception("Startup: Tesouro Direto cache warm failed")
+
+
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: dispatch sync for all stale bank connections
@@ -58,6 +90,9 @@ async def lifespan(app: FastAPI):
logger.info("Startup: dispatched sync_all_connections task to Celery")
except Exception:
logger.exception("Startup: failed to dispatch sync task")
+ # Background pre-warm of the Tesouro cache (non-blocking; gated to BRL
+ # instances inside the helper). Kept on app.state so it isn't GC'd.
+ app.state.tesouro_warm_task = asyncio.create_task(_warm_tesouro_cache())
yield
# Shutdown
await close_redis()
diff --git a/backend/app/providers/market_price.py b/backend/app/providers/market_price.py
index 1049e8cb..1e1b7212 100644
--- a/backend/app/providers/market_price.py
+++ b/backend/app/providers/market_price.py
@@ -435,6 +435,127 @@ class _UnreachableException(Exception):
"""Placeholder used when yfinance doesn't expose its rate-limit exception."""
+
+class CompositeMarketPriceProvider(MarketPriceProvider):
+ """Market-price provider that routes provider-specific symbols.
+
+ Yahoo remains the default for ordinary tickers. Tesouro Direto bonds are
+ exposed as compact TD:* symbols so they reuse the same market_price asset
+ ledger, creation, refresh, and UI flows.
+ """
+
+ name = "composite"
+
+ def __init__(self, default_provider: Optional[MarketPriceProvider] = None) -> None:
+ self.default_provider = default_provider or YFinanceProvider()
+
+ async def search(self, query: str, limit: int = 20) -> list[MarketSymbolMatch]:
+ q = (query or "").strip()
+ # A Tesouro search wants bonds, not Yahoo's "tesouro" text matches — so
+ # when we have bond results, return them alone. Fall through to Yahoo
+ # only when Tesouro is off or has nothing (e.g. flag disabled).
+ if _tesouro_enabled() and _looks_like_tesouro_query(q):
+ tesouro = await self._search_tesouro(q, limit=limit)
+ if tesouro:
+ return tesouro[:limit]
+ return await self.default_provider.search(q, limit=limit)
+
+ async def get_quote(self, symbol: str) -> Optional[MarketSymbolQuote]:
+ if _is_tesouro_symbol(symbol):
+ return await self._tesouro_quote(symbol)
+ return await self.default_provider.get_quote(symbol)
+
+ async def get_latest_prices(self, symbols: list[str]) -> dict[str, Optional[Decimal]]:
+ out: dict[str, Optional[Decimal]] = {}
+ regular: list[str] = []
+ tesouro: list[str] = []
+ for symbol in symbols:
+ if _is_tesouro_symbol(symbol):
+ tesouro.append(symbol)
+ else:
+ regular.append(symbol)
+ if regular:
+ out.update(await self.default_provider.get_latest_prices(regular))
+ if tesouro:
+ out.update(await self._tesouro_latest_prices(tesouro))
+ return out
+
+ async def _search_tesouro(self, query: str, limit: int) -> list[MarketSymbolMatch]:
+ from app.providers.tesouro_direto import (
+ _normalize,
+ get_tesouro_direto_provider,
+ tesouro_symbol_for,
+ )
+
+ quotes = await get_tesouro_direto_provider().get_available_bonds()
+ # Filter by name so the shared search box behaves like ticker search:
+ # "selic" → Selic bonds, "ipca 2035" → that maturity. "tesouro"/"direto"
+ # are dropped as filler so a bare "tesouro" lists everything.
+ tokens = [t for t in _normalize(query).split() if t and t not in ("tesouro", "direto")]
+ if tokens:
+ quotes = [
+ q
+ for q in quotes
+ if all(tok in _normalize(f"{q.title_type} {q.maturity_date.year}") for tok in tokens)
+ ]
+ return [
+ MarketSymbolMatch(
+ symbol=tesouro_symbol_for(q.title_type, q.maturity_date),
+ name=f"{q.title_type} · {q.maturity_date.strftime('%d/%m/%Y')}",
+ exchange="Tesouro Direto",
+ quote_type="BOND",
+ )
+ for q in quotes[:limit]
+ ]
+
+ async def _tesouro_quote(self, symbol: str) -> Optional[MarketSymbolQuote]:
+ from app.providers.tesouro_direto import get_tesouro_direto_provider, tesouro_symbol_for
+
+ quote = await get_tesouro_direto_provider().get_quote_by_symbol(symbol)
+ if quote is None:
+ return None
+ return MarketSymbolQuote(
+ symbol=tesouro_symbol_for(quote.title_type, quote.maturity_date),
+ name=f"{quote.title_type} {quote.maturity_date.year}",
+ exchange="Tesouro Direto",
+ currency="BRL",
+ price=float(quote.pu_base),
+ quote_type="BOND",
+ )
+
+ async def _tesouro_latest_prices(self, symbols: list[str]) -> dict[str, Optional[Decimal]]:
+ from app.providers.tesouro_direto import get_tesouro_direto_provider
+
+ quotes = await get_tesouro_direto_provider().get_quotes_by_symbol(symbols)
+ return {symbol.upper(): (q.pu_base if q else None) for symbol, q in quotes.items()}
+
+def _tesouro_enabled() -> bool:
+ try:
+ from app.core.config import get_settings
+
+ return bool(get_settings().tesouro_direto_enabled)
+ except Exception:
+ return False
+
+# Bond-name keywords that route the shared search to Tesouro Direto. A bare
+# "td" prefix is deliberately excluded — it collides with real tickers like TD
+# (Toronto-Dominion Bank). Stock/crypto queries skip the bond path entirely so
+# they never wait on the Treasury CSV.
+_TESOURO_KEYWORDS = ("tesouro", "selic", "ipca", "prefixado", "igpm", "educa", "renda")
+
+
+def _looks_like_tesouro_query(query: str) -> bool:
+ normalized = query.strip().casefold()
+ return any(keyword in normalized for keyword in _TESOURO_KEYWORDS)
+
+def _is_tesouro_symbol(symbol: str | None) -> bool:
+ try:
+ from app.providers.tesouro_direto import is_tesouro_symbol
+
+ return is_tesouro_symbol(symbol)
+ except Exception:
+ return False
+
# Module-level singleton — cheap to construct, stateless beyond the yfinance
# import, and consumers need a stable instance for dependency overrides.
_default_provider: Optional[MarketPriceProvider] = None
@@ -444,7 +565,7 @@ def get_market_price_provider() -> MarketPriceProvider:
"""Return the configured market-price provider (yfinance by default)."""
global _default_provider
if _default_provider is None:
- _default_provider = YFinanceProvider()
+ _default_provider = CompositeMarketPriceProvider()
return _default_provider
diff --git a/backend/app/providers/tesouro_direto.py b/backend/app/providers/tesouro_direto.py
new file mode 100644
index 00000000..adf65c4b
--- /dev/null
+++ b/backend/app/providers/tesouro_direto.py
@@ -0,0 +1,266 @@
+from __future__ import annotations
+
+import asyncio
+import csv
+import hashlib
+import io
+import re
+import time
+import unicodedata
+from dataclasses import dataclass
+from datetime import date, datetime
+from decimal import Decimal, InvalidOperation
+from typing import Optional
+import requests
+
+TESOURO_DIRETO_CSV_URL = (
+ "https://www.tesourotransparente.gov.br/ckan/dataset/"
+ "df56aa42-484a-4a59-8184-7676580c81e3/resource/"
+ "796d2059-14e9-44e3-80c9-2d9e30b405c1/download/precotaxatesourodireto.csv"
+)
+
+TESOURO_SYMBOL_PREFIX = "TD"
+
+# The official CSV carries years of daily history for every bond, so a fresh
+# download + parse takes ~25s. Prices only change once per business day, so we
+# cache the latest-per-bond snapshot in-process for several hours — long enough
+# that as-you-type search stays warm through a session. Shared across provider
+# instances since get_tesouro_direto_provider() returns a fresh one each call.
+_CACHE_TTL_SECONDS = 6 * 60 * 60
+_latest_cache: dict = {"ts": 0.0, "quotes": None}
+
+def tesouro_symbol_for(title_type: str, maturity_date: date) -> str:
+ """Return a compact market-price symbol for a Tesouro Direto bond.
+
+ Asset.ticker is limited to 32 chars, so we cannot store the full bond
+ title. The short hash is derived from the normalized title and resolved
+ against the official CSV together with maturity date.
+ """
+ digest = hashlib.sha1(_normalize(title_type).encode("utf-8")).hexdigest()[:8].upper()
+ return f"{TESOURO_SYMBOL_PREFIX}:{digest}:{maturity_date.isoformat()}"
+
+def is_tesouro_symbol(symbol: str | None) -> bool:
+ return bool(symbol and symbol.upper().startswith(f"{TESOURO_SYMBOL_PREFIX}:"))
+
+def parse_tesouro_symbol(symbol: str) -> tuple[str, date]:
+ parts = (symbol or "").strip().upper().split(":", 2)
+ if len(parts) != 3 or parts[0] != TESOURO_SYMBOL_PREFIX or not parts[1]:
+ raise ValueError(f"invalid Tesouro Direto symbol: {symbol}")
+ return parts[1], _parse_date(parts[2])
+
+
+
+@dataclass(frozen=True)
+class TesouroDiretoQuote:
+ title_type: str
+ maturity_date: date
+ price_date: date
+ pu_base: Decimal
+
+
+def _normalize(value: str) -> str:
+ text = unicodedata.normalize("NFKD", value or "")
+ text = "".join(ch for ch in text if not unicodedata.combining(ch))
+ return re.sub(r"\s+", " ", text).strip().casefold()
+
+
+def _header_key(value: str) -> str:
+ return re.sub(r"[^a-z0-9]+", "", _normalize(value))
+
+
+def parse_brl_decimal(value: str) -> Decimal:
+ raw = str(value or "").strip()
+ if not raw:
+ raise ValueError("empty decimal")
+ # Treasury CSV usually uses pt-BR format (15.130,10). Plain decimal is
+ # accepted for tests and future endpoint changes.
+ if "," in raw:
+ raw = raw.replace(".", "").replace(",", ".")
+ try:
+ return Decimal(raw)
+ except InvalidOperation as exc:
+ raise ValueError(f"invalid decimal: {value}") from exc
+
+
+def _parse_date(value: str) -> date:
+ raw = str(value or "").strip()
+ for fmt in ("%d/%m/%Y", "%Y-%m-%d"):
+ try:
+ return datetime.strptime(raw, fmt).date()
+ except ValueError:
+ pass
+ raise ValueError(f"invalid date: {value}")
+
+
+class TesouroDiretoProvider:
+ """Official Tesouro Transparente CSV provider.
+
+ Tesouro Direto bonds do not have stable tickers, so prices are keyed by the
+ pair (title type, maturity date). The official CSV includes historical rows;
+ we keep the latest Data Base for each key.
+ """
+
+ def __init__(self, *, csv_text: str | None = None, url: str = TESOURO_DIRETO_CSV_URL) -> None:
+ self._csv_text = csv_text
+ self.url = url
+
+ async def get_latest_price(self, title_type: str, maturity_date: date) -> Optional[TesouroDiretoQuote]:
+ target = (_normalize(title_type), maturity_date)
+ for quote in await self._latest_quotes():
+ if (_normalize(quote.title_type), quote.maturity_date) == target:
+ return quote
+ return None
+
+ async def get_latest_prices(
+ self, keys: list[tuple[str, date]]
+ ) -> dict[tuple[str, date], Optional[TesouroDiretoQuote]]:
+ wanted = {(_normalize(t), m): (t, m) for t, m in keys}
+ out: dict[tuple[str, date], Optional[TesouroDiretoQuote]] = {key: None for key in keys}
+ for quote in await self._latest_quotes():
+ original = wanted.get((_normalize(quote.title_type), quote.maturity_date))
+ if original is not None:
+ out[original] = quote
+ return out
+
+ async def get_available_bonds(self) -> list[TesouroDiretoQuote]:
+ # Only bonds still open for investment — the CSV also lists long-matured
+ # series that nobody should be adding as a current holding.
+ today = date.today()
+ bonds = [q for q in await self._latest_quotes() if q.maturity_date >= today]
+ return sorted(bonds, key=lambda q: (q.title_type, q.maturity_date))
+
+ async def get_quote_by_symbol(self, symbol: str) -> Optional[TesouroDiretoQuote]:
+ try:
+ title_hash, maturity_date = parse_tesouro_symbol(symbol)
+ except ValueError:
+ return None
+ for quote in await self._latest_quotes():
+ if quote.maturity_date != maturity_date:
+ continue
+ if tesouro_symbol_for(quote.title_type, quote.maturity_date).split(":")[1] == title_hash:
+ return quote
+ return None
+
+ async def get_quotes_by_symbol(self, symbols: list[str]) -> dict[str, Optional[TesouroDiretoQuote]]:
+ return {symbol.upper(): await self.get_quote_by_symbol(symbol) for symbol in symbols}
+
+ async def _latest_quotes(self) -> list[TesouroDiretoQuote]:
+ """Latest (most recent Data Base) quote per (title, maturity).
+
+ Cached in-process for ``_CACHE_TTL_SECONDS`` to avoid re-downloading and
+ re-parsing the full historical CSV on every quote/search/refresh.
+ """
+ if self._csv_text is not None:
+ return self._build_latest(self._csv_text)
+ now = time.monotonic()
+ cached = _latest_cache["quotes"]
+ if cached is not None and (now - _latest_cache["ts"]) < _CACHE_TTL_SECONDS:
+ return cached
+ csv_text = await asyncio.to_thread(self._download_csv)
+ quotes = self._build_latest(csv_text)
+ _latest_cache["quotes"] = quotes
+ _latest_cache["ts"] = now
+ return quotes
+
+ def _build_latest(self, csv_text: str) -> list[TesouroDiretoQuote]:
+ latest: dict[tuple[str, date], TesouroDiretoQuote] = {}
+ for quote in self._iter_quotes(csv_text):
+ key = (_normalize(quote.title_type), quote.maturity_date)
+ if key not in latest or quote.price_date > latest[key].price_date:
+ latest[key] = quote
+ return list(latest.values())
+
+ def find_price(
+ self,
+ title_type: str,
+ maturity_date: date,
+ *,
+ csv_text: str | None = None,
+ ) -> Optional[TesouroDiretoQuote]:
+ target = (_normalize(title_type), maturity_date)
+ latest: Optional[TesouroDiretoQuote] = None
+ for quote in self._iter_quotes(csv_text or self._csv_text or ""):
+ if (_normalize(quote.title_type), quote.maturity_date) != target:
+ continue
+ if latest is None or quote.price_date > latest.price_date:
+ latest = quote
+ return latest
+
+ def find_price_by_symbol(
+ self,
+ symbol: str,
+ *,
+ csv_text: str | None = None,
+ ) -> Optional[TesouroDiretoQuote]:
+ try:
+ title_hash, maturity_date = parse_tesouro_symbol(symbol)
+ except ValueError:
+ return None
+ latest: Optional[TesouroDiretoQuote] = None
+ for quote in self._iter_quotes(csv_text or self._csv_text or ""):
+ if quote.maturity_date != maturity_date:
+ continue
+ if tesouro_symbol_for(quote.title_type, quote.maturity_date).split(":")[1] != title_hash:
+ continue
+ if latest is None or quote.price_date > latest.price_date:
+ latest = quote
+ return latest
+
+ def list_latest_quotes(self, *, csv_text: str | None = None) -> list[TesouroDiretoQuote]:
+ latest: dict[tuple[str, date], TesouroDiretoQuote] = {}
+ for quote in self._iter_quotes(csv_text or self._csv_text or ""):
+ key = (_normalize(quote.title_type), quote.maturity_date)
+ if key not in latest or quote.price_date > latest[key].price_date:
+ latest[key] = quote
+ return sorted(latest.values(), key=lambda q: (q.title_type, q.maturity_date))
+
+ def _download_csv(self) -> str:
+ response = requests.get(
+ self.url,
+ headers={"User-Agent": "Securo TesouroDiretoProvider"},
+ timeout=30,
+ )
+ response.raise_for_status()
+ response.encoding = response.encoding or "latin1"
+ return response.text
+
+ def _iter_quotes(self, csv_text: str):
+ if not csv_text.strip():
+ return
+ sample = csv_text[:2048]
+ dialect = csv.Sniffer().sniff(sample, delimiters=";,\t")
+ reader = csv.DictReader(io.StringIO(csv_text), dialect=dialect)
+ if not reader.fieldnames:
+ return
+ header_map = {_header_key(name): name for name in reader.fieldnames}
+ title_col = header_map.get("tipotitulo")
+ maturity_col = header_map.get("datavencimento")
+ price_date_col = header_map.get("database")
+ pu_col = (
+ header_map.get("pubasem\u00e3ha")
+ or header_map.get("pubasemanh")
+ or header_map.get("pubase")
+ )
+ # _header_key removes accents, so the real key is pubasemanh[a]. Keep a
+ # fallback scan to avoid coupling to exact spelling.
+ if not pu_col:
+ for key, original in header_map.items():
+ if key.startswith("pubase"):
+ pu_col = original
+ break
+ if not (title_col and maturity_col and price_date_col and pu_col):
+ return
+ for row in reader:
+ try:
+ yield TesouroDiretoQuote(
+ title_type=(row.get(title_col) or "").strip(),
+ maturity_date=_parse_date(row.get(maturity_col) or ""),
+ price_date=_parse_date(row.get(price_date_col) or ""),
+ pu_base=parse_brl_decimal(row.get(pu_col) or ""),
+ )
+ except ValueError:
+ continue
+
+
+def get_tesouro_direto_provider() -> TesouroDiretoProvider:
+ return TesouroDiretoProvider()
diff --git a/backend/app/schemas/asset.py b/backend/app/schemas/asset.py
index 6af9dcae..d182853f 100644
--- a/backend/app/schemas/asset.py
+++ b/backend/app/schemas/asset.py
@@ -28,6 +28,7 @@ class AssetCreate(BaseModel):
# fetches the live quote on create and seeds the first AssetValue.
ticker: Optional[str] = None
ticker_exchange: Optional[str] = None
+ maturity_date: Optional[date] = None
# Per-unit price for the opening buy of a market-priced holding (preço
# médio model, consistent with the transaction ledger). When omitted, the
# service seeds the buy at the live quote ("bought at market now").
diff --git a/backend/app/services/asset_service.py b/backend/app/services/asset_service.py
index c6991dda..f898c88c 100644
--- a/backend/app/services/asset_service.py
+++ b/backend/app/services/asset_service.py
@@ -8,11 +8,11 @@ from fastapi import HTTPException, status
from sqlalchemy import select, func, desc
from sqlalchemy.ext.asyncio import AsyncSession
-from app.core.config import get_settings
from app.models.asset import Asset
from app.models.asset_transaction import AssetTransaction
from app.models.asset_value import AssetValue
from app.models.user import User
+from app.core.config import get_settings
from app.providers.market_price import (
MarketPriceProvider,
MarketPriceRateLimitedError,
@@ -462,6 +462,7 @@ async def create_asset(
growth_rate=data.growth_rate,
growth_frequency=data.growth_frequency,
growth_start_date=data.growth_start_date,
+ maturity_date=data.maturity_date,
is_archived=data.is_archived,
position=data.position,
group_id=data.group_id,
@@ -470,7 +471,11 @@ async def create_asset(
last_price=Decimal(str(quote.price)) if quote else None,
last_price_at=datetime.now(timezone.utc) if quote else None,
logo_url=quote.logo_url if quote else None,
- source="yfinance" if data.valuation_method == "market_price" else "manual",
+ source=(
+ "tesouro_direto"
+ if quote and quote.exchange == "Tesouro Direto"
+ else ("yfinance" if data.valuation_method == "market_price" else "manual")
+ ),
)
session.add(asset)
await session.flush()
@@ -489,6 +494,7 @@ async def create_asset(
)
)
+
# Create initial value if provided
if data.current_value is not None:
value = AssetValue(
@@ -971,7 +977,7 @@ async def get_asset_values_at(
async def _apply_price_to_asset(
- session: AsyncSession, asset: Asset, new_price: Decimal
+ session: AsyncSession, asset: Asset, new_price: Decimal, *, value_date: date | None = None
) -> None:
"""Update the cached price and upsert today's AssetValue.
@@ -986,7 +992,7 @@ async def _apply_price_to_asset(
if not asset.units or asset.units <= 0:
return
- today = date.today()
+ today = value_date or date.today()
new_amount = new_price * Decimal(str(asset.units))
existing = await session.execute(
select(AssetValue)
diff --git a/backend/tests/test_tesouro_direto_assets.py b/backend/tests/test_tesouro_direto_assets.py
new file mode 100644
index 00000000..c233778f
--- /dev/null
+++ b/backend/tests/test_tesouro_direto_assets.py
@@ -0,0 +1,119 @@
+from __future__ import annotations
+
+from decimal import Decimal
+from typing import Optional
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from app.models.asset import Asset
+from app.models.asset_value import AssetValue
+from app.models.user import User
+from app.providers.market_price import MarketPriceProvider
+from app.schemas.asset import AssetCreate, MarketSymbolMatch, MarketSymbolQuote
+from app.services import asset_service
+from app.services.asset_service import refresh_all_market_prices
+
+
+class FakeMarketProvider(MarketPriceProvider):
+ name = "fake"
+
+ def __init__(self, quotes: dict[str, MarketSymbolQuote], batch_prices: dict[str, Optional[Decimal]] | None = None):
+ self.quotes = {k.upper(): v for k, v in quotes.items()}
+ self.batch_prices = {k.upper(): v for k, v in (batch_prices or {}).items()}
+ self.bulk_calls = 0
+
+ async def search(self, query: str, limit: int = 20) -> list[MarketSymbolMatch]:
+ return []
+
+ async def get_quote(self, symbol: str) -> Optional[MarketSymbolQuote]:
+ return self.quotes.get(symbol.upper())
+
+ async def get_latest_prices(self, symbols: list[str]) -> dict[str, Optional[Decimal]]:
+ self.bulk_calls += 1
+ if self.batch_prices:
+ return {symbol.upper(): self.batch_prices.get(symbol.upper()) for symbol in symbols}
+ return {
+ symbol.upper(): (Decimal(str(self.quotes[symbol.upper()].price)) if symbol.upper() in self.quotes else None)
+ for symbol in symbols
+ }
+
+
+def _quote(symbol: str = "TD:ABC12345:2029-03-01", price: float = 15130.10) -> MarketSymbolQuote:
+ return MarketSymbolQuote(
+ symbol=symbol,
+ name="Tesouro Selic 2029",
+ exchange="Tesouro Direto",
+ currency="BRL",
+ price=price,
+ quote_type="BOND",
+ )
+
+
+async def test_create_tesouro_market_price_asset_reuses_market_ledger(
+ session: AsyncSession, test_user: User, test_workspace
+):
+ symbol = "TD:ABC12345:2029-03-01"
+ provider = FakeMarketProvider({symbol: _quote(symbol)})
+ data = AssetCreate(
+ name="Tesouro Selic 2029",
+ type="investment",
+ currency="USD",
+ valuation_method="market_price",
+ ticker=symbol,
+ units=Decimal("2.5"),
+ )
+
+ created = await asset_service.create_asset(
+ session, test_workspace.id, test_user.id, data, market_provider=provider
+ )
+
+ assert created.valuation_method == "market_price"
+ assert created.source == "tesouro_direto"
+ assert created.currency == "BRL"
+ assert created.ticker == symbol
+ assert created.last_price == pytest.approx(15130.10)
+ assert created.current_value == pytest.approx(37825.25)
+ assert created.value_count == 1
+ assert created.average_price == pytest.approx(15130.10)
+ assert created.transaction_count == 1
+
+ db_asset = await session.get(Asset, created.id)
+ assert db_asset.external_metadata is None
+
+
+async def test_refresh_all_market_prices_updates_tesouro_symbol_through_market_provider(
+ session: AsyncSession, test_user: User, test_workspace
+):
+ symbol = "TD:ABC12345:2029-03-01"
+ created = await asset_service.create_asset(
+ session,
+ test_workspace.id,
+ test_user.id,
+ AssetCreate(
+ name="Tesouro Selic 2029",
+ type="investment",
+ valuation_method="market_price",
+ ticker=symbol,
+ units=Decimal("2"),
+ ),
+ market_provider=FakeMarketProvider({symbol: _quote(symbol, 15130.10)}),
+ )
+
+ refresh_provider = FakeMarketProvider({}, {symbol: Decimal("15200.00")})
+ summary = await refresh_all_market_prices(session, market_provider=refresh_provider)
+
+ assert summary == {"refreshed": 1, "skipped": 0, "rate_limited": 0}
+ assert refresh_provider.bulk_calls == 1
+ db_asset = await session.get(Asset, created.id)
+ assert db_asset.last_price == Decimal("15200.000000")
+ values = (
+ await session.execute(
+ select(AssetValue).where(AssetValue.asset_id == db_asset.id).order_by(AssetValue.date)
+ )
+ ).scalars().all()
+ assert values[-1].price == Decimal("15200.000000")
+ assert values[-1].amount == Decimal("30400.00")
diff --git a/backend/tests/test_tesouro_direto_provider.py b/backend/tests/test_tesouro_direto_provider.py
new file mode 100644
index 00000000..04dc06c5
--- /dev/null
+++ b/backend/tests/test_tesouro_direto_provider.py
@@ -0,0 +1,61 @@
+from __future__ import annotations
+
+from datetime import date
+from decimal import Decimal
+
+from app.providers.tesouro_direto import TesouroDiretoProvider, parse_brl_decimal, tesouro_symbol_for
+
+
+CSV = """Tipo Titulo;Data Vencimento;Data Base;Taxa Compra Manha;Taxa Venda Manha;PU Compra Manha;PU Venda Manha;PU Base Manha
+Tesouro Selic;01/03/2029;17/06/2026;0,01;0,02;15120,00;15129,00;15.123,45
+Tesouro Selic;01/03/2029;18/06/2026;0,01;0,02;15125,00;15135,00;15.130,10
+Tesouro IPCA+;15/05/2035;18/06/2026;6,10;6,20;4320,00;4322,00;4.321,99
+"""
+
+
+def test_parse_brl_decimal_accepts_brazilian_and_plain_formats():
+ assert parse_brl_decimal("15.130,10") == Decimal("15130.10")
+ assert parse_brl_decimal("15130,10") == Decimal("15130.10")
+ assert parse_brl_decimal("15130.10") == Decimal("15130.10")
+
+
+def test_find_price_matches_title_type_and_maturity_with_latest_price_date():
+ provider = TesouroDiretoProvider(csv_text=CSV)
+
+ quote = provider.find_price(" tesouro selic ", date(2029, 3, 1))
+
+ assert quote is not None
+ assert quote.title_type == "Tesouro Selic"
+ assert quote.maturity_date == date(2029, 3, 1)
+ assert quote.price_date == date(2026, 6, 18)
+ assert quote.pu_base == Decimal("15130.10")
+
+
+def test_find_price_returns_none_for_missing_bond():
+ provider = TesouroDiretoProvider(csv_text=CSV)
+
+ assert provider.find_price("Tesouro Prefixado", date(2031, 1, 1)) is None
+
+
+def test_list_latest_quotes_deduplicates_by_title_and_maturity():
+ provider = TesouroDiretoProvider(csv_text=CSV)
+
+ quotes = provider.list_latest_quotes()
+
+ assert [(q.title_type, q.maturity_date, q.price_date, q.pu_base) for q in quotes] == [
+ ("Tesouro IPCA+", date(2035, 5, 15), date(2026, 6, 18), Decimal("4321.99")),
+ ("Tesouro Selic", date(2029, 3, 1), date(2026, 6, 18), Decimal("15130.10")),
+ ]
+
+
+def test_find_price_by_symbol_resolves_compact_market_price_symbol():
+ provider = TesouroDiretoProvider(csv_text=CSV)
+ symbol = tesouro_symbol_for("Tesouro Selic", date(2029, 3, 1))
+
+ quote = provider.find_price_by_symbol(symbol)
+
+ assert symbol.startswith("TD:")
+ assert len(symbol) <= 32
+ assert quote is not None
+ assert quote.title_type == "Tesouro Selic"
+ assert quote.pu_base == Decimal("15130.10")
diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml
index 581a5a6e..6f9909a1 100644
--- a/docker-compose.prod.yml
+++ b/docker-compose.prod.yml
@@ -37,6 +37,7 @@ x-backend-env: &backend-env
OIDC_WORKSPACE_ROLE_MAP: ${OIDC_WORKSPACE_ROLE_MAP:-}
OPENEXCHANGERATES_APP_ID: ${OPENEXCHANGERATES_APP_ID:-}
FX_SYNC_MODE: ${FX_SYNC_MODE:-on_demand}
+ TESOURO_DIRETO_ENABLED: ${TESOURO_DIRETO_ENABLED:-true}
REDIS_URL: redis://redis:6379/0
STORAGE_LOCAL_PATH: /app/data/attachments
AGENTS_KNOWLEDGE_STORAGE_PATH: /app/data/agent_knowledge
diff --git a/docker-compose.yml b/docker-compose.yml
index f2e85f45..398fb36c 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -38,6 +38,7 @@ x-backend-env: &backend-env
REDIS_URL: redis://redis:6379/0
OPENEXCHANGERATES_APP_ID: ${OPENEXCHANGERATES_APP_ID:-}
FX_SYNC_MODE: ${FX_SYNC_MODE:-on_demand}
+ TESOURO_DIRETO_ENABLED: ${TESOURO_DIRETO_ENABLED:-true}
STORAGE_LOCAL_PATH: /app/data/attachments
AGENTS_KNOWLEDGE_STORAGE_PATH: /app/data/agent_knowledge
# Agents/LLM feature — opt-in. Off by default so existing users pay
diff --git a/docs/tesouro-direto-asset-dialog.png b/docs/tesouro-direto-asset-dialog.png
new file mode 100644
index 00000000..241dfe41
Binary files /dev/null and b/docs/tesouro-direto-asset-dialog.png differ
diff --git a/docs/tesouro-direto-holding.png b/docs/tesouro-direto-holding.png
new file mode 100644
index 00000000..b5a20f2f
Binary files /dev/null and b/docs/tesouro-direto-holding.png differ
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index a6785a84..f22a778b 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -1242,7 +1242,7 @@ export const search = {
// App-level feature flags (whether optional modules like agents are mounted)
export interface AppInfo {
- features: { agents: boolean }
+ features: { agents: boolean; tesouro_direto?: boolean }
}
export const info = {
diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsx
index a2184476..5edf10b5 100644
--- a/frontend/src/pages/assets.tsx
+++ b/frontend/src/pages/assets.tsx
@@ -570,6 +570,7 @@ export default function AssetsPage() {
setTickerSearchLoading(false)
}
+
function openCreate() {
setEditingAsset(null)
setFormName('')
@@ -661,6 +662,7 @@ export default function AssetsPage() {
}
}
+
if (!editingAsset && formCurrentValue) {
payload.current_value = parseFloat(formCurrentValue)
}
@@ -738,7 +740,7 @@ export default function AssetsPage() {
<AssetIcon logoUrl={asset.logo_url} Icon={Icon} colorClass={config.color} bgClass={config.bg} size={16} tile="w-8 h-8" />
<div className="min-w-0">
<div className="flex items-center gap-1.5">
- <span className="font-semibold text-foreground truncate">{asset.ticker || asset.name}</span>
+ <span className="font-semibold text-foreground truncate">{asset.ticker && !asset.ticker.startsWith('TD:') ? asset.ticker : asset.name}</span>
{needsBuys && (
<Badge
variant="outline"
@@ -756,7 +758,7 @@ export default function AssetsPage() {
<Badge variant="outline" className="text-[9px] px-1 py-0 text-sky-600 border-sky-200">{t('assets.synced')}</Badge>
)}
</div>
- <span className="text-[11px] text-muted-foreground truncate block">{asset.ticker ? asset.name : t(`assets.type${asset.type.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase()).replace(/^./, c => c.toUpperCase())}`)}</span>
+ <span className="text-[11px] text-muted-foreground truncate block">{asset.ticker && !asset.ticker.startsWith('TD:') ? asset.name : (asset.ticker?.startsWith('TD:') ? 'Tesouro Direto' : t(`assets.type${asset.type.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase()).replace(/^./, c => c.toUpperCase())}`))}</span>
</div>
</div>
{/* Quant. */}
@@ -1195,7 +1197,7 @@ export default function AssetsPage() {
{/* Valuation Method — locked on edit */}
<div className="space-y-2">
<Label>{t('assets.valuationMethod')}</Label>
- <div className="grid grid-cols-3 gap-2">
+ <div className="grid gap-2 grid-cols-3">
{VALUATION_METHODS.map(m => (
<button
key={m}
@@ -1240,7 +1242,11 @@ export default function AssetsPage() {
/>
{tickerMatches.length > 0 && !editingAsset && (
<div className="absolute z-20 mt-1 w-full max-h-60 overflow-y-auto rounded-lg border border-border bg-popover shadow-lg">
- {tickerMatches.map(match => (
+ {tickerMatches.map(match => {
+ // Tesouro bonds carry an internal TD:* symbol — show
+ // the readable name instead of the hash for those.
+ const isBond = match.symbol.startsWith('TD:')
+ return (
<button
key={`${match.symbol}-${match.exchange ?? ''}`}
type="button"
@@ -1248,16 +1254,17 @@ export default function AssetsPage() {
className="flex flex-col w-full text-left px-3 py-2 hover:bg-muted transition-colors"
>
<div className="flex items-center justify-between gap-2">
- <span className="font-semibold text-sm">{match.symbol}</span>
+ <span className="font-semibold text-sm truncate">{isBond ? (match.name ?? match.symbol) : match.symbol}</span>
{match.exchange && (
- <span className="text-xs text-muted-foreground">{match.exchange}</span>
+ <span className="text-xs text-muted-foreground shrink-0">{match.exchange}</span>
)}
</div>
- {match.name && (
+ {match.name && !isBond && (
<span className="text-xs text-muted-foreground truncate">{match.name}</span>
)}
</button>
- ))}
+ )
+ })}
</div>
)}
{tickerSearchLoading && (
@@ -1272,8 +1279,8 @@ export default function AssetsPage() {
<div className="rounded-lg border border-border bg-card p-3 text-sm">
<div className="flex items-center justify-between">
<div className="flex flex-col min-w-0">
- <span className="font-semibold">{selectedQuote.symbol}</span>
- {selectedQuote.name && (
+ <span className="font-semibold truncate">{selectedQuote.symbol.startsWith('TD:') ? (selectedQuote.name ?? selectedQuote.symbol) : selectedQuote.symbol}</span>
+ {selectedQuote.name && !selectedQuote.symbol.startsWith('TD:') && (
<span className="text-xs text-muted-foreground truncate">{selectedQuote.name}</span>
)}
{/* Staleness hint — only meaningful when editing an
@@ -1373,6 +1380,7 @@ export default function AssetsPage() {
</div>
)}
+
{/* Growth Rule Settings */}
{formMethod === 'growth_rule' && (
<div className="space-y-3 p-3.5 rounded-xl border border-primary/20 bg-primary/5">