Implementing an AI Portfolio Manager. With Learning!
Building a continuously learning AI portfolio manager for different investment styles
Most AI trading demos stop at “call GPT with some stock data and print the response.” Here’s how I am building an AI agentic system that manages real portfolios — picks stocks, sizes positions, rebalances on schedule, and learns from its own track record.
Architecture Overview
The stack is Cloudflare Workers + D1 (SQLite at the edge) + a React frontend. The AI portfolio system is a worker that handles CRUD, holdings computation, and the AI allocation pipeline. A separate cron worker triggers scheduled rebalances.
All AI calls route through an api-gateway worker for centralized logging and rate limiting. The model is gpt-oss-120b via Groq — fast inference, JSON mode, 0.3 temperature for deterministic outputs (but will be comparing it against other alternatives later).
The Two-Trip Allocation Pattern
The core insight: you can’t just dump 8,000 stock tickers into a prompt. Token limits aside, the AI produces garbage when overwhelmed with data. So allocation is a two-trip process.
Trip 1: List Selection. The outside platform (QuantApe) maintains curated screener lists — “momentum breakouts,” “oversold value plays,” “high dividend yield,” etc. The AI picks 5-10 relevant lists based on the portfolio’s strategy and current market conditions.
Trip 2: Allocation. Pull the actual symbols from those lists (~50 candidates), enrich with technical indicators (RSI, MACD, Bollinger bands, swing percentages, volume ratios), and ask the AI to build a portfolio.
Trip 1 Trip 2
This narrows the universe before the expensive reasoning step. The AI reasons over 50 enriched candidates instead of 8,000 sparse rows.
Strategy as Prompt Engineering
Six strategies are supported: short-term momentum, mid-term momentum, long-term momentum, value, growth, and income. Each maps to a block of prompt instructions that fundamentally changes the AI’s behavior.
A short-term momentum strategy requires daily swing above 2%, focuses on RSI reversals and Bollinger band entries, and exits when RSI hits overbought. A value strategy looks for low P/E with analyst upside and oversold quality signals. Same model, same weights — completely different stock-picking behavior driven by prompt structure.
Each strategy also has preset list categories (which screener lists to pull from), a data emphasis profile (which metrics matter most), a cap-size focus, and a natural rebalance cadence.
The rebalance cadence interacts with tax treatment. If a user selects “long-term optimized,” the cadence floors at monthly regardless of strategy — because you don’t want a momentum strategy churning positions before the 12-month long-term capital gains threshold.
Holdings From Transactions, Not Snapshots
There’s no “holdings” table. Holdings are computed from the transaction ledger — sum up buys, subtract sells, filter for positive balances. This is intentional. The transaction log is the source of truth. Every position traces back to a dated buy with a price and the AI’s reasoning. Computed holdings can never drift from reality because they’re derived, not stored.
Live prices get joined in at query time to produce market values, P&L, and weights.
Rebalancing: Exit Criteria in the Prompt
The rebalance prompt is where things get interesting. The AI sees its current positions with full technical overlays and has to make sell/hold/buy decisions. But LLMs are biased toward action — they’ll always find something to trade. So the prompt encodes explicit exit criteria and trading aggressiveness tuned to the rebalance frequency.
The exit rules are concrete: up 10%+ with weakening momentum means trim, down 15%+ with no recovery signal means cut, up 25%+ means consider locking in profits regardless.
A daily rebalancer is told to be “very active, react to intraday signals.” A monthly rebalancer is told to be “conservative, only act on urgent situations.” Same model, completely different behavior.
Position count enforcement is another prompt-level constraint. If holdings exceed the configured maximum, the AI gets an explicit instruction to sell the weakest positions down to the limit. Without this, the model tends to suggest buys without selling, gradually bloating the portfolio.
The Missing Piece: Memory
Everything above worked, but each rebalance was stateless. The AI had no idea whether its previous decisions were right or wrong. It would buy the same volatile name on every dip, oblivious to the fact that this trade had lost money three times running.
The Learning Loop
Each rebalance cycle now has three extra steps:
1. Score previous trades. Before building the prompt, look back at trades from the last cycle and evaluate them against current prices. For buys still held: is the position profitable? Simple binary scoring — profitable equals correct. This is deliberately naive. A more sophisticated system might evaluate risk-adjusted returns or whether the thesis played out regardless of P&L. But for prompt injection, a clear signal beats a nuanced one.
2. Inject memory into the prompt. The last 10 memory entries get formatted and appended to the rebalance prompt.
Token budget matters here — the prompt is already packed. So the three most recent cycles get full detail (each trade scored, the reflection text), while the older seven get compressed to a single line with just the date, P&L, win rate, and a truncated reflection. This keeps memory context around 500 tokens.
3. Generate reflection. After trades execute, a separate lightweight AI call reviews the scored outcomes and writes a 2-3 sentence self-assessment. This is the qualitative layer — the AI identifies patterns like “dip-buying volatile names was premature” or “momentum exits continue to work well.” Max 200 tokens. The output gets stored and fed into the next cycle’s prompt.
What the AI Portfolio Manager Sees
Every time the AI makes a buy/sell decision, it receives a comprehensive briefing assembled from 10+ real-time data sources across platform. Here’s everything it has access to:
Market Regime & Macro
- Fear & Greed Index — Current market sentiment score (0-100), used to calibrate aggression. The AI deploys cash in fear, raises cash in greed.
- Economic Events — Upcoming high-impact US events for the next 7 days (Fed decisions, CPI, jobs reports) with forecasts, plus recent releases with surprise/in-line flags and an AI-generated assessment of market implications.
- Market Analysis — AI-generated market outlook, overall sentiment, top 3 risks, opportunities, catalysts, and per-sector sentiment (tech, finance, healthcare, consumer, industrial).
- Treasury Yield Curve — Current 3M, 2Y, 10Y, 30Y rates plus 2Y-10Y spread with automatic inversion detection. Tells the AI whether the rate environment favors growth or value, and flags recession signals.
- Sector Rotation — Which sectors have strong/weakening momentum, sourced from our sector rotation model.
Available to momentum and sector-rotation strategies.
Per-Stock Intelligence (for every candidate and holding)
Price & Trend:
Current price, daily change, 52-week range
50-day and 200-day moving averages (above/below)
Volume vs. 3-month average
Technical Indicators:
RSI (overbought/oversold)
MACD histogram (momentum direction)
Bollinger Bands (volatility position)
Stochastic %K
Resistance levels
Swing Potential:
Daily, weekly, and monthly expected swing percentages
Best trading timeframe for each stock
Fundamentals:
PEG ratio, revenue growth rate
Gross and operating margins
Short interest (% of float) — crowded trade detection
Beta — portfolio risk calibration
Analyst consensus target price with upside/downside %
Forward and trailing EPS
Event Risk:
Days until next earnings date — the AI avoids buying into uncertain earnings or takes profits pre-announcement
Company-Specific News (rebalance only)
Up to 3 recent headlines per held stock from the last 3 days, sourced from our news aggregation pipeline. Helps the AI decide whether to hold through news or exit.
Earnings Deep Dive (earnings strategy only)
Historical earnings reactions (average move, win rate, last 4-6 quarters)
Real-time Perplexity-sourced intel on analyst revisions, industry trends, and catalysts
Strategy Configuration
Strategy-specific instructions (momentum, value, growth, income — each with distinct entry/exit criteria)
Trading style (active/balanced/patient) with customized entry, exit, and cash management guidelines
Stop-loss, take-profit, and stale position thresholds
Cap focus constraints (small/mid/large)
Tax treatment awareness (long-term optimized strategies avoid selling positions held < 12 months)
User-provided custom instructions
Portfolio Memory (rebalance only)
Track record stats (win rate, average P&L per trade)
Last 3 rebalance cycles with full detail: what was traded, why, and a self-reflection on what worked
7 older cycle summaries capturing key learnings
The AI explicitly learns from its own mistakes and winning patterns
Memory on No-Trade Cycles
One subtle choice: memory entries are saved even when the AI recommends zero trades. “I reviewed everything and decided to hold” is a signal. It tells future cycles that the portfolio was well-balanced under specific market conditions (captured via a fear/greed snapshot stored alongside each entry). Without this, the memory only represents action — never deliberate inaction.
The Fear/Greed Context Layer
Every memory entry snapshots the Fear & Greed index at the time of the decision. This means the AI can start noticing regime-dependent patterns: “my dip-buying works when fear is 40-50 but fails when fear is 20-30.” The AI doesn’t need to be told this explicitly — it sees the correlation in its own history and adjusts.
The Cron Worker
Scheduled rebalancing is a separate worker that runs on weekdays. It doesn’t contain any allocation logic — just checks each active portfolio’s last rebalance date against its configured frequency, and hits the main worker’s rebalance endpoint for any that are due. This keeps all decision-making in one place.
What’s Next
Right now this functionality is in R&D phase - we will be updating with the results as we get them if there’s interest.
Another thing that we should look at is Clawdbot / OpenClaw implementation for temporal reasoning, as well as using MCP / tools instead of upfront data enrichment - lots of interesting experimentation, for sure!
About
QuantApe started as an experiment: what happens when you give a retail investor the same data infrastructure that quant funds rely on? Sector rotation models, earnings reaction patterns, economic event analysis, yield curve monitoring, real-time market sentiment, alternative performance indicators — all of it piped through 60+ serverless data pipelines into a single interface.
The AI Portfolio Manager is the latest extension of that experiment. Instead of just surfacing the data, what if an AI traded on it — across fourteen strategies, with a memory of its own past decisions, learning what works and what doesn’t.






