# neochiu1004/rebalance architecture

> Architecture diagram of the GitHub repository neochiu1004/rebalance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/neochiu1004/rebalance
- Repository: https://github.com/neochiu1004/rebalance

## Overview

#### 1. Purpose and runtime
This is a client-only Taiwan equity portfolio rebalancing tool. It runs as a static browser application with no build or server requirement; `index.html` is the entry page and JavaScript modules implement the application behavior. Primary runtime state is persisted in browser `localStorage`.

Paths: `index.html`, `core.js`, `state.js`

#### 2. Application bootstrap and shared control layer
`core.js` is the likely central application coordinator: it connects page interactions, shared calculations, and feature modules. The UI is hosted in one HTML page, so module-to-module coordination occurs in the browser rather than through backend APIs or server routing.

Paths: `index.html`, `core.js`

#### 3. Persistent portfolio state and backup boundary
`state.js` owns the persistence boundary for holdings, transaction history, and user settings. Data survives browser sessions through `localStorage`; clearing browser storage, changing devices, or private browsing can lose it. Backup import replaces portfolio/settings data, while API credentials remain local and are excluded from backups.

Paths: `state.js`, `index.html`

#### 4. Holdings and transaction-record domain
Asset/holding management is separated from transaction recording. Holdings provide the current portfolio basis, while transaction records capture buys and sells used for cost and performance-oriented calculations. Trading fees and tax treatment are material domain rules: discounted brokerage commission with a minimum fee, plus differing sell tax rates for stocks versus ETFs.

Paths: `assets.js`, `transactions.js`, `trade.js`

#### 5. Trade calculation flow
The trade feature takes an intended buy or sell and applies cost rules to calculate actual paid cost or sale proceeds. Those results feed the portfolio overview’s cost basis, where estimated current-sale commission and transaction tax are recalculated from current price rather than permanently accumulated.

Paths: `trade.js`, `transactions.js`, `assets.js`

#### 6. Rebalancing workflow
The rebalancing module compares current holdings against target allocations and produces the calculations needed to move toward the desired portfolio mix. Its inputs cross the holdings/state boundary and likely use current market prices when available; its output is planning information rather than broker execution.

Paths: `rebalance.js`, `assets.js`, `state.js`

#### 7. Market-data and analytical features
Two external data boundaries supply market information:
- Fugle provides real-time quotes, authenticated with a user-entered API key.
- FinMind provides historical prices and high/low analysis, authenticated with a token.

`watch.js` represents quote/watch functionality, while `waterlevel.js` implements high/low “water level” analysis. Credentials are device-local settings and should not enter backup files.

Paths: `watch.js`, `waterlevel.js`, `state.js`

#### 8. Front-end dependencies
The browser UI depends on CDN-delivered Chart.js for chart rendering and Tailwind CSS for styling. The repository itself is plain static HTML/JavaScript; a local static server is optional only to serve `index.html`.

Paths: `index.html`, `README.md`

Main entry points: [`core.js`](https://github.com/neochiu1004/rebalance/blob/main/core.js) (Application coordinator), [`state.js`](https://github.com/neochiu1004/rebalance/blob/main/state.js) (Portfolio state), [`assets.js`](https://github.com/neochiu1004/rebalance/blob/main/assets.js) (Holdings), [`index.html`](https://github.com/neochiu1004/rebalance/blob/main/index.html) (Single-page UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser application"]
  node_page["Single-page UI<br/>HTML entry<br/>[index.html]"]
  node_core["Application coordinator<br/>browser controller<br/>[core.js]"]
  node_state["Portfolio state<br/>persistence module<br/>[state.js]"]
  node_storage[("Browser localStorage<br/>browser storage")]
end

subgraph group_portfolio["Portfolio domain"]
  node_assets["Holdings<br/>portfolio module<br/>[assets.js]"]
  node_transactions["Transaction records<br/>portfolio module<br/>[transactions.js]"]
  node_trade["Trade calculator<br/>domain rules<br/>[trade.js]"]
  node_rebalance["Rebalancing planner<br/>planning module<br/>[rebalance.js]"]
end

subgraph group_market["Market data"]
  node_watch["Quote watch<br/>market feature<br/>[watch.js]"]
  node_waterlevel["Water-level analysis<br/>market feature<br/>[waterlevel.js]"]
end

subgraph group_external["External services"]
  node_fugle{{"Fugle quotes<br/>external API"}}
  node_finmind{{"FinMind history<br/>external API"}}
  node_chartjs{{"Chart.js CDN<br/>frontend dependency"}}
  node_tailwind{{"Tailwind CSS CDN<br/>frontend dependency"}}
end

node_page -->|"boots"| node_core
node_page -.->|"loads"| node_chartjs
node_page -.->|"loads"| node_tailwind
node_core -->|"reads and updates"| node_state
node_core -->|"coordinates"| node_assets
node_core -->|"coordinates"| node_transactions
node_core -->|"uses calculations"| node_trade
node_core -->|"opens planning"| node_rebalance
node_core -->|"opens quotes"| node_watch
node_core -->|"opens analysis"| node_waterlevel
node_state -->|"persists"| node_storage
node_state -->|"stores holdings"| node_assets
node_state -->|"stores history"| node_transactions
node_trade -->|"calculates buy/sell results"| node_transactions
node_trade -->|"updates cost basis"| node_assets
node_rebalance -->|"compares holdings"| node_assets
node_rebalance -->|"reads targets"| node_state
node_watch -->|"fetches quotes"| node_fugle
node_waterlevel -->|"fetches history"| node_finmind
node_state -.->|"keeps API key local"| node_fugle
node_state -.->|"keeps token local"| node_finmind

click node_page "https://github.com/neochiu1004/rebalance/blob/main/index.html"
click node_core "https://github.com/neochiu1004/rebalance/blob/main/core.js"
click node_state "https://github.com/neochiu1004/rebalance/blob/main/state.js"
click node_assets "https://github.com/neochiu1004/rebalance/blob/main/assets.js"
click node_transactions "https://github.com/neochiu1004/rebalance/blob/main/transactions.js"
click node_trade "https://github.com/neochiu1004/rebalance/blob/main/trade.js"
click node_rebalance "https://github.com/neochiu1004/rebalance/blob/main/rebalance.js"
click node_watch "https://github.com/neochiu1004/rebalance/blob/main/watch.js"
click node_waterlevel "https://github.com/neochiu1004/rebalance/blob/main/waterlevel.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_page,node_core,node_state,node_storage toneBlue
class node_assets,node_transactions,node_trade,node_rebalance toneAmber
class node_watch,node_waterlevel toneMint
class node_fugle,node_finmind,node_chartjs,node_tailwind toneRose
```

## Components

### Browser application

Static client-only runtime.

- [Single-page UI](https://github.com/neochiu1004/rebalance/blob/main/index.html): HTML entry · Hosts the application interface and browser modules. · `index.html`
- [Application coordinator](https://github.com/neochiu1004/rebalance/blob/main/core.js): browser controller · Connects interactions, calculations, and feature modules. · `core.js`
- [Portfolio state](https://github.com/neochiu1004/rebalance/blob/main/state.js): persistence module · Owns holdings, history, settings, and backup import. · `state.js`
- **Browser localStorage**: browser storage · Device-local persistence boundary.

### Portfolio domain

- [Holdings](https://github.com/neochiu1004/rebalance/blob/main/assets.js): portfolio module · Maintains the current portfolio basis. · `assets.js`
- [Transaction records](https://github.com/neochiu1004/rebalance/blob/main/transactions.js): portfolio module · Captures buys and sells for cost and performance calculations. · `transactions.js`
- [Trade calculator](https://github.com/neochiu1004/rebalance/blob/main/trade.js): domain rules · Applies commission, minimum fee, and stock/ETF sell-tax rules. · `trade.js`
- [Rebalancing planner](https://github.com/neochiu1004/rebalance/blob/main/rebalance.js): planning module · Compares holdings with target allocations; does not execute trades. · `rebalance.js`

### Market data

- [Quote watch](https://github.com/neochiu1004/rebalance/blob/main/watch.js): market feature · `watch.js`
- [Water-level analysis](https://github.com/neochiu1004/rebalance/blob/main/waterlevel.js): market feature · Analyzes historical highs and lows. · `waterlevel.js`

### External services

- **Fugle quotes**: external API · Real-time quotes authenticated by a local API key.
- **FinMind history**: external API · Historical prices authenticated by a local token.
- **Chart.js CDN**: frontend dependency · Renders charts.
- **Tailwind CSS CDN**: frontend dependency · Styles the interface.

## Connections

- Single-page UI → Application coordinator: boots
- Single-page UI → Chart.js CDN: loads
- Single-page UI → Tailwind CSS CDN: loads
- Application coordinator → Portfolio state: reads and updates
- Application coordinator → Holdings: coordinates
- Application coordinator → Transaction records: coordinates
- Application coordinator → Trade calculator: uses calculations
- Application coordinator → Rebalancing planner: opens planning
- Application coordinator → Quote watch: opens quotes
- Application coordinator → Water-level analysis: opens analysis
- Portfolio state → Browser localStorage: persists
- Portfolio state → Holdings: stores holdings
- Portfolio state → Transaction records: stores history
- Trade calculator → Transaction records: calculates buy/sell results
- Trade calculator → Holdings: updates cost basis
- Rebalancing planner → Holdings: compares holdings
- Rebalancing planner → Portfolio state: reads targets
- Quote watch → Fugle quotes: fetches quotes
- Water-level analysis → FinMind history: fetches history
- Portfolio state → Fugle quotes: keeps API key local
- Portfolio state → FinMind history: keeps token local
