Portfolio Optimizer (version 1)
Status (2026-11-09): phases 1 and 2 (hedged versions) built, not deployed. Page TBS > Portfolio Optimizer (ADMIN only: sidebar link hidden, route minRank=ADMIN, API router gated with require_min_rank('ADMIN')) (pages/TbsPortfolio.jsx), code backend/tbs/portfolio.py (maths), portfolio_run.py (search), app/api/tbs_portfolio.py (job + API), results in files under backend/tmp/tbs_portfolios (env TBS_PORTFOLIO_PATH).
What it does
- Creates strategies: families of plain sell, combined premium SL and move SL to cost short straddles / strangles (ATM, OTM 1 to 3) over entry time x SL grids, by time slot, using the real engine through
tbs/sweep.run_sweep. - Prices every cell day by day after slippage, brokerage and statutory charges (
portfolio.sold_leg_net, same formulas assweep.leg_net; brokerage per order or per lot is added when the lots are known). - Pass 1 runs a coarse grid, pass 2 refines only around the best coarse cells (effort level caps the backtests: about 6k / 25k / 100k).
- Finds zones: connected plateaus of cells whose smoothed net is positive, win % reaches the minimum and which traded enough days.
- Picks integer lots for the best Calmar ratio (annual return / max drawdown) (
portfolio.select_lots): one lot at a time, always the move with the best ratio, subject to the win % floor, diversification caps (lots per entry hour, per zone, per strategy; Off / Moderate / Strong), max strategies (at most 20, the basket run limit), max lots and the margin cap. Cards: Best Calmar (stops when no lot improves the ratio), then Light / Standard / Full (40% / 70% / 100% of the lots the margin allows, each at its best ratio). An optional minimum return filters the cards. - Margin is the Quick estimate (flat % of sold notional, the user's rate from Basket settings) counted by time of day: a strategy that has passed its slot exit time frees its margin.
- The last N % of dates are not used to choose; the page shows the mix on them.
- Create basket saves the members as library strategies (
PF25L Z1-01 NF 11:07 SL25) and a basket whose multipliers are the lots.
Hedged versions (phase 2)
hedge: off / both (with and without) / on (only hedged), wing premium in rupees. A hedged strategy also buys, on each side, the out-of-the-money option whose premium is the largest one not above the wing premium (sweep.make_strategy(hedge=), strike rule premium_lte); the sweep keeps 8 prices per cell (sold CE / PE, wing CE / PE). Wings are priced with portfolio.bought_leg_net (STT on the sell premium, stamp on the buy premium) and add 2 orders per wing. Margin of a hedged strategy is an estimate (hedge_margin_factor, default 0.6 of the unhedged, from broker screenshots); the exact figure comes from the basket run. With both the result has a with / without / mixed comparison table.
Walk-forward windows, exact SPAN margin, compare, export, weekday mode
- Walk-forward windows: the whole period is cut into
wf_windows(default 6) windows and the chosen mix is shown window by window (net, win %, drawdown; labelled chosen on / mixed / unseen), with "windows profitable". The mix is not re-chosen per window (the pool was qualified on the optimised dates), so this shows stability, not a re-fitted walk-forward. - Exact SPAN margin:
spanmargin.PortfolioSpanplaces every strategy's legs on the latest archived NSE SPAN file (steps from ATM, or by premium from the file's own option prices, hedge wings included), nets the positions and takes the margin at the busiest moment of the day. NIFTY only (other indexes are counted as not covered); it is a typical-day figure, shown next to the Quick estimate on each portfolio. - Compare / export: compare any two portfolios of a result (equity overlay and a figures table); Excel (
portfolio_export.build_xlsx) writes summary, members, daily P&L, walk-forward windows, correlation, margin by time, zones and settings. - Run each weekday separately: five searches (one per ticked weekday) are queued behind each other; each day's result opens on its own tab and Create a basket for every day saves the Best Calmar portfolio of each as a basket (strategy names carry a search / day tag so baskets never overwrite each other).
- Re-run to latest: a saved search is re-run with both dates moved forward to the latest data day.
Range breakout, wait and trade, re-entry
Engine families (portfolio_engine.py): the sweep price table keeps one entry and one exit price per leg, which cannot describe a re-entry or an entry decided inside the day, so these run the engine per cell and return the net rupees of every (entry, SL) cell per day from the trades' own fills (slippage in the fills, statutory charges added with the same formulas; brokerage later). Range breakout: legs enter when their own premium breaks the range that starts at the slot's entry time and lasts the given minutes. Wait and trade: each sold leg enters only after its premium has risen by the given %. Re-entry: a leg is re-executed at once after its SL, up to the given count (partial square off). Each value is its own variant with its own bucket in the pool; no hedge for these types; margin is counted from the slot's entry time (they enter later, so it is conservative). Create basket builds the matching library strategy.
Reducing overfitting: three-way split and plateau selection
- Train / validation / hidden final test. The dates are cut into train (everything is chosen on it), validation (
holdout_pct, unseen, shown) and a hidden final test (final_pct, the last dates). Until the user presses Reveal the API (portfolio_run.sanitize) removes the final test's dates, its curve and every figure that includes it (final,all); the walk-forward windows never include it.POST /api/tbs/portfolio-opt/{id}/revealis one way (stored in the meta); exports are sanitized the same way. Rule of use: compare settings on the validation segment, decide, then reveal the final test once; a search tuned after revealing is no longer honest. - Plateau, consistency and t-statistic. A candidate is judged on the plateau around it (
portfolio.neighbourhood_mean: the mean daily P&L of the cell and its 3x3 neighbours), not on the cell alone: it must be profitable in at leastcons_minofcons_windowsequal periods of the train dates and the plateau's daily P&L must have a t-statistic of at leastmin_tstat; candidates are ranked by that t-statistic, so the centre of a good region outranks a lone lucky peak. The pool summary shows how many were dropped as inconsistent. - Short samples are noisy: a validation or final segment of 25 days says little. Use a long history (two or three years) when the data has it.
Saved searches
Save search stores a name, every setting of the page (dates included), the result it gave (result_id) and the headline figures of its best portfolio (Calmar, return, drawdown, unseen return) in presets_<user>.json next to the results (portfolio_store.save_preset). The Saved searches table loads the settings back, opens the result and sorts by best Calmar, so several searches can be compared and the best pulled. The version number (PORTFOLIO_VERSION) is stored with results and saved searches.
Day filters (phase 3)
Open versus the previous day range (inside / above PDH / below PDL) and India VIX bands (under 13, 13 to 18, over 18), from the index EOD rows (runview.day_attrs, portfolio_run.day_masks). The search runs once per family; each ticked filter is a mask over the days (portfolio.mask_days), so a filter costs no extra backtests. Every filter gets its own zones, candidate pool and portfolios, because a basket report applies one filter to all its strategies. Create basket saves the strategies and puts the filter in the basket notes and name; the same filter must be set on the basket report to reproduce the search (a library strategy has no day filter).
Premium-based strikes (phase 4)
Besides steps from ATM, a family can pick its strikes by premium: closest to, at most, or at least a rupee premium (premium_strikes, premium_rule; engine rules closest_premium / premium_lte / premium_gte). A fixed premium keeps the risk per leg steadier across low and high VIX days than a fixed distance from ATM. The family carries otm = None and prem / prem_rule; labels read Plain sell premium ~Rs100; Create basket builds the same strike rule (portfolio_run.strike_rule). The page takes a premium range (from, to, step: e.g. 30 to 300 step 10 is 28 values, at most 60): pass 1 tries about six of them (prem_stride, coarse_premiums), pass 2 refines the premiums between the coarse ones around the best (each premium once). Margin uses the ATM rate (the strike moves from day to day): an estimate until the basket is run with Exact margin. In the refine pass every family gets an equal share of what is left of the budget (a family too big for its share is refined only around its best cell), and families that were not refined still show their coarse zones.
Candidate pool, buckets and correlation (phase 5)
After the search, the candidates are the best setups of four buckets: plain sell (fixed strikes), move SL to cost (fixed strikes), premium strikes and combined SL / target. A cell qualifies when it traded enough days, is profitable after costs, reaches the win % minimum and its 3x3 neighbourhood is positive. Within a bucket they are ranked by the cell's own return per drawdown and thinned to one per neighbourhood (portfolio_run.build), keeping the best pool_per_bucket (default 100). The pool is then taken in round-robin order over (bucket, time slot) and a setup is skipped when its daily P&L correlates above max_corr (default 0.6; 1 = off) with one already taken, measured on the optimised dates only. The Calmar selector (lots, slot / hour / bucket / strike / type caps, margin by time of day) then runs on this low-correlation pool; every portfolio reports the average and highest correlation and its matrix. The combined SL type has its own SL range (combined_sl_from/to/step), because 10 to 60 % of the combined premium loses money. The number of qualifying setups depends on the data and on the win % minimum: a short or gappy history gives far fewer than 100 per bucket.
Strike and mix caps
Strikes ITM 2 to OTM 3. SPAN scales the Quick margin by strike (spanmargin.straddle_rates). Diversification caps lots per entry hour or time slot, per zone, per strike and per strategy type; every family keeps its best two zones so one strike cannot crowd out the others.
Limits (also stated on the page)
Straddle / strangle sellers only; drawdown on daily closes; basket-level SL / target not modelled; Quick margin (SL exits before the slot exit are ignored: conservative); SENSEX margin is the NIFTY-equivalent estimate. The previous-day filters and VIX bands are supported; a mix of different day filters in one basket is not (one filter per basket report) (the plan is in the developer todo list).
Settings
One popup (gear) shared with the basket page, stored per user (usr_tbs_setting, API /api/tbs/basket-settings): slippage % and where it applies, brokerage per order or per lot, statutory rates, Quick margin rate (with Calculate from latest NSE SPAN).