User

\"\"\" tab_portfolio.py — SIA Portfolio tab. v6-SPLIT (29-Jul-2026): streamlit_app.py se extract kiya gaya (pure refactor, koi behavior-change nahi). \"\"\" import streamlit as st import stock_agent as sa from cached_fetch import cached_analyze, cached_

Asked by CNI Follower · 2 hrs ago · 14-09-2026

This Streamlit tab is already quite robust from a financial logic perspective. Below is a focused review of the key portfolio/Indian-tax-related pieces, and where you may want to tighten behaviour or user communication.

---

1. Price, P&L and portfolio aggregation

What you are doing:

- Unified view of:

- `portfolio.csv` (Qty > 0 = holdings, Qty = 0 often used as watchlist),

- FIFO-based holdings from `purchases/sales`,

- explicit `watchlist.csv`.

- Double-counting protection:

- If a symbol appears in both `portfolio.csv` and FIFO-open-lots, you keep only the `portfolio.csv` entry and show a warning.

- P&L per stock:

- Uses `pu.calculate_pnl(...)` with:

- `quantity` and `buy_price` from portfolio,

- OR weighted-average price and total open quantity from FIFO.

- Portfolio-level summary:

- Total invested, current value, total P&L and %.

Comments / suggestions:

- Conceptually sound for a retail Indian equity portfolio.

- Skipping FIFO-holdings when the same symbol exists in `portfolio.csv` is the right call to avoid overstating exposure.

- For clarity in the UI, consider adding a tiny note in the Portfolio Summary card like:

- “Note: If a stock exists both in `portfolio.csv` and ‘Add Purchase’ (FIFO), only `portfolio.csv` is counted.”

---

2. NSE/BSE price fallback and data quality

What you are doing:

- `_fetch_with_nse_bse_fallback`:

- If user passes plain symbol:

- Try `symbol + ".NS"` on yfinance-like backend.

- If empty/fails, try `symbol + ".BO"`.

- If suffix already present (`.NS`/`.BO`), pass through without modification.

- SME / BSE-only edge cases: if both fail, return `(None, None, None)` and treat as unavailable.

Comments / suggestions:

- This is a good, honest fallback approach for India:

- Avoids incorrectly forcing `.NS` for BSE-only counters (like CHOKSILA, RUDRAECO, etc.).

- You correctly re-use this same helper in:

- SIA verdict engine (`compute_decision_engine_v2`),

- Relative-strength heat-chart (`compute_relative_strength`),

ensuring consistent symbol mapping.

- For user communication: where you show “Heat-chart-abhi-available-nahi (NIFTY-data-fetch-fail-hua)”, you may also want to log or expose a “symbol-not-found / SME-format-unsupported” line in an expander so advanced users understand why a given stock is missing from the RS table.

---

3. LTCG/STCG holding period – tracker and FIFO engine

What you are doing:

- Uses `pu.is_ltcg(buy_date_str)` for:

- Direct holdings from `portfolio.csv` (when `pnl["is_holding"]` and a valid `Buy_Date`).

- Each open FIFO lot (per lot, per date).

- You explicitly mention:

- Calendar-month-based "more than 12 months" rule, consistent with Indian LTCG definition for listed equity.

- LTCG / STCG tracker:

- Shows per-row:

- Symbol, buy date, quantity,

- Holding days,

- Status (✅ LTCG / ⏳ STCG),

- Days left to LTCG.

Comments / suggestions:

- Conceptually correct if `pu.is_ltcg` really implements:

- Holding period > 12 months (not just ≥ 365 days), which you note it does.

- The per-lot LTCG status for FIFO holdings is very useful:

- Users can see that even within same stock, some tranches are LTCG, some STCG.

- Good that you clearly label: “Ye sirf holding-period ka indicator hai, tax-advice nahi.”

---

4. Realized gains (FIFO) and tax estimation – post Budget 2024 rules

What you are doing:

- Purchases / sales:

- Append via UI, persisted to local storage.

- FIFO allocation done by `pu.calculate_all_fifo_gains`.

- FY aggregation:

- `aggregate_gains_by_financial_year` collects LTCG / STCG by FY.

- Tax estimate:

- `estimate_capital_gains_tax()` uses:

- LTCG @ 12.5% with a per-FY exemption (`pu.LTCG_EXEMPTION_PER_FY`, likely ₹1.25 lakh),

- STCG @ flat 20%.

- Shows disclaimer string.

Alignment with current law:

- After the Finance Act 2024 / Budget 2024, for listed equity and equity mutual funds:

- LTCG rate: 12.5% on gains exceeding ₹1.25 lakh per year.

- STCG under section 111A: 20% (was 15% earlier). (pib.gov.in)

Your estimates (12.5% LTCG, 20% STCG and exemption) match the new regime for such assets and are reasonable as a rough guide, assuming:

- Only listed Indian equity/equity MF,

- No surcharges/cess, no old-regime grandfathering complications.

Suggestions:

- In the caption where you say:

> “LTCG @ 12.5% (₹X exemption already adjust hai) + STCG @ flat 20%.”

explicitly add:

- “Assumes listed Indian equity/equity-oriented MF and post‑Budget‑2024 rules; does not account for surcharge, cess, or grandfathering – consult a CA before filing.”

- For sales between 1 April 2024 and 22 July 2024, the transitional rates are slightly different; you may want a small footnote in the tax-disclaimer (even if the back-end doesn’t fully model it yet) to avoid misinterpretation by power users.

---

5. Sector concentration and portfolio risk summary

What you are doing:

- Sector concentration:

- Uses `fundamentals.sector` and invested value to compute % by sector.

- Highlights:

- ≥ 50% → Warning (high concentration risk),

- 35–50% → Info (dominant sector),

- < 35% → Success (reasonably diversified).

- Risk summary:

- Uses `entry_exit.stop_loss` from decision engine,

- Flags holdings where current price is within 5% of stop-loss.

Comments / suggestions:

- Thresholds (35% / 50%) are sensible for a quick concentration heuristic.

- Data-source caveat (“sector yfinance se aata hai, Unknown ho sakta hai”) is good; just remember:

- Indian smallcaps / SME often have misclassified or missing sectors, so treat this only as soft guidance.

- Stop-loss proximity using (Current − SL) / Current is fine, but for user clarity you may add:

- “Distance % is measured relative to current price.”

---

6. Corporate actions, trade journal, and misc. UX

Corporate actions:

- You aggregate upcoming corporate actions across the portfolio and display a consolidated calendar.

- Since you rely on external data (likely yfinance / similar), it’s correct to:

- Keep messaging modest (“koi upcoming corporate action nahi mila” vs. “none exist”).

Trade journal:

- Simple JSON-backed journal is a nice behavioural tool; no financial correctness issue.

- Consider adding an optional “Size / Risk” column in future so users can retrospectively link notes to capital-at-risk per trade.

---

7. Overall risk & expectation management

Across the tab you repeatedly mark:

- Projection is not prediction,

- Data-source limitations (PE data, SME tickers, sector gaps),

- Tax calculations as approximate, not advice.

This is exactly the right stance for a retail-facing India-equity tool.

---

If you want to tighten anything next, I would prioritise:

1. Making tax assumptions more explicit (asset type, post‑2024 rules, no surcharge/cess).

2. Slightly richer explanations around missing price/RS/sector/corp‑action data so advanced users know it’s a data gap, not a bug.

3. Optional export (CSV) for LTCG/STCG and FIFO tables so users can share with their CA.

If you have any further queries, please connect with us on 022-6290-10141 (Timings : 09.00 AM to 05.00 PM) or you can email us on info@cniinfoxchange.com