A simple portfolio rebalancing engine implemented in Go.
This project calculates which stocks should be bought or sold in order to match a desired target allocation.
A portfolio consists of:
- Current holdings (stocks with quantity and price)
- Target allocation (desired percentage distribution)
The system:
- Calculates total portfolio value
- Determines current allocation
- Compares against target allocation
- Generates actions: BUY / SELL / HOLD
| Stock | Value |
|---|---|
| META | 600 |
| AAPL | 400 |
Total = 1000
| Stock | % |
|---|---|
| META | 40% |
| AAPL | 60% |
| Stock | Action | Amount |
|---|---|---|
| META | SELL | 200 |
| AAPL | BUY | 200 |
The project follows a simple layered structure:
cmd/ → entry point (main)
internal/
domain/ → core business logic
application/ → use case orchestration
types/ → shared models
- Domain logic is isolated from application logic
- Rebalancing is deterministic and pure
- No external dependencies (kept intentionally simple)
- Action types use integers (DB and i18n friendly)
- Go 1.20+
go run cmd/main.gogo test ./...For each stock:
target_value = total_portfolio_value * target_percentage
difference = target_value - current_value
- If difference > 0 → BUY
- If difference < 0 → SELL
- If difference = 0 → HOLD
- Stocks present in holdings but not in target → SELL all
- Stocks present in target but not in holdings → BUY from zero
- Empty portfolio handling
This project was developed with the assistance of an LLM for:
- validating architectural decisions in Golang constraints
- Identifying edge cases
All implementation and final decisions were reviewed and written manually.
Additional development notes, including design decision and LLM usage, are available in:
docs/development-notes.pdf
(Not implemented to avoid overengineering)
- Transaction fees handling
- Minimum trade thresholds
- Real-time price integration
- API interface (REST)
- AI-driven allocation (external system)
Jhoan Gutiérrez
Software Engineer (Backend & Systems Focus)