initial commit — cassandra v0.1
Containerised macro-strategy dashboard: 4-panel web UI (indicators, portfolio, flash news, AI strategic log), MariaDB store, hourly ingestion jobs, OpenRouter-backed AI analysis. Ports the four prototype scripts in the parent dir (market_pulse, flash_news, trading212, strategic_log) into async services backed by a persistent DB and served via FastAPI + Jinja2 + HTMX. APScheduler runs as a separate compose service for crash-safety and easier restarts. Portfolio composition + position names come live from Trading 212; news per-ticker headlines reuse those names. Tone (NOVICE/INTERMEDIATE/ PRO) and analysis style (DRY/SPECULATIVE) are env-configurable and stored on each log row so historical entries show what produced them. Default model is deepseek/deepseek-v4-flash (overridable via env). Light/dark theme toggle, sans-serif for prose surfaces, monospace for data. Bearer-token auth, OpenRouter monthly cost cap, RSS feeds auto- disabled on consecutive failures. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
a10409c02b
61 changed files with 4890 additions and 0 deletions
90
app/jobs/portfolio_job.py
Normal file
90
app/jobs/portfolio_job.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
"""Hourly Trading 212 snapshot. One Portfolio row per portfolio name
|
||||
(currently just 'pie'); one PortfolioSnapshot per run; N Position rows."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.config import get_settings
|
||||
from app.db import utcnow
|
||||
from app.jobs._helpers import job_lifecycle, log
|
||||
from app.models import Portfolio, PortfolioSnapshot, Position
|
||||
from app.services.trading212 import Trading212
|
||||
|
||||
|
||||
PORTFOLIO_NAME = "pie" # only one for now; multi-portfolio extension is schema-ready
|
||||
|
||||
|
||||
async def run() -> None:
|
||||
async with job_lifecycle("portfolio_job") as (session, jr):
|
||||
if jr.status == "skipped":
|
||||
return
|
||||
s = get_settings()
|
||||
if not (s.API_KEY and s.SECRET_KEY):
|
||||
log.warning("portfolio_job.skipped_no_creds")
|
||||
jr.status = "skipped"
|
||||
return
|
||||
|
||||
t212 = Trading212()
|
||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||
summary = await t212.summary(client)
|
||||
positions = await t212.positions(client)
|
||||
# The instruments call is heavy (~5 MB / 17k rows) but it's our
|
||||
# only path to a human-readable name per ticker. Once per hour is
|
||||
# fine; later we could cache to disk.
|
||||
try:
|
||||
instruments = await t212.instruments(client)
|
||||
name_by_ticker = {
|
||||
i["ticker"]: i.get("name") or i.get("shortName") or i["ticker"]
|
||||
for i in (instruments or [])
|
||||
}
|
||||
except Exception:
|
||||
name_by_ticker = {}
|
||||
|
||||
portfolio = (
|
||||
await session.execute(
|
||||
select(Portfolio).where(Portfolio.name == PORTFOLIO_NAME)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if portfolio is None:
|
||||
portfolio = Portfolio(
|
||||
name=PORTFOLIO_NAME, source="trading212",
|
||||
currency=summary.get("currency", "GBP"),
|
||||
)
|
||||
session.add(portfolio)
|
||||
await session.flush() # need id for FK
|
||||
|
||||
cash = (summary.get("cash") or {})
|
||||
investments = (summary.get("investments") or {})
|
||||
snap = PortfolioSnapshot(
|
||||
portfolio_id=portfolio.id,
|
||||
snapshot_at=utcnow(),
|
||||
total_value=summary.get("totalValue"),
|
||||
cash=cash.get("availableToTrade"),
|
||||
invested=investments.get("currentValue"),
|
||||
raw_json=summary,
|
||||
)
|
||||
session.add(snap)
|
||||
await session.flush()
|
||||
|
||||
for p in positions or []:
|
||||
tkr = p.get("ticker", "")
|
||||
session.add(Position(
|
||||
snapshot_id=snap.id,
|
||||
ticker=tkr,
|
||||
name=name_by_ticker.get(tkr),
|
||||
quantity=p.get("quantity"),
|
||||
average_price=p.get("averagePrice"),
|
||||
current_price=p.get("currentPrice"),
|
||||
ppl=p.get("ppl"),
|
||||
))
|
||||
|
||||
await session.commit()
|
||||
jr.items_written = len(positions or []) + 1
|
||||
log.info("portfolio_job.done", positions=len(positions or []))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run())
|
||||
Loading…
Add table
Add a link
Reference in a new issue