Asset Allocation
Page /asset-allocation (header link). Backtests a Nifty / Gold / Bonds / Cash allocation against inflation on real data,
monthly from the Dec-1994 close to the last complete month, plus a daily Backtester (Buy & Hold, Rebalancing, plan comparison).
Code: backend/assetalloc/ (calculation), backend/app/api/asset_allocation.py (API), frontend/src/pages/AssetAllocation.jsx
and frontend/src/components/assetalloc/ (UI). It replaced the synthetic-data TAA Regime Dashboard.
Access tiers (teudb features)
| Tier | Feature | What |
|---|---|---|
| Everyone (anonymous included) | aa | Monthly page, predefined plans, sliders, charts, year-by-year table |
| Registered (FREE and above) | aa_plans | Save / open / edit / delete / duplicate own plans, Excel and PDF downloads |
| Power (POWER and above) | aa_backtester | Rebalance tab, Comparison chart (daily Backtester), plan import / export |
Grants live in usr_cat_feature (alembic_user migration c63e9a2b7d14). The router is split in three (public_router,
plans_router, backtester_router) and each is gated with require_feature in app/main.py. Feature claims travel in the access
token, so a signed-in user sees a new grant after the next token refresh or sign-in.
Data (read in-process, assetalloc/data.py)
| Series | Source |
|---|---|
| Nifty 50, Midcap 150, Smallcap 250 daily close | eq_store.get_eod(..., adjust="price") |
| MCX Gold spot (INR/10g) from 2005-06 | mcx_spot_store, symbol GOLD (looked up by name) |
| GOLDINR 1994-12 to 2005-05 | scr_global_price, name GOLDINR |
| SBI 1-2yr FD rate, India CPI (annual) | te_asset / te_price: in_fixed_deposit_rate, CPI_India_Annual |
| NIFTY Composite G-sec Index from 2015-10 | eq_store, masid 987121 (INDIA_GSEC_COMPOSITE) |
Instruments are found by name, never by a fixed masid (masids differ between environments). Data is cached in memory and
reloaded when a new day starts. Rules (month-end close, Bonds booked once a year, gold = MCX / 10 x 1.03 GST, 2026 inflation repeats 2025,
Budget-day 1997 fix, frozen MCX 2020 days replaced by a GOLDINR-based estimate) are in the docstrings of assetalloc/backtest.py,
daily.py and engine.py.
Saved plans
Table usr_allocation_plan in teudb: planid, usrid (FK, cascade), name (unique per user, case-insensitive), weights JSONB, timestamps.
At most 100 plans per user. A plan's API id is u<planid>. There are no predefined plans: a visitor has none, a user has the ones they saved.
The weights column holds the user's WHOLE configuration as one JSON document (validated by plans.clean_config, no migration needed):
{"version": 2, "mode": "basic" | "advanced",
"weights": {...}, "dividends": true|false, "etf": true|false,
"rebalance": {"on": true|false, "tolerance": 5},
"capital": 200000, "start": "2018-05", "end": "2026-08",
"mapping": {"LARGE": {"index": ..., "tri": ..., "etf": ...}, ...}} (advanced only: the Settings table in use)
Basic weights are NIFTY, GOLD, BOND, CASH; Advanced weights are LARGE, MID, SMALL, SP500, GOLD, SILVER, BONDS, CASH. etf is an Advanced setting.
A row saved before this (a flat {"NIFTY": 50, ...} dict) reads as a Basic plan with default settings (plans.config_of). Open plan loads every field
(it switches the version, ticks the boxes, sets capital and period); Save as plan writes them.
API (/api/asset-allocation)
Public: GET /range, POST /backtest, GET /assets, GET /allocations (your plans), GET /allocations/{id}.
Registered: POST /allocations (body {name, config}; old {name, weights} still works), PUT / DELETE /allocations/{id}, POST /allocations/{id}/duplicate.
Power: POST /advanced/compare (saved plans, Basic and Advanced, side by side), GET /assets/ranges, POST /portfolio-backtest (Basic plans only), GET /export, POST /import; POST /backtest with rebalance: true.
Deploy
cd backend && alembic -c alembic_user.ini upgrade head # usr_allocation_plan + aa_plans / aa_backtester grants
pull.sh rebuilds the frontend (new npm dependencies: write-excel-file, jspdf, jspdf-autotable; run npm install in frontend/).
Basic and Advanced versions, Include dividends, Settings
- Basic: equity = Nifty 50, gold = gold (the monthly page above). Advanced (registered users): equity split into large / mid / small cap and S&P 500, gold split into gold and silver, bonds and cash; code
assetalloc/advanced.py, APIPOST /api/asset-allocation/advanced/backtest,GET /advanced/range; UIcomponents/assetalloc/AdvancedView.jsx. - Include dividends (both versions): ticked = returns from the TRI index (Basic:
NIFTY TRIinstead of the Nifty price index; Advanced: the TRI mapped to each sleeve, for the whole window). Unticked in Advanced = ETF prices, with the mapped index price spliced in before the ETF existed (scaled to join without a jump; the page lists which assets use index data before which month). Gold and silver have no TRI, so the box does not change them. - Settings (per registered user, teudb
usr_allocation_setting; visitors see the defaults read-only): for each sleeve the index, its TRI index and its ETF; defaults = the reference table inassetalloc/mapping.py(Nifty 50 -> NIFTY TRI -> NIFTYBEES, Nifty Midcap 100 -> MOM100, Nifty Smallcap 250 -> SMALLCAP, S&P 500 -> MASPTOP50, Gold -> GOLDBEES, Silver -> SILVERBEES). APIGET/PUT/DELETE /settings,GET /settings/options,GET /settings/etf. - Data: TRI series are
scr_mastertyp=12 (python -u -m nseeod.index_tri sync --only "Nifty Next 50,Nifty 100,Nifty Midcap 100,NIFTY SMLCAP 250"loads the ones the table needs; NSE lists the small-cap index asNIFTY SMLCAP 250). S&P 500 comes from the global table in USD x USDINR; silver = MCX silver spot, SILVERINR before it. Instruments are looked up by name. - A series with a hole in its history (e.g. Nifty Midcap 100 before 2018-05) shortens the common window: buy and hold cannot jump over a gap, so only the last unbroken run is used.
- Deploy:
alembic -c alembic_user.ini upgrade head(addsusr_allocation_setting) and the TRI sync above.
Advanced: ETF prices, group sliders, Rebalance and Comparison (Nov 2026)
- Return source of each part (header boxes, Advanced): ETF prices unticked = the index price itself (full history, so the common window is long); ticked = the ETF close with the index spliced in before the ETF existed. Include dividends ticked = the TRI index for equity (it wins over ETF prices for parts that have a TRI; gold and silver have none, so they follow the ETF box). Basic has no ETF box (Nifty 50 price index, or NIFTY TRI).
- Sliders are group first (
components/assetalloc/advAlloc.js): Equity and Metals have a total (% of the portfolio) and, inside, each part is a share of its group, so a whole group can sit in one part (65% equity = 65% small cap). Moving a part rescales its group mates; Bonds and Cash as before. - Rebalance (header box, both versions, power users): every December, only if a part drifted more than the tolerance. It changes the main results
and is saved in the plan. The Rebalance tab shows with and without rebalancing. In Advanced it tells, for every part, what to buy and sell in rupees and,
with ETF prices ticked, ETF units held, units to buy or sell and units after (units use that month's ETF close;
advanced.simulate(prices=)), plus the ETF units held at the last month on the main page. - Comparison tab (both versions): Basic keeps the daily Backtester (Basic plans only). Advanced compares any saved plan with
POST /advanced/compare; each plan runs with its own mode, dividends / ETF boxes, rebalance setting and mapping, over the months all plans have data for (advanced.compare). A Basic plan runs there as Large cap = Nifty 50, Gold, Bonds, Cash on the index price. - Tests:
tests/test_asset_allocation.py(plan config),tests/test_asset_allocation_advanced.py(index basis, ETF units, compare).