← back to the bench
on the benchsoftware · Jun 2025 → now

nestLeap

Long-term stock & market research platform using deterministic scoring and LLM (Gemini) integration

tl;dr

Having invested in stocks since I was 16, I wanted to build a tool that could quickly identify investment opportunities and summarize news in a way that would be useful for me. nestLeap is my attempt at this. Every Sunday, it pulls fundamentals for about 200 US large caps, scores each one with a deterministic rules-based model, and then asks Gemini for a thesis, risks and a fair value. A simulated portfolio puts $1,000 a week behind its picks, and the same money goes into the S&P 500 for comparison. After about seven months, it's outperforming the index by around 14 percentage points, including the November correction. While some further testing is needed, these results give me the confidence to build an automated trading pipeline, with a full audit trail for automatic investments.

Rules first, LLM second

One thing I wanted to avoid from the start was an LLM making up numbers, which happened frequently in my early tests. While relatively inconsistent on its own, Gemini could perform much better when fed a variety of data such as fundamentals, earnings and recent news as a basis for every analysis. nestLeap is based on a deterministic scoring system, which gets injected into the prompt as a baseline and adjusted from there.

The deterministic model is made up of 16 checks across four pillars. Each one is bucketed against absolute thresholds instead of being ranked against peers, so a score means the same thing every week:

  • Quality (35%): ROE, operating margin, debt/equity, current ratio, FCF yield
  • Value (30%): P/E, PEG, P/B, EV/EBITDA, FCF yield
  • Growth (25%): revenue growth, earnings growth, P/E compression (forward vs trailing)
  • Momentum (10%): price vs 200-day average, RSI, position in the 52-week range

If a metric is missing, it gets a fixed number of "neutral" points instead of shrinking the denominator, and a stock needs at least 8 of the 16 data points to be scored at all.

Initial Portfolio

After the scoring system was set up, I constructed a mock portfolio to track investments over time before investing any real money (though I have acted on some stock picks in my personal portfolio, which have worked out great). I implemented a simple system with $1,000 weekly buys, where stocks are sold once they are deemed "fair value" with the proceeds getting reinvested. As a benchmark, the same $1,000 goes into a virtual SPY position every week.

The dashboard below uses real data from the live site, as of 6 November 2026:

try it →

the dashboard, with real numbers

nestLeap
real data · batch of 2026-11-05 · 202 tickers · 6,228 analyses
invested
$31,000
$1,000 / week
portfolio
$34,639
40 holdings
model return
+11.7%
on invested capital
S&P 500
−2.3%
same $ into SPY
vs S&P
+14.0 pp
325 trades so far
performance vs S&P 500 · weekly model S&P
0%5%10%15%+11.7%−2.3%04-1811-06
biggest holdings
TSM$2,819-15.6%
INTU$2,614+12.3%
ACN$2,479+61.9%
CRM$2,249+14.2%
CTSH$2,053+30.0%
NXPI$2,002+4.0%
ZBH$1,972+14.9%
latest rebalance
  • BUYAMZNTop-10 undervalued, score 64
  • BUYMSFTTop-10 undervalued, score 74
  • BUYUNHTop-10 undervalued, score 74
  • BUYGOOGLTop-10 undervalued, score 78
  • BUYXOMTop-10 undervalued, score 84
  • BUYORCLTop-10 undervalued, score 84
  • BUYMETATop-10 undervalued, score 86

Admin Panel showing current performance & detailed transaction history.

Maintenance Issues

In early November, I noticed that the site had stopped recommending anything. The server itself was fine, and prices and scores were still updating, but every Gemini call had been failing. After some diagnosis, it turned out that the Gemini model (2.5 Flash) was no longer supported, which a simple upgrade to 3.8 Flash resolved.

Luckily, the portfolio only buys stocks with a valuation, so with no valuations it simply didn't trade that week. While fixing it, I also found out that the yfinance version on the server had been silently returning no news at all. Both problems are covered now: a week where most analyses fail no longer trades, and all dependencies are pinned.

v2: Improving Consistency Further

Looking back through the 7 months of trading history, the main issue seemed to be that price targets still changed significantly week-to-week, even when stock fundamentals stayed consistent. About 21% of the labels changed from one week to the next, and a third of those changes reversed the week after, usually without any new information.

To fix this, v2 includes Gemini's previous assessment, what changed in the metrics since then, any earnings reported in between and dated news, with anything new marked as [NEW]. Instead of starting from scratch, Gemini has to explain what changed. Every fourth week, it gets a fresh look without the previous assessment, so new perspectives are detected.

I A/B tested this on the live API with 28 stocks:

  • Running the same inputs twice now gives exactly the same fair value (it used to differ by 1.7%).
  • For stocks without earnings, the median weekly fair value move dropped from 6.3% to 0.0%.
  • After earnings, it still revised (V, XOM and ABBV all moved by 7–10%), and every change note cited the actual results.

v2: Strategy and Backtesting

The original trading rules were very basic: put equal amounts into the top 10, and sell everything as soon as a stock stops being undervalued. For v2, I moved the rules into pure functions that the website portfolio, the backtester and the broker trading all share:

  • Buy and sell zones. Buy at 12% or more upside to fair value, sell at 5% or less, and do nothing in between.
  • Selling within two weeks. The first week in the sell zone sells half, the second week the rest.
  • Conviction-weighted sizing with a 10% cap per stock.
  • A trend filter. While the S&P 500 is below its 200-day average, it keeps 30% of the portfolio in cash.
  • A coverage guard that skips trading in any week where Gemini mostly failed.

To evaluate the new strategy, I replayed the 29 weeks of stored Gemini analyses under different rules. Since the old rules reproduce the real paper record to the cent, there's no hindsight in it. Interestingly, all variants landed within about 1.5% of each other: which stocks Gemini picks drives the returns, while the rules mostly change the risk.

To see the risk side, I also ran the rules through the dot-com bust, 2008, COVID and 2022, plus 800 synthetic two-year markets stitched together from real returns. The trend filter turned out to be the biggest protection, cutting the worst drop in a 2008 repeat from −60% to −48%, at the cost of 1–2 points in fast rebounds. The dot-com bust is the one scenario where holding only a few stocks hurts no matter what.

backend/app/analysis/strategy.py
# Zones on upside = fair_value / price - 1.
# Calibrated to Gemini's own labels: "Undervalued" starts near +12% upside.
buy_upside: float = 0.12
# Sell once the stock is no longer cheap. Waiting for real overvaluation
# almost never sells, because Gemini's fair values drift up with the price.
sell_upside: float = 0.05
# Round 1 in the sell zone: sell half. Round 2: sell the rest.
confirm_rounds: int = 2
trim_fraction: float = 0.5

# Sizing
top_n: int = 10
weighting: str = "conviction"
max_position_pct: float = 0.10

# While SPY < its 200-day SMA, keep this much of the portfolio in cash.
# Never sells holdings to raise cash, it only stops deploying new money.
trend_filter: bool = True
risk_off_cash_pct: float = 0.30

# Skip trading when fewer than 90% of analyses produced a fair value.
min_coverage: float = 0.90

Part of the v2 config. The same rules run the website portfolio, the backtester and the broker.

Real Trading & Audits

The paper portfolio held up well through the recent Nasdaq correction: since the October high, it went from +8.0% to +11.7%, while the S&P went from +3.0% to −2.3%. Naturally, the next step would be to let the system build a real portfolio autonomously. This is something I'm still working on for v2 and don't want to rush, as a mistake now would cost real money. What's built so far:

  • A weekly cycle: propose, approve, submit, sync fills and reconcile, with every step safe to re-run.
  • Safeguards: a kill switch, a paper/live lock, caps on new money and on the number of buys per week, a whitelist of tradable stocks, a price check before each order, and limit orders during NYSE hours only.
  • Daily reconciliation: if the broker's positions don't match the database, trading halts.
  • A weekly audit with 13 checks, including a hash-chained event log (so an edited or deleted entry gets detected) and a replay that recomputes each week's decision from the stored inputs.

The current v2 runs against a simulated broker, and I approve every week by hand. Next up is the Interactive Brokers paper account, and once that has run cleanly for a few weeks, real money.

Things I learned building it

  • One prompt beats a team of agents. Early versions used separate Gemini calls for news, fundamentals and valuation. Folding them into one structured prompt cut cost and latency by about 5×, and the answers actually got more consistent.
  • SQLite is fine, actually. The working set is only about 5 MB, so a single file on a volume beats running a database container on a small VPS.
  • Make batches re-runnable. Each stock has a 90 s timeout, and anything analysed in the last day gets skipped, so if a Sunday batch crashes, I can just run it again cheaply.
  • One codebase, two apps. The same Next.js code builds as a standalone server for the web and as a static export inside Capacitor for iOS.
  • Fail loudly. A warning in a log file isn't an alert. The Gemini outage went unnoticed for nine days because nothing complained.
  • Don't just trust the LLM. Telling it to "only adjust a little" in the prompt isn't a real constraint, so the trading rules work on the fair values with their own buffers instead of taking Gemini's labels at face value.
frontend/app/page.tsx
function PerformanceChart({ data }: { data: PortfolioHistoryPoint[] }) {
  if (data.length < 2) return null;

  // Cumulative return over amount invested. Both series start at 0% and diverge.
  const points = data.map((d) => ({
    date: d.date,
    portfolio: d.invested > 0 ? ((d.portfolio - d.invested) / d.invested) * 100 : 0,
    sp500: d.invested > 0 ? ((d.sp500 - d.invested) / d.invested) * 100 : 0,
  }));

  const allValues = points.flatMap((p) => [p.portfolio, p.sp500, 0]);
  const rawMin = Math.min(...allValues);
  const rawMax = Math.max(...allValues);
  // Add ~15% headroom on each side, minimum 2 pp so a flat series still has vertical space.
  const pad = Math.max((rawMax - rawMin) * 0.15, 2);

Since dollar-cost averaging adds money every week, the raw portfolio value goes up no matter what, which makes it useless for comparison. Instead, the chart plots the return on invested capital.