DEV Community

Dany Trakhtenberg
Dany Trakhtenberg

Posted on

I Built a Risk-Driven Stock Portfolio Optimizer (And What I Learned)

Like many developers, I’ve seen a lot of “portfolio builder” tools that promise outperformance with vague AI claims or black-box optimization.

I wanted to explore a different angle:

What if risk controlled portfolio structure — not just which stocks are labeled “risky”?

So I built StockRisker, a risk-driven stock portfolio optimizer that focuses on constraints, explainability, and realistic tradeoffs rather than guarantees.

👉 Live demo: https://stockrisker.com/build

The Core Idea

Most retail investing tools treat risk as a side effect:

  • Higher risk → “riskier stocks”

  • Lower risk → “safer stocks”

In practice, that’s not how portfolios behave.

Risk tolerance usually affects:

  • How concentrated you’re willing to be

  • How much sector exposure you allow

  • How asymmetric your allocations can get

So in StockRisker:

  • Risk directly controls portfolio constraints
  • Higher risk = fewer penalties, higher concentration caps
  • Lower risk = stricter diversification rules

Risk isn’t just a label — it’s a structural input.

How the Optimizer Works (High Level)

The optimizer is built around three layers:

  1. Risk Mapping

A user selects a risk level (1–10).
That value dynamically controls:

  • Max allocation per stock

  • Max allocation per sector

  • Strength of diversification penalties

At high risk levels, penalties are relaxed instead of forcing “safe” portfolios that don’t match user intent.

  1. Constraint-Driven Portfolio Generation

Portfolios are evaluated under:

  • Hard constraints (must never be violated)

  • Soft constraints (applied as scoring penalties)

This avoids a common failure mode where optimization technically succeeds but produces unrealistic portfolios.

  1. Explainable Scoring

Each portfolio is scored using a weighted function that balances:

  • Expected return estimates

  • Volatility and diversification

  • Concentration and sector exposure

If results look suspicious (e.g., very high return vs relatively low volatility), the UI surfaces contextual warnings instead of hiding the risk.

No “guaranteed outperformance” messaging.

What I Optimized Against

One of the most interesting challenges wasn’t math — it was expectation management.

Some things I intentionally avoided:

  • No promise to beat the S&P 500

  • No opaque AI model

  • No single “correct” portfolio

Instead, the tool helps users reason about tradeoffs:

  • Concentration vs diversification

  • Risk appetite vs volatility

  • Simplicity vs optimization

Tech Stack

  • Backend: TypeScript + Node.js

  • Modular optimizer design (risk mapping → constraints → scoring)

  • Cached stock universes for performance and stability

  • Frontend: Clean, minimal UI focused on clarity and explainability

The architecture is designed to support future extensions like:

  • Backtesting

  • Risk-adjusted metrics (Sharpe, drawdown)

  • Scenario simulations

What I Learned

Risk is a UX problem, not just a math problem
Users need to understand why a portfolio looks the way it does.

Constraints matter more than algorithms
A simple optimizer with good constraints beats a complex one without them.

Transparency builds trust faster than performance claims
Surfacing limitations actually increases confidence.

Top comments (0)