I Built a Risk-Driven Stock Portfolio Optimizer (And What I Learned)
Source: Dev.to
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”—drives the optimizer?
👉 Live demo:

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
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:
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.
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.
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 trade‑offs:
- 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.