Portfolio Rebalancer
Calculate buy/sell amounts to rebalance your investment portfolio to target allocations.
Asset Name
Current Value ($)
Target %
%
%
%
| Asset | Current ($) | Current % | Target % | Action |
|---|---|---|---|---|
| US Stocks | $50,000.00 | 55.56% | 60.00% | +$4,000.00 |
| Bonds | $30,000.00 | 33.33% | 30.00% | -$3,000.00 |
| International | $10,000.00 | 11.11% | 10.00% | -$1,000.00 |
| Total | $90,000.00 | 100% | 100.00% |
= over-allocated (sell)
= under-allocated (buy)
Balanced
Questions Fréquentes
Implémentation du Code
def rebalance_portfolio(holdings: dict, total_value: float = None) -> dict:
"""
holdings: {'AssetName': {'current_value': 50000, 'target_pct': 60}}
Returns buy/sell amounts needed
"""
if total_value is None:
total_value = sum(h['current_value'] for h in holdings.values())
results = {}
for name, h in holdings.items():
current = h['current_value']
target_pct = h['target_pct']
current_pct = (current / total_value) * 100 if total_value > 0 else 0
target_value = (target_pct / 100) * total_value
diff = target_value - current
results[name] = {
'current': current,
'current_pct': round(current_pct, 2),
'target_pct': target_pct,
'target_value': round(target_value, 2),
'action': round(diff, 2),
'action_type': 'BUY' if diff > 0 else 'SELL' if diff < 0 else 'HOLD'
}
return results
# Example
portfolio = {
'US Stocks': {'current_value': 50000, 'target_pct': 60},
'Bonds': {'current_value': 30000, 'target_pct': 30},
'International': {'current_value': 10000, 'target_pct': 10},
}
result = rebalance_portfolio(portfolio)
total = sum(h['current_value'] for h in portfolio.values())
print(f"Total Portfolio: ${total:,.0f}")
for name, r in result.items():
action = "BUY" if r['action'] > 0 else "SELL"
print(f"{name}: {r['current_pct']}% → {r['target_pct']}% | {action} ${abs(r['action']):,.0f}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.