Modelando três mercados (BR / US / Cripto) com um só motor de rebalanceamento
Parte de uma série sobre construir o Balance , um app de rebalanceamento de carteira, como dev solo. O Balance começou como um app só do Brasil: tickers da B3, preços em reais, IR brasileiro. Depois vieram ações dos EUA. Depois cripto. O caminho ingênuo teria sido três apps colados — três buscadores de preço, três conjuntos de formulários, três motores de rebalanceamento. Em vez disso, tudo gira…
The Balance app, a portfolio rebalancing tool, began as a single app for the Brazilian stock market, using B3 tickers and prices in reais, with Brazilian tax considerations. It later expanded to include US stocks and cryptocurrencies. Instead of creating three separate apps, the developers decided to build a unified system with a single market field: market = models.CharField(max_length=6, choices=[(BR, "Brazil"), (US, "United States"), (CRYPTO, "Crypto")], default=BR).
All market-specific aspects branch from this market field, while the core rebalancing engine remains unchanged.
The three markets differ in four key aspects: the source of price data, the currency symbol, tax rules, and the precision of quantities. For example, the price source differs between BRAPI for Brazilian stocks, Yahoo for US stocks, and Binance for cryptocurrencies. The ticker format is also different, with US stocks using symbols like AAPL, Brazilian stocks using PETR4, and cryptocurrencies using BTC.
The rebalancing engine is almost identical across all markets, except for the handling of cryptocurrency quantities. When the portfolio market is set to CRYPTO, the engine rounds quantities to two decimal places, while other markets use the default of eight decimal places. The display format adapts accordingly, removing unnecessary trailing zeros for cryptocurrencies while keeping other quantities precise. This approach minimizes changes to the core rebalancing engine while accommodating market-specific nuances.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.