AWS Builder Center
Weekend Annoying Task Challenge: The Lazy Rebalancer

Weekend Annoying Task Challenge: The Lazy Rebalancer

Rebalancing my portfolio was my most annoying recurring chore — so I built a serverless app that checks the drift for me and finds the mathematically cheapest speed to fix it. Built in a weekend on Amazon S3, API Gateway, Lambda, and Bedrock.

Vision & What the app does

My most annoying recurring task isn't dramatic — it's the weekly portfolio check. Open the brokerage app, look at every position, wonder "have I drifted enough from my target mix that I need to do something?", usually conclude "probably not?", and repeat next week with the same nagging uncertainty. And on the months where the answer is "yes", a second chore begins: an evening with a spreadsheet working out what to buy and sell — followed by paying the trading costs of jumping all the way back in one shot.
The Lazy Rebalancer kills the whole loop. Open it, and the check has already happened: it pulls live prices for your holdings (type any ticker — US stocks, Indian NSE symbols, crypto — and a live search dropdown finds it; enter shares and the values compute themselves), measures your drift, and answers the real question in a banner before you touch anything: "Nothing to do — drift 2.1%, below your alert level, come back later" or "Time to act — here's your plan." You set the alert threshold yourself with a slider. Two more chores die with it: a "new cash" mode computes a buy-only plan for fresh money (deploy your monthly savings toward target without selling anything — no capital-gains events), and a Copy orders button turns any plan into broker-ready text.
Here is the thing I learned while doing a deep dive into quantitative finance this year: jumping straight back to your target is mathematically the wrong move when trading has friction. There is a famous result in finance (Gârleanu & Pedersen, 2013, "Dynamic Trading with Predictable Returns and Transaction Costs") showing that when large trades cost disproportionately more — market impact, spreads widening on size, tax-bracket effects — the optimal strategy is to trade only partway toward your target each period. Hedge funds literally run on this math. Retail investors have never had a simple tool for it.
The Lazy Rebalancer is that tool. You enter your holdings, your target percentages, and three sliders: your trading cost (in basis points), your "large-trade friction" (the quadratic cost — the Gârleanu–Pedersen ingredient), and how much being off-target drags on you per year. The app then finds your optimal laziness θ* — what fraction of the gap to close each month — by honestly simulating every speed from "glacial" to "jump now" and adding up all the dollars. It shows you both plans side by side, the dollar savings, a cost-vs-speed curve where you can see the sweet spot, this month's exact trades, and a plain-English explanation written by Amazon Bedrock.
Two honest touches I'm proud of: if your costs are low, the app tells you plainly "just rebalance now — impatience is fine here" (the math degenerates correctly: with zero quadratic friction, θ*=100% and laziness saves $0). And it states clearly that it is an educational calculator applying your targets and your assumptions — arithmetic, not investment advice.

How I built it

The core is a ~90-line pure-Python cost simulator. For each candidate speed θ, it walks month by month: trade θ of the remaining gap (paying linear bps costs plus a quadratic impact term on the trade size), then suffer the drag of whatever drift remains. Summing both costs over 12 months gives each θ a total dollar cost; the minimum is θ*.
The key design decision was keeping the model honest rather than impressive. My first version had only linear costs — and the optimizer kept answering θ*=100% ("just rebalance now"). That is actually correct: with purely linear costs, trading slowly moves the same total dollars and just adds drag. The laziness only becomes optimal once the quadratic term exists — which is exactly the theory's point. Debugging my way into re-deriving the paper's core insight was the best moment of the weekend.
The second challenge was trust: a calculator like this must give the same answer every time. So there is no ML training loop, no random seed anywhere — the optimizer is a deterministic grid search, and the frontend even contains the identical math in JavaScript as an offline fallback, so the app works before the backend URL is configured.

AWS Services Used / Architecture Overview


  • Amazon API Gateway (HTTP API) — the public front door; one proxy route to Lambda with CORS. (Fun war story: I first used a Lambda Function URL, hit a persistent 403 on my fresh account, and swapped to API Gateway in four CLI commands — a good lesson in having two ways to expose a function.)
  • AWS Lambda — one 256 MB Python function, three actions, standard library only. The agent is triggered by the browser's POST through API Gateway.
  • Amazon Bedrock (Nova Lite) — turns the numeric result into three friendly sentences a non-quant can act on.
  • Amazon S3 — static website hosting for the single-file frontend.
  • A neat testing trick:  local_server.py  serves the same frontend and routes  /api  through the byte-identical Lambda handler locally, so local testing exercises exactly the code that deploys.
  • Everything sits comfortably inside the Free Tier. Live quotes are informational (public Yahoo data, delayed on some exchanges) — fine for rebalancing math, and disclosed in-app.

What I learned

First: Lambda Function URLs are wonderfully underrated — a public HTTPS endpoint with CORS in two CLI commands, no API Gateway ceremony. Second: Bedrock's Converse API makes the "explain this JSON like I'm human" pattern almost free. Third, and biggest: an app earns trust by refusing to overclaim — building the degenerate case ("your costs are low, just rebalance") into the product made it feel like advice from an honest friend rather than a tool trying to look clever. And I finally understand, at the level of running code, why hedge funds trade lazily: it is not patience, it is arithmetic.

Link to App / Repo

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article