RSJ Portfolio Portfolio Optimization

← All posts

2026-11-01

Automate Portfolio Optimization with Cached End-of-Day Prices

Automate Portfolio Optimization with Cached End-of-Day Prices
portfolio optimization modern portfolio theory portfolio correlation portfolio optimization hierachical risk parity minimize volatility minimize semivariance maximize sharpe

Automate Portfolio Optimization with Cached End-of-Day Prices

Rebalance day is a checklist, and the checklist is the problem.

Pull end-of-day prices. Check whether the correlation structure between your candidates has shifted. Re-run the optimizer. Copy the weights into a spreadsheet. Try to remember which risk model you used last quarter and whether the date range was the same. None of those steps is intellectually hard — that’s exactly why they’re dangerous. Each repetition is a chance to optimize against a stale price file, or against parameters that quietly drifted from the ones you documented.

This article is about collapsing that loop into something repeatable: prices cached locally, parameters fixed once, every run producing an allocation you can archive and compare. We’ll use RSJ Portfolio — a Windows desktop application that builds a statistically optimized stock portfolio from historic end-of-day prices — as the concrete example, because its workflow maps cleanly onto the three pieces that make automation practical: a persistent configuration, a local price cache, and file exports.

Why Automate Portfolio Optimization?

The manual loop has a fixed cost, and that cost is what stops most people from rebalancing as often as their model says they should. Re-downloading years of daily prices for a few dozen symbols takes time and network round-trips; doing it every month instead of every quarter multiplies that cost by three without changing the analysis you actually care about.

Caching end-of-day prices changes the economics. Once the history is local, a repeated run only needs the rows that arrived since the last run — not the full series again. The result is that frequent re-optimization becomes practical rather than theoretical, and frequent re-optimization is precisely what keeps correlation estimates current.

The goal of the automation isn’t “run more often for its own sake.” It’s a repeatable, statistically driven workflow in which the correlation between holdings is re-measured on schedule, weak correlation is rewarded in the allocation, and the resulting portfolio’s volatility stays closer to what the model predicted. RSJ Portfolio does the statistical work — it analyses return correlation between stocks and allocates capital to candidates that correlate weakly, using modern portfolio theory, with a selectable objective, risk estimator and set of weight constraints. Your job is to make that run boring.

One-Time Setup: Configure RSJ Portfolio for Automated Runs

Everything downstream depends on getting this hour right. An automated pipeline built on undocumented parameters just produces wrong answers faster.

1. Install and verify. Download the Windows installer from the downloads page and check the SHA-512 hash published there — it’s in a versioned filename, so a mismatch is a real signal, not a typo. The dedicated walkthrough is in How to Verify a SHA-512 Checksum Before Installing Portfolio Software. The license is a single-user license; pricing lists what it covers.

2. Configure the data layer. On the data and currencies page you’ll find the supported data providers, the price cleaning pipeline, and the local cache. Set, in this order:

  • Data provider — pick one and stay on it, so your history is consistent.
  • Local currency — the currency your weights and risk numbers should be expressed in.
  • Cache location — put it on a fast local disk, not a network share. This directory is the thing your scheduled runs will depend on.
  • Date range — long enough to estimate correlation and risk meaningfully, short enough to reflect the regime you’re investing in.
  • Cleaning settings — keep the defaults unless you have a documented reason not to; the pipeline is what flags suspect prices later.

3. Choose a starting objective and risk model. The features page enumerates the five allocation methods — minimum volatility, maximum Sharpe ratio, minimum semi variance, hierarchical risk parity, and conditional value at risk — plus eight risk models and multiple return models. If you don’t yet have a view, minimum volatility or minimum semi variance are the natural first choices for a correlation-driven, volatility-reducing mandate. (The mechanics of the semi-variance objective get their own treatment in How to Build a Minimum Semivariance Portfolio.)

4. Build the candidate list once. Search symbols and assemble the universe you’ll re-optimize over. This list is the input that stays stable across runs — you’ll review it deliberately, not rebuild it from memory each period.

5. Write the setup down. Not in your head. Next to the exports, as a manifest you can diff:

# run-manifest.yaml — your record of the configuration, not a product file
portfolio: core-mandate
data:
  provider: <the provider configured in RSJ Portfolio>
  local_currency: EUR
  cache_dir: D:\rsj\cache
  date_range_start: 2016-01-01
  cleaning: defaults
optimizer:
  objective: minimum_semivariance
  risk_model: <one of the eight available>
  weight_constraints: <the bounds you selected>
  candidates_source: saved-candidate-list
rebalance: quarterly
export_dir: D:\rsj\exports

The getting started guide covers the requirements, the settings worth touching before your first run, the recommended starting parameters, and how to read the output — worth thirty minutes before you commit to a configuration you’ll be repeating for years.

The Automated Pipeline: From Cached Prices to Optimized Weights

Here is the shape of the loop:

scheduled trigger
      │
      ├─► cache freshness check ──► stale? ──► refresh prices in the app, stop
      │
      ├─► run optimizer with the saved configuration
      │
      ├─► export allocation (CSV)
      │
      └─► archive + drift report vs. previous period

Three properties make this work. The configuration persists, so each run uses the same provider, cache, objective, risk model and constraints as the last one. The cache persists, so the price-loading step is a small incremental update rather than a full re-download. And the export persists — it’s a file on disk, which means anything downstream (a reporting sheet, a custodian instruction, a review memo) can consume it without you touching the optimizer.

To be clear about the boundary: RSJ Portfolio is a desktop application without a built-in scheduler or public API. The automation lives around it — a scheduled task runs your wrapper, the wrapper does the freshness check and the post-run bookkeeping, and the optimizer run itself is the same short, deliberate click-path you configured during setup. That’s still a large win: the fragile parts (remembering parameters, noticing stale data, archiving results) are the parts that are now scripted.

“Just script it” the hard way — write a downloader, align trading calendars, build a covariance matrix, hand-roll the optimizer — is a week of work followed by a year of maintenance, and it puts the statistical correctness on you. The managed workflow keeps the statistics in the product and leaves you the plumbing.

Registering the schedule on Windows:

$action  = New-ScheduledTaskAction -Execute "python.exe" `
           -Argument "D:\rsj\bin\rebalance.py" `
           -WorkingDirectory "D:\rsj\bin"
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 7:30am

Register-ScheduledTask -TaskName "RSJ Portfolio - weekly rebalance" `
  -Action $action -Trigger $trigger -RunLevel Limited

And the wrapper itself — freshness before, archive after:

#!/usr/bin/env python3
"""Wrapper around a quarterly RSJ Portfolio rebalance.

Owns the bookkeeping, not the statistics:
  1. is the local price cache fresh?
  2. is there an export to archive?
  3. archive it and report drift against the previous period
"""
import csv, shutil, sys
from datetime import date, timedelta
from pathlib import Path

CACHE   = Path(r"D:\rsj\cache")
EXPORTS = Path(r"D:\rsj\exports")
PERIOD  = f"{date.today():%Y}-Q{(date.today().month - 1) // 3 + 1}"
STALE_AFTER = timedelta(days=4)   # long weekend + one buffer day


def newest_cache_write() -> date:
    files = [p for p in CACHE.rglob("*") if p.is_file()]
    if not files:
        sys.exit("cache is empty - run RSJ Portfolio once to populate it")
    return date.fromtimestamp(max(p.stat().st_mtime for p in files))


def latest_export() -> Path:
    found = sorted(EXPORTS.glob("allocation-*.csv"))
    if not found:
        sys.exit("no allocation export found - run the optimizer in the app")
    return found[-1]


def load_weights(path: Path) -> dict[str, float]:
    # adjust these column names to match your export
    with path.open(newline="") as fh:
        return {r["symbol"]: float(r["weight"]) for r in csv.DictReader(fh)}


def main() -> None:
    written = newest_cache_write()
    if date.today() - written > STALE_AFTER:
        sys.exit(f"cache last touched {written} - refresh prices before optimizing")

    src = latest_export()
    dest_dir = EXPORTS / PERIOD
    dest_dir.mkdir(exist_ok=True)
    shutil.copy2(src, dest_dir / src.name)

    current = load_weights(src)
    previous = sorted(EXPORTS.glob("*/*.csv"))[:-1]
    if previous:
        prior = load_weights(previous[-1])
        moved = {s: round(current.get(s, 0) - prior.get(s, 0), 4)
                 for s in set(current) | set(prior)}
        print("drift vs previous period:",
              {s: d for s, d in moved.items() if abs(d) >= 0.005})

    print(f"archived {src.name} -> {dest_dir}")


if __name__ == "__main__":
    main()

The drift printout is the cheap part of the pipeline and the one you’ll actually read. A weight that moves ten points between periods is either a genuine correlation shift or a configuration mistake — and you want to know which before the orders go out.

Monitoring and Failure Handling

An automated pipeline that fails silently is worse than a manual one, because you’ll trust it. Watch for three failure classes.

Stale data. The most common failure is also the quietest: the cache is intact but nothing new arrived. The freshness check above catches the obvious case; the price cleaning pipeline catches the subtler one, surfacing suspect prices rather than letting them feed the risk estimator. If the cleaning output looks odd for a symbol, refresh that data before you optimize — a corrupted close price propagates into every correlation estimate that symbol touches.

Infeasible constraints. If the weight constraints you set cannot be satisfied, the run does not produce a usable allocation. Treat that as a hard failure, not a result to interpret. Loosen the constraint that conflicts with your objective, or widen the candidate list, then re-run — and note the change in the manifest so the next run isn’t a mystery.

Failed or skipped runs. Every wrapper should exit non-zero on failure and shout:

python.exe D:\rsj\bin\rebalance.py
if ($LASTEXITCODE -ne 0) {
  Invoke-RestMethod -Uri $env:ALERT_WEBHOOK -Method Post -Body (@{
    text = "RSJ Portfolio rebalance wrapper failed: exit $LASTEXITCODE"
  } | ConvertTo-Json)
}

Fallbacks. When the primary run fails, don’t improvise. Have a documented second choice: re-run with a different risk model, or switch the objective (minimum volatility → minimum semi variance, for instance) and compare the two allocations. If both produce wildly different weights, that disagreement is itself information about how unstable your candidate universe is.

History. Keep every archived allocation. Over a few periods you can compare realized volatility against what the model predicted when the weights were set. If realized volatility is consistently higher than expected, the problem is usually upstream — a stale universe, a date range that’s too short, or a risk model that doesn’t match the assets you hold.

Best Practices for Automated Portfolio Optimization

  • Align the objective with the mandate. Minimum volatility if the goal is the lowest possible variance; maximum Sharpe ratio if you’re optimizing return per unit of risk; minimum semi variance if only downside matters to you; hierarchical risk parity if you want the allocation driven by the correlation structure itself; conditional value at risk if your constraint is tail loss. RSJ Portfolio lets you switch between all five — switching deliberately is the discipline. For the tail-risk case, see What Is Conditional Value at Risk in Portfolio Optimization?.
  • Use weight constraints to encode reality. Position limits, liquidity limits, mandate restrictions — the constraints are selectable and can be combined, so express the real-world bounds in the configuration rather than fixing them by hand after the fact. A constraint applied after the optimizer runs isn’t a constraint; it’s a distortion.
  • Review the candidate list on a schedule, not on a whim. Automation gives you consistency, not judgment. Correlation structures shift when businesses change, and a universe assembled three years ago may no longer contain weakly correlated names. Pair the periodic review with an automated screen — How to Screen Stocks for Low Correlation Automatically covers the mechanics.
  • Document the parameters. The manifest is the artifact that makes a run reproducible six months later. Diff it whenever you change something.
  • Re-verify the cache location after upgrades. A reinstall that resets the cache path will silently rebuild history from scratch on the next run — slower, and with a different data vintage.
  • Keep the manual click deliberate. The wrapper tells you a run is due and archives what came out. It shouldn’t pretend to be the optimizer.

FAQ

Can I schedule RSJ Portfolio to run automatically? RSJ Portfolio is a Windows desktop application. While it does not include a built-in scheduler, you can automate recurring optimization by scripting the workflow around the application — using its configuration settings and export features — to run at defined intervals.

How does caching end-of-day prices help reduce correlation and volatility? Caching end-of-day prices eliminates the need to re-download data for each optimization run, making frequent rebalancing practical. This allows you to regularly update correlation estimates and re-optimize allocations, which helps maintain a portfolio of weakly correlated assets and thereby reduces volatility.

What if my portfolio includes multiple currencies? RSJ Portfolio provides a conversion path for portfolios quoted in several currencies, as detailed in the data and currencies documentation. You can configure the local currency and the product handles the necessary conversions.

Is there an API for automation? RSJ Portfolio does not expose a public API. Automation is achieved through the desktop application’s configuration and export capabilities, which allow you to integrate the optimizer into a repeatable workflow.

Conclusion

The difference between an optimizer you use once and one you actually run on schedule is mostly plumbing: a cache that makes repeated runs cheap, a configuration that makes them consistent, and exports that make the output usable. Set those three up properly and the quarterly rebalance stops being a project and becomes a scheduled task with a drift report attached.

Start with the getting started guide, configure the data provider, cache location and local currency once, pick your objective and risk model, and save the candidate list. Then write the manifest, register the task, and let the cache do the repetitive work.


Related posts

EU label: AI-generated content