Skip to content
Proyek

Tentang

Portfolio optimization toolkit on skfolio: market-data pipeline, optimizer-computed candidate portfolios, paper trading. Source-available (PolyForm Noncommercial). Research software, not investment advice.

Bisnis dan keuangan

Cara angka ini diukur

Pertumbuhan bintang terukur

dalam 24 jam
0
dalam 7 hari
0
dalam 15 hari
0
dalam 1 bulan
Mengumpulkan riwayat

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

Optimizer

Portfolio optimization in one uv workspace: a composable skfolio / scikit-learn optimization library, a scheduled market-data ingestion daemon that keeps PostgreSQL fresh, a shared database layer, and an agentic bridge (fund) where the optimizer computes candidate portfolios, you choose, and orders are paper tickets only — four packages, one shared virtual environment.

Python License Lint: Ruff Types: mypy | pyright


Notices

  • Not investment advice. Optimizer is research and education software. Its output (weights, candidate portfolios, scenarios, simulated orders) is the result of a model run under assumptions you set. It is not a personal recommendation, investment advice, portfolio management, or an offer to buy or sell any instrument, and nothing here is provided by a regulated firm. You alone decide whether to act on any output. Simulations and backtests do not predict future results; you can lose money.
  • No broker execution. The software places no orders with any broker. The fund component records simulated (paper) orders only.
  • Your data and LLM providers. The fund component sends the data you give it (holdings, constraints, your optimisation inputs, ESG preferences) to the LLM provider you configure (Ollama Cloud by default). Choose a provider whose terms you accept, or run a local model. Do not enter data you are not willing to share.
  • Third-party data. Market data comes from third parties (Yahoo Finance through the unofficial yfinance library, FRED). You, not the project, are the party to their terms; use is limited to personal, non-commercial research. The ingestion's news step also downloads the full text of the articles Yahoo's news feeds link to, from the news publishers' own sites, and each publisher's terms apply to what you fetch. portopt setup shows Yahoo's terms and asks you to acknowledge them (--accept-yahoo-terms in a non-interactive setup), and the ingestion daemon will not start without that acknowledgement. The project is not affiliated with, endorsed by or sponsored by Yahoo or any data provider. Do not redistribute data this software downloads.
  • Licence. Source-available under PolyForm Noncommercial 1.0.0 (not an OSI-approved licence). No warranty and no liability, to the extent the law allows. No hosted service, no fees, no individual support.

What's in here

optimizer is not a single program — it is a workspace of four cooperating Python packages that share one virtual environment but keep strict import boundaries:

You want to… Use Needs a database / Docker?
Build and tune portfolio-optimization pipelines in your own code the optimizer library (portopt-core) No — pure Python (numpy/pandas/skfolio)
Keep a PostgreSQL store of market / fundamental / macro data fresh on a schedule the portopt ingestion daemon Yes — PostgreSQL (via Docker)
Share models, repositories and migrations across packages the portopt-db layer Yes — it is the DB layer
Have the optimizer compute candidate portfolios that LLM agents prepare and describe, then choose one yourself (paper tickets only) the fund bridge (portopt-fund) Yes — PostgreSQL + a chat-model endpoint (DeepSeek on Ollama Cloud by default; any of ten providers via LLM_PROVIDER)

The library installs and runs with zero Docker/DB dependency. Docker + PostgreSQL are required only for the ingestion daemon and the fund bridge, which persist data. If all you want is optimization, install portopt-core and skip the rest.


Architecture

Four workspace members, one shared venv (uv sync resolves them together):

Directory Dist name Import name Role
packages/portopt-core/ (declared from the repo-root pyproject.toml) portopt-core optimizer Pure-Python optimization library (DB-agnostic, sklearn/skfolio-based)
ingestion/ portopt app Headless ingestion daemon (APScheduler in-process, no HTTP API) + the portopt CLI
packages/portopt-db/ portopt-db portopt_db Shared SQLAlchemy Base + models + repositories + DatabaseManager + the single Alembic tree
fund/ portopt-fund fund deepagents/langgraph bridge: the optimizer computes candidate portfolios; you choose; paper tickets only

Import boundaries (enforced by static import-scan tests)

Because there is one shared venv, install-time isolation does not exist — the boundaries below are guarded by source-scan hygiene tests, not by what's installed:

  • ingestion/ and portopt-db/ do not import optimizer.
  • portopt-db/ carries no sklearn/skfolio stack.
  • optimizer (portopt-core) does not import portopt_db — the library stays DB-free.
  • fund/ is the only member allowed to import both optimizer and portopt_db; deepagents/langgraph live only in fund/.

The ingestion daemon does ship scikit-learn — not for optimization, but because yfinance's price-repair path (repair=True) imports sklearn.cluster.DBSCAN. Without it ~22% of tickers return empty history and get dropped. It is a data-layer dependency there.

The optimization library

optimizer is a set of composable primitives, not a fixed end-to-end runner. There is no run_full_pipeline; opinionated, DB-connected orchestration lives in fund/. Every module follows the same convention: a frozen @dataclass config + a factory function + str, Enum types. Transformers are sklearn BaseEstimator + TransformerMixin and compose in sklearn.pipeline.Pipeline.

Module flow:

prices → preprocessing → pre_selection → moments → views
       → optimization → validation → tuning → rebalancing

Plus the cross-cutting modules factors/, synthetic/, scoring/, universe/, distance/, cluster/, uncertainty_set/, linear_model/, online/, fx/ and eight research tools that sit beside the pipeline and are not part of it — 26 submodules in total:

Tool What it gives you
risk/ Value-at-Risk (historical, parametric, Cornish-Fisher, Monte Carlo) with expected shortfall, and a Kupiec / Christoffersen backtest of the forecasts
simulation/ GBM and bootstrap price paths, path summaries, and a Kelly-criterion toolkit
strategies/ Moving-average and momentum signals and a no-look-ahead weight backtest with proportional costs
regression/ OLS with Newey-West errors, a CAPM report, a security market line and Fama-French-style factor regressions
plotting/ plotly figures: correlation heatmap, efficient frontier, returns and volatility, security market line
derivatives/ Black-Scholes prices, Greeks and implied volatility, a binomial tree, a delta-hedging simulator
credit/ The Merton structural credit model: asset value and volatility, distance to default, spread
statarb/ Cointegration tests, a Kalman hedge ratio, z-score signals and PCA residual models (research only: the fund is long-only and paper-only)

validation/ also holds the probabilistic and deflated Sharpe ratio.

The optimization module exposes fifteen optimizer builders (each a frozen config + build_* factory): Mean-Risk (build_mean_risk) and its regime-blended and robust variants, HRP, HERC, NCO, Schur-Complementary, Risk Budgeting, Max-Diversification, Distributionally-Robust CVaR, Stacking, Benchmark-Tracker, and three naive baselines (Equal-Weighted, Inverse-Volatility, Random). Full per-module inventories (covariance/mu estimators, factor definitions, investability screens, presets) live in each submodule's source and docstrings under packages/portopt-core/optimizer/.


Requirements

  • Python ≥ 3.12 (all four packages; the numpy 2.5 / pandas 3.1 pins make 3.12 a hard floor). CI runs 3.12 and 3.13.
  • uv — the workspace resolver / runner.
  • Docker + the docker compose v2 plugin — only for the ingestion daemon and the fund bridge (they need PostgreSQL 16). Not needed for the library.
  • Optional API key (ingestion): FRED_API_KEY. The universe is built from the yfinance Screener and needs no key.

Installation

1. The optimization library only (no Docker, no DB)

# From a clone, into the current environment (the repo root declares `portopt-core`):
pip install -e .                            # editable install of dist `portopt-core`

Then import optimizer works with nothing else running. The library has no sibling-package dependencies, so plain pip is safe here. The daemon and the fund bridge are not: they depend on portopt-db / portopt-core, which exist only as workspace paths that uv resolves (pip ignores [tool.uv.sources] and would look those unpublished names up on PyPI), so install those two through uv.

2. The full platform (ingestion daemon)

The daemon ships an install wizard. The one-line bootstrap needs git: it clones this repo, installs uv, installs the portopt CLI straight from it (the CLI and the daemon are never published to PyPI, and this repo uploads nothing there; release.yml builds only the portopt-core library), then launches the wizard:

# macOS / Linux
curl -LsSf https://raw.githubusercontent.com/SilvioBaratto/optimizer/development/install.sh | bash
# Windows (PowerShell)
powershell -c "irm https://raw.githubusercontent.com/SilvioBaratto/optimizer/development/install.ps1 | iex"

Both scripts pull from the development branch by default; set PORTOPT_REF (a branch, tag or commit SHA) or PORTOPT_REPO before running to install a different ref or fork.

portopt runs docker compose and renders its plaintext secrets inside a checkout of this repo (it owns docker-compose.yml), and installing the CLI into a uv tool venv does not provide one. The scripts therefore clone into ~/optimizer (%USERPROFILE%\optimizer on Windows; set PORTOPT_HOME to choose elsewhere, which install.sh also accepts as PORTOPT_DIR), reuse that directory if it already holds a checkout, and export OPTIMIZER_REPO for the wizard, which then remembers the location in ~/.portopt/config.toml. install.sh also launches the wizard from inside the checkout and, on a re-run, updates an existing git checkout to PORTOPT_REF; it refuses to touch a directory that is a checkout of some other repo. Without a checkout, portopt setup stops with "Cannot locate the optimizer checkout" before it persists anything.

Equivalently, by hand — clone first, then run the from-clone front door:

git clone https://github.com/SilvioBaratto/optimizer && cd optimizer
./setup.sh      # setup.cmd / setup.ps1 on Windows; interactive wizard (see below)

To install the CLI yourself, point it at an existing checkout:

uv tool install "git+https://github.com/SilvioBaratto/optimizer.git@development#subdirectory=ingestion"
OPTIMIZER_REPO=/path/to/optimizer portopt setup

portopt setup first shows Yahoo's terms and asks you to acknowledge them (declining stops it before any key or setting is stored; a --non-interactive setup needs --accept-yahoo-terms or PORTOPT_ACCEPT_YAHOO_TERMS=1, and a re-run does not ask again). It then verifies Docker + the compose plugin, validates your FRED and LLM provider keys live, encrypts them (Fernet + scrypt) to ~/.portopt/secrets.enc (mode 0600, passphrase never persisted), brings up PostgreSQL (docker compose up -d --wait db), and applies the Alembic migrations. It seeds no data. Re-run any time with portopt setup. The acknowledgement is kept in ~/.portopt/config.toml, and portopt start hands it to the scheduler as YAHOO_TERMS_ACKNOWLEDGED: the ingestion daemon refuses to start (exit status 2) without it. The LLM provider prompt says where your portfolio data goes: to the provider you choose.

Lifecycle:

portopt start     # decrypt secrets → render as compose secrets → docker compose up -d --wait (ingestion always, fund when an LLM backend is configured)
portopt stop      # stop the fund engine + delete its plaintext LLM keys (daemon + db keep running)
portopt stop --all  # stop the whole stack + delete every rendered plaintext secret
portopt status    # report Docker + service health (non-zero exit if anything is down)
portopt migrate   # back up the database (verified pg_dump), then apply pending migrations; asks first (--yes, --backup-dir, --no-backup)

portopt start brings up the fund engine only when an LLM backend is configured (a local Ollama base URL, or the selected provider's key plus an explicit model id; Ollama Cloud is the default provider). Without one it starts the data pipeline alone and prints one line saying so; run portopt setup --reconfigure to add a backend, then portopt start again.

While it builds the images and waits for the services to report healthy, portopt start shows its progress. On a terminal that is one redrawn line with a bar, the BuildKit step count, the elapsed time and the step running now, then a spinner with the services' states (db healthy, scheduler starting). In a pipe, a CI log, TERM=dumb or with NO_COLOR set it prints one plain line per build step and per change of the states instead. Each phase ends with the time it took, and a build or start that fails ends with the last 40 lines of Docker's output.

portopt start renders the encrypted secrets into git-ignored ./secrets/<name> files. portopt stop deletes the LLM keys only the fund mounts, but keeps the FRED key the ingestion daemon mounts: the daemon restarts on its own and needs it. Run portopt stop --all to stop the daemon too and leave no plaintext on disk. It removes only the files this version renders: if you upgraded, delete by hand any file an earlier version left in ./secrets/ that this version no longer creates (the keys of an integration it no longer has). Both refuse while a fund run, fund approve or fund-tui is running inside the fund container, because stopping it would kill that round mid-flight; add --force to stop anyway.

Upgrading. After you pull a new version, portopt start checks the database before it rebuilds or starts anything. If the new code brings migrations the database has not run yet and the database already holds data, the start stops and says which revision the database is at and how far behind it is, because some migrations delete data (see "Destructive migrations" in packages/portopt-db/README.md). Run portopt migrate. It lists the revisions and asks, refuses while a fund command started with docker compose exec (a round, an approval, the cockpit) runs in the fund container, stops the scheduler and fund, writes a pg_dump of the whole database to ~/.portopt/backups/optimizer_db-<YYYYmmdd>-<HHMMSS>-pre-migrate.dump, checks that the dump lists every table and reads back in full, and only then applies the migrations. A folder portopt creates for the dumps is private (mode 0700), and the default one is outside the checkout; --backup-dir or a backup_dir key in ~/.portopt/config.toml picks another, which keeps its own mode (with a warning when other users can read it), and old dumps are never deleted. If the dump, its check or the upgrade fails or is interrupted by Ctrl-C, SIGTERM or SIGHUP (a closed terminal or a dropped SSH session), the partial dump is removed and the scheduler and fund come back on their previous code while the database is still at its earlier revision. Under nohup an ignored SIGHUP stays ignored, so the migrate carries on; on Windows only SIGTERM is handled. A kill -9 or a power loss cannot be caught: it may leave the .partial file and the stopped daemons behind, and a kill -9 during the upgrade leaves Alembic running, which can still commit. Delete a leftover .partial, rerun portopt migrate (it says when nothing is left to do), then run portopt start again. portopt setup does not migrate such a database either. The scheduler and fund containers refuse it on their own as well (exit status 3), so a plain docker compose up stops there too (--wait fails at once, and docker compose logs scheduler shows the reason): Docker retries them with a growing delay (measured at 3, 7, 13, 25 and 52 seconds, then about once a minute), and they start once the database is at head. A fresh install is still migrated on its first start. For an unattended setup, PORTOPT_AUTO_MIGRATE=true (in the shell or the root .env) restores migrating on every start, without a backup. A database whose tables carry no Alembic stamp is never migrated, not even with that variable or portopt migrate --no-backup: nothing can tell which migrations already ran there. The refusal says how to go on: dump it yourself, record the revision its schema matches with Alembic's stamp command, then run portopt migrate, or restore a dump of a stamped database into a new database. Once the database is current, the start rebuilds the images when any one's sources changed ("Rebuilding images: the checkout changed since they were built."), each of them mostly from the build cache, and shows the build's progress as it goes; the first build after an upgrade can take several minutes.

First run. Setup seeds no data, so a fresh database has an empty instruments table. The first daily_pipeline (07:00, or run it straight away with docker compose exec scheduler python -m app.cli daily) therefore builds the universe first, a multi-minute yfinance Screener sweep, and then downloads prices and fundamentals for every instrument it found. Plan for many hours: about 8,700 tickers on 14 exchanges took roughly 14 hours on the first reported install. The scheduler logs a notice when it starts that download, later daily runs are incremental and short, and an interrupted bootstrap resumes on the next run because already-fresh data is skipped.

3. Developer setup (all four packages)

git clone https://github.com/SilvioBaratto/optimizer
cd optimizer
uv sync --all-packages --all-extras     # one venv with every member + every extra
export YAHOO_TERMS_ACKNOWLEDGED=true    # after reading Yahoo's terms; the scheduler needs it
docker compose --profile ingestion up -d # PostgreSQL (54320) + Adminer (18081) + scheduler (metrics 9000)
export DATABASE_URL=postgresql://postgres:postgres@localhost:54320/optimizer_db   # alembic reads only this variable, never .env
cd packages/portopt-db && alembic upgrade head   # apply migrations (single owner)

Usage

The optimization library

Feed linear returns (never log returns), keep temporal order, and compose pre-selection + an optimizer into one flat sklearn Pipeline:

from skfolio.preprocessing import prices_to_returns
from optimizer.optimization import MeanRiskConfig, build_mean_risk
from optimizer.pre_selection import PreSelectionConfig, build_portfolio_pipeline

# `prices`: a wide DataFrame — DatetimeIndex, one column per ticker.
X = prices_to_returns(prices)            # linear returns; runs OUTSIDE the pipeline

estimator = build_mean_risk(MeanRiskConfig())          # frozen config + factory
pipeline = build_portfolio_pipeline(                   # flattens pre-selection + optimizer
    estimator,
    pre_selection_config=PreSelectionConfig(),
)
pipeline.fit(X)
weights = pipeline[-1].weights_          # fitted skfolio optimizer exposes weights_

build_portfolio_pipeline flattens the steps so nested params are tunable via get_params() (e.g. "optimizer__l2_coef", "drop_correlated__threshold"). sector_mapping, expiration_dates and an outlier_protection_mask are optional keyword arguments (plain values, not queried from any database).

See each submodule's source and docstrings under packages/portopt-core/optimizer/ for every config, factory, preset and shape contract, and the skfolio/yfinance skills under .claude/skills/ for API-level guidance.

The ingestion daemon

Run the daemon (blocks; SIGTERM to stop) — normally via Docker Compose, but it can run in-process:

YAHOO_TERMS_ACKNOWLEDGED=true uv run --package portopt python -m app.worker

It refuses to start (exit status 2) until Yahoo's terms are acknowledged: set the variable only once you have read them, or let portopt setup record it for portopt start.

It schedules seven jobs with APScheduler in-process (a SQLAlchemyJobStore):

Job Cadence
daily_pipeline daily 07:00
midday_news daily 14:00
universe_build Saturday 02:00
weekly_refetch Saturday 03:00
weekly_market_wide Saturday 04:00
fred_monthly 1st of month 08:00
orphan_reaper on an interval

universe_build runs before weekly_refetch (a stale universe caps yfinance). daily_pipeline additionally builds the universe itself when instruments has no active rows (a fresh install), so prices do not wait for the first Saturday. The reaper is a pure heartbeat lease: it fails any active (pending/running) job whose last_heartbeat_at is NULL or older than the lease TTL — so run exactly one daemon per database.

Trigger the same work manually (same job-slot / heartbeat path):

docker compose exec scheduler python -m app.cli daily
docker compose exec scheduler python -m app.cli refetch-all
docker compose exec scheduler python -m app.cli yfinance --mode full --period 5y

Full CLI surface (portopt <command> or python -m app.cli <command>): daily, refetch-all, universe, yfinance, fx, fred, news, market-structure, calendars, market-summary, options, add-instrument (validates one Yahoo ticker, stores 5y of prices; --pin keeps it through the weekly universe build), daily-events (--backfill START:END rebuilds a date range), plus the lifecycle commands setup, start, stop, status, migrate.

The fund component (fund)

In the fund bridge the optimizer computes candidate portfolios, you choose, and orders are paper tickets only. LLM agents prepare the inputs and describe the results; every weight comes from optimizer, no agent chooses a candidate, and nothing is sent to a broker. Runs persist to PostgreSQL via portopt_db, and by default a round pauses at a human-in-the-loop gate before place_orders (each portfolio's settings set its gates). The chat model is chosen by LLM_PROVIDER from a ten-provider registry (ollama, openai, openrouter, anthropic, google, groq, nvidia, huggingface, aws, microsoft); set nothing and it stays on the verified default — DeepSeek on Ollama Cloud. Install a provider's SDK with the matching extra (uv sync --package portopt-fund --extra <provider>, or --extra all-llm; openrouter and microsoft use --extra openai, ollama needs none). CLI (fund <command>):

Command What it does
fund profile <portfolio_id> Record or refresh a portfolio's optimisation inputs (the answers go to the configured LLM provider)
fund mandate set <portfolio_id> / fund mandate show <portfolio_id> Write or display a portfolio's settings (capital, base currency, drift threshold, benchmark, eligible tickers via repeatable --eligible / --clear-eligible)
fund run <portfolio_id> --asof <date> Compute the candidate portfolios for a rebalance bar
fund approve <run_id> --candidate <id> / fund reject <run_id> Resolve a paused (human-in-the-loop) run; a rebalance gate is approved by naming one of its candidates, an inputs (profile) gate without --candidate
fund status [portfolio_id] Show the paused-run queue
fund report <run_id> Tabular audit of one run, with its candidates and the one you chose
fund nav-mark [--rebuild] Mark every active portfolio's NAV now (the daily step, run by hand)

Two more entry points ship with it:

  • fund-worker — the daemon: a weekly rebalance sweep, an interval drift monitor, a daily NAV mark and an orphan-job reaper (a heartbeat lease, like the ingestion reaper). The NAV mark waits for the day's ingestion price sweep to finish (up to FUND_NAV_MARK_WAIT_SECONDS, 4 h by default) so it values holdings at fresh closes.
  • fund-tui — a Textual oversight cockpit with eight views (Posture, Deliberation, Decisions, Constraints, Audit, Performance, Ask, Daily brief). Every action it takes (approve, reject, freeze, settings edits) lands in the audit log; approving a rebalance gate's candidate, a killswitch, a re-arm and every action typed in Ask ask for a confirm first. Ask answers questions about the fund in plain language by reading its data, docs and source (read-only), keeps a saved conversation per portfolio, and /clear deletes it. Press ? for the key map.

Choosing a candidate at the rebalance gate

A rebalance round computes four candidate portfolios over the same eligible universe, constraints and ESG exclusions: hrp (hierarchical risk parity), mean_risk (a mean-risk optimisation, labelled by its objective and risk measure), min_risk (minimum variance) and risk_parity (equal risk contribution). They are always listed in that id order, each with the same measures: annualised volatility, CVaR 95%, largest position, number of positions, turnover from current holdings and the constraint check. A candidate that fails or breaches a constraint is shown as infeasible with its breaches. The agents present and compare the candidates and never choose one; there is no default.

You choose at the place_orders gate, and paper orders are placed from the chosen candidate's weights only:

fund report <run_id>                           # the candidates, by id, with their measures
fund approve <run_id> --candidate min_risk     # approve that candidate
fund reject <run_id>                           # or place nothing

An approve without --candidate, or with an unknown or infeasible id, is refused and the gate stays open. In fund-tui the Decisions view lists the candidates in a table: select a feasible row (↵), press a, and accept the confirm that names the candidate, its weights and its measures. Ask does not approve a rebalance gate; it points to Decisions.

Restricting a portfolio to a set of instruments

A portfolio can be limited to a hand-picked list (an ETF-only book, say). The list is a hard allowlist: the fund tools only list, optimise, risk-check and order tickers on it, and an empty list (the default) leaves the portfolio unrestricted. To set one up:

# 1. put each ticker in the universe (validates it on Yahoo, stores 5y of prices and the
#    ETF fund data; --pin keeps it through the weekly universe build)
portopt add-instrument VWCE.DE --asset-class equity --pin
portopt add-instrument IBGS.L --asset-class fixed_income --fi-subclass government --pin

# 2. allowlist them in the portfolio's settings (repeatable; replaces the list)
fund mandate set <portfolio_id> --eligible VWCE.DE --eligible IBGS.L
fund mandate set <portfolio_id> --clear-eligible   # back to unrestricted

The same field is on the TUI Constraints view ("eligible tickers", comma-separated, saved with ctrl+s). A ticker with no priced instrument yet is accepted with a warning, so the settings can be saved before the instrument exists; until it has prices the ticker stays barred. Saving the settings also pins the tickers (source mandate:<portfolio_id>) so the weekly build cannot delist them.

Excluding sectors on ESG grounds is optional: leave the list empty and nothing is screened on ESG grounds. With ESG exclusions in your inputs, a fund is allowed by look-through: its latest sector snapshot must keep the excluded share within your stated tolerance (look_through_tolerance, strict 0 by default). Commodity funds and government or inflation-linked bond funds pass; any other fund whose snapshot is missing, older than FUND_ESG_SECTOR_WEIGHTS_MAX_AGE_DAYS (180) or incomplete is barred, so most corporate and aggregate bond ETFs are out under exclusions. The orchestrator's instruction names every barred ticker with its reason.


Dependencies

Each member declares its own; the single shared venv installs one version of each shared pin. Exact runtime pins:

portopt-core (library): numpy==2.5.5, pandas==3.1.1, scipy==1.18.1, scikit-learn==1.9.1, skfolio==1.14.6, jinja2==3.1.6, plotly==7.2.0. (arch is not declared — it reaches bootstrap uncertainty-set classes transitively via skfolio. Code importing it directly must guard with try/except ImportError or declare it.)

portopt-db (DB layer): SQLAlchemy==2.1.9, psycopg2-binary==2.9.13, alembic==1.20.1, pandas==3.1.1, pydantic==2.14.4. Owns the SQLAlchemy pin the whole workspace inherits.

portopt (ingestion): yfinance==1.8.0, scikit-learn==1.9.1 (declared, for DBSCAN price-repair), numpy==2.5.5, pandas==3.1.1, APScheduler==3.11.3, SQLAlchemy==2.1.9, exchange_calendars==4.13.3, httpx==0.28.1, beautifulsoup4==4.15.0, requests==2.34.2, prometheus-client==0.26.0, typer==0.29.3, questionary==2.1.1, rich==15.0.0, cryptography==50.0.2, pydantic==2.14.4, pydantic-settings==2.16.3, python-dotenv==1.2.4, plus portopt-db (workspace; psycopg2-binary/alembic arrive transitively).

portopt-fund (bridge): deepagents==0.7.42 (the only exact pin), langgraph, langgraph-checkpoint-postgres, psycopg[binary], psycopg-pool, langchain-ollama, APScheduler, typer, textual, pydantic, python-dotenv, plus portopt-core + portopt-db (workspace).


Development

uv drives everything. Run per-package with uv run --package <name> ….

# Library (portopt-core) — tests, lint, types
uv run --package portopt-core pytest tests/ -v
uv run --package portopt-core pytest -k "test_name"
uv run --package portopt-core ruff check packages/portopt-core/optimizer/ tests/
uv run --package portopt-core mypy packages/portopt-core/optimizer/
uv run pyright                              # all four packages, via [tool.pyright] include

# Ingestion daemon (portopt) — member suites run from the member directory
cd ingestion && uv run pytest tests/

# Shared DB layer (portopt-db)
cd packages/portopt-db && uv run pytest
cd packages/portopt-db && DATABASE_URL=postgresql://postgres:postgres@localhost:54320/optimizer_db alembic upgrade head   # head: 8ed6504aee6a

# Fund bridge (portopt-fund)
cd fund && uv run pytest tests/

--package only selects the dependency set; it does not change the working directory. Run from the repo root, pytest always collects the root testpaths = ["tests"] (the library suite), so a member suite must be started from its own directory, exactly as CI does.

The root Makefile (make lint | format | typecheck | test | all) runs the same ruff and mypy gates as the CI lint/typecheck jobs: ruff over the library, ingestion/, fund/ and packages/portopt-db/, and mypy over the library, portopt-db and ingestion. make test runs only the library suite.

CI (.github/workflows/ci.yml, push/PR to main and development, Ubuntu, Python 3.12 & 3.13, uv-driven) runs eleven jobs: lint (ruff check → ruff format --check on all four members → the complexity ratchet → shellcheck → pip-audit), typecheck (mypy), pyright (pinned 1.1.414), test (--cov=optimizer ≥ 90%), ingestion-test (--cov=app ≥ 80%), portopt-db-test (--cov=portopt_db ≥ 90%), fund-test (--cov=fund ≥ 80%), postgres-integration (the Alembic upgrade to head on a real PostgreSQL, alembic check, a downgrade/upgrade of the seven most recent revisions, then the integration-marked tests of portopt-db and fund), changelog (advisory, pull requests only: shipped code needs a CHANGELOG.md entry), docker-images (compose config + build of both images, then a boot of both stacks), and windows-install (install.ps1 on a Windows runner under Windows PowerShell 5.1 and PowerShell 7). release.yml builds portopt-core on v* tags, but only after checking that the tagged commit is on main, that the tag equals v<pyproject version>, and that the whole CI workflow passes on it. It uploads nothing to PyPI.


Repository layout

optimizer/
├── pyproject.toml                 # root = dist `portopt-core`; declares the uv workspace
├── packages/
│   ├── portopt-core/optimizer/    # the optimization library (import `optimizer`)
│   └── portopt-db/                # shared DB layer + the single Alembic tree
├── ingestion/                     # the `portopt` daemon + CLI (import `app`)
├── fund/                          # the `portopt-fund` bridge (import `fund`)
├── tests/                         # library test suite (mirrors optimizer/ + scheduler/)
├── scheduler/                     # shell wrappers over the CLI
├── install.sh / install.ps1       # one-line bootstrap (clone + uv + the setup wizard)
├── setup.sh / .cmd / .ps1         # the from-clone setup front door
└── docker-compose.yml             # db + adminer + scheduler + fund (every service profile-gated)

License

Source-available under PolyForm Noncommercial 1.0.0, not an OSI-approved licence. No warranty and no liability, to the extent the law allows (see Notices).

Jelajahi proyek terkait

Memuat…