Portfolio rebalancing bot: automating target weights
A portfolio rebalancing bot keeps a basket of assets at fixed target weights — say 50% BTC, 30% ETH, 20% stablecoin — by periodically selling whatever has grown beyond its target and buying whatever has fallen below it. It is the most disciplined, contrarian behaviour in investing reduced to a few lines of code: it systematically trims winners and adds to laggards, which both controls risk and harvests a small "rebalancing bonus" from mean reversion between uncorrelated assets. This guide explains how rebalancing works, builds both calendar and threshold rebalancing bots with real code, and is honest about the fee and tax drag that decides whether rebalancing actually helps you.
What a rebalancing bot does
You define target weights for each asset. Over time price moves push the real weights away from the targets — a rallying asset becomes overweight, a laggard becomes underweight. The bot computes the gap and trades it back: sell the overweight, buy the underweight, until the basket matches its targets again. It is the engine behind disciplined portfolio management, and it enforces "buy low, sell high" mechanically.
Why rebalance
Two reasons. First, risk control: without rebalancing your best performer slowly dominates the portfolio, and you end up concentrated in one asset exactly when it is most expensive. Rebalancing caps that drift. Second, the rebalancing bonus: between volatile, imperfectly-correlated assets, systematically selling the one that rose and buying the one that fell harvests a small excess return from mean reversion — the same edge a mean-reversion strategy chases, applied at the portfolio level.
Building the rebalancing bot
Read balances and prices, compute current weights, and trade each asset back toward its target. The core is pure arithmetic.
python · rebalance_bot.pytargets = {'BTC': 0.5, 'ETH': 0.3, 'USDT': 0.2}
bal = ex.fetch_balance()
prices = {a: ex.fetch_ticker(f'{a}/USDT')['last']
for a in targets if a != 'USDT'}
prices['USDT'] = 1.0
vals = {a: bal[a]['total'] * prices[a] for a in targets}
total = sum(vals.values())
for a, w in targets.items():
drift = vals[a] - w * total # + = overweight, sell
if abs(drift) < 0.05 * w * total: continue # 5% band
units = abs(drift) / prices[a]
side = 'sell' if drift > 0 else 'buy'
print(side, round(units, 6), a)
# ex.create_market_{side}_order(f'{a}/USDT', units)
Note the 5% no-trade band — it stops the bot churning on tiny drifts and paying fees for nothing. Run it on a schedule per how to deploy a trading bot.
Calendar vs threshold rebalancing
Calendar rebalancing trades on a fixed schedule (monthly, quarterly) regardless of drift — simple and predictable. Threshold rebalancing trades only when an asset drifts beyond a band (e.g. ±5% absolute, or ±25% relative) — more responsive and usually more efficient, because it only acts when there is real drift to correct. Most robust bots combine them: check on a schedule, trade only if the band is breached, exactly as the code above does.
Fees, taxes and the drag
Every rebalance is a round of taxable, fee-paying trades. Rebalance too often and fees plus slippage erode the rebalancing bonus entirely; in a taxable account, frequent rebalancing can also trigger short-term capital-gains events — see tax implications. Use a no-trade band, prefer wider thresholds, and rebalance with new contributions where possible so you buy laggards without selling winners.
Getting started safely
Choose targets you genuinely believe in for the long run, set a sensible no-trade band, paper trade the rebalance logic to confirm the maths before any live order, and start with infrequent, wide-band rebalancing to keep fee and tax drag low.
Frequently asked questions
What is a portfolio rebalancing bot?
A portfolio rebalancing bot keeps a basket of assets at fixed target weights by periodically selling whatever has grown beyond its target and buying whatever has fallen below it. It enforces "buy low, sell high" mechanically — trimming winners and topping up laggards to restore your chosen allocation and control concentration risk.
What is the difference between calendar and threshold rebalancing?
Calendar rebalancing trades on a fixed schedule like monthly or quarterly regardless of how far weights have drifted. Threshold rebalancing only trades when an asset drifts beyond a set band, such as plus or minus 5%. Threshold is usually more efficient because it acts only when there is real drift to correct, and many bots combine the two: check on a schedule but trade only if the band is breached.
Does rebalancing actually improve returns?
It primarily improves risk control by preventing one asset from dominating the portfolio. Between volatile, imperfectly correlated assets it can also harvest a small rebalancing bonus from mean reversion. But fees, slippage and taxes can erase that bonus if you rebalance too often, so wide no-trade bands and infrequent rebalancing matter.
How do I avoid fees and taxes eating my rebalancing gains?
Use a no-trade band so the bot ignores tiny drifts, prefer wider thresholds and less frequent rebalancing, and where possible rebalance with new contributions — buying underweight assets without selling overweight ones avoids realising gains. In a taxable account, frequent rebalancing can trigger short-term capital-gains events, so model the tax cost before automating.