67 Commits

Author SHA1 Message Date
Thigazhezhilan J
298d245048 fix: surface SIP_NO_FILL warnings and prevent silent fund failures
- execution.py: dual-write SIP_NO_FILL and SIP_PARTIAL to engine_event
  so the strategy summary can surface them to users
- execution.py: emit SIP_NO_FILL event (with cash/required amounts) on
  the paper path instead of silently returning when funds are insufficient
- strategy_service.py: improve insufficient_funds message to show exact
  shortfall and reassure user that next SIP will auto-execute when funded
- strategy_service.py: clear SIP_NO_FILL warning after a successful
  SIP_TRIGGERED so it does not persist after funds are added
- runner.py: always write PRICE_FETCH_ERROR and HISTORY_LOAD_ERROR to
  engine_event regardless of ENGINE_DEBUG flag
- db.py (backend + engine): raise default pool sizes to 20/50 max
  connections to handle 100 concurrent users without pool exhaustion

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:53 +05:30
Thigazhezhilan J
98ef7701d1 fix: auto-refresh expired broker session on server startup
Every time pm2 restarts, the Zerodha token may be in EXPIRED state
(knocked out by the engine between midnight and 6:05 AM auto-login).
Now on startup we check each auto-login user's broker auth_state and
immediately re-login anyone who is expired/disconnected, so the broker
shows as connected the moment the user opens the website.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 09:34:17 +05:30
Thigazhezhilan J
10e262231f feat: make paper and live trading fully independent
- start_strategy filters running check by mode so starting LIVE
  won't clash with an active PAPER run and vice versa
- stop_strategy and resume_strategy accept optional mode param
  so each tab stops/resumes only its own run
- paper_broker_service scopes all run lookups to mode=PAPER
- paper_mtm scopes run lookup to mode=PAPER
- routers/strategy exposes ?mode= query param on /stop and /resume
- run_service get_active_run_id and get_running_run_id already
  support mode filtering (added in previous session)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-03 22:11:29 +05:30
Thigazhezhilan J
d4e06d9211 fix: show JUNIORBEES.NS position in paper MTM by fetching unknown LTP
market.py's LTP cache only has NIFTYBEES.NS and GOLDBEES.NS. For Alpha
Shield, JUNIORBEES.NS was missing from the map so paper_mtm.py was
silently dropping it. Now falls back to fetch_live_price for any symbol
not in the cached map.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-03 10:25:12 +05:30
Thigazhezhilan J
6215910c9c fix: set auth_state to VALID (not CONNECTED) after auto-login reconnect
Frontend checks authState === 'VALID' to clear the session-expired
banner. Using 'CONNECTED' left the banner stuck even after a successful
token refresh.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-02 22:32:47 +05:30
Thigazhezhilan J
5dce19c7d8 Persist strategy change to DB when restarting an existing run
When start_strategy is called with a new strategy on an active run,
update strategy_config in DB before building engine config so the
new strategy is used both now and on future resumes/restarts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 10:47:14 +05:30
Thigazhezhilan J
dd2b9480d3 Fix strategy_name from request taking precedence over stale DB config
When restarting an existing run with a different strategy, the saved
DB config was winning over the user's selection. req.strategy_name
now takes priority so switching strategies works correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 10:41:57 +05:30
Thigazhezhilan J
b06f7d4ffa Fix strategy name fallback to use registry key instead of display name
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-28 18:33:52 +05:30
Thigazhezhilan J
18d07c1910 Remove debug logging from auto-login (feature working)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:58:29 +05:30
Thigazhezhilan J
bec31b9f9d Add OAuth completion step after twofa
After twofa verifies identity, re-visit connect/login as authenticated user.
Zerodha then redirects to the registered callback URL with request_token.
This mirrors what the browser JS does to complete the OAuth flow.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:55:19 +05:30
Thigazhezhilan J
9d1126b84d Fix auto-login: pass sess_id to twofa to link request to OAuth app
The sess_id from the connect/login redirect is Zerodha's OAuth session
identifier. Without it in the twofa POST, Zerodha returns profile:{}
(regular web login) instead of redirect_url with request_token.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:49:28 +05:30
Thigazhezhilan J
e447a39912 Fix twofa returning profile instead of redirect_url
Following the connect/login redirect chain overwrites kf_session with a
plain web session, stripping the OAuth context. Stop at the first 302
so kf_session retains the api_key OAuth context through the twofa step.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:39:18 +05:30
Thigazhezhilan J
dc663a129c Fix auto-login: remove Referer header, add Accept/Origin headers
Referer on POST was causing Zerodha to reject login with Invalid username.
Add Accept, Accept-Language, Origin headers for proper browser-like requests.
Keep redirect_url body check for twofa. Add login step debug logging.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 21:28:09 +05:30
Thigazhezhilan J
4922ea69e8 Fix auto-login OAuth context not being established
Root cause: X-Kite-Version header on web login endpoints caused Zerodha
to return plain profile response instead of OAuth redirect_url.

Changes:
- Remove X-Kite-Version from session headers (only valid for Kite Connect API)
- Use allow_redirects=False on connect/login GET to preserve OAuth cookie
- Add Referer header to login/twofa POSTs
- Check data.redirect_url in twofa JSON body (modern Zerodha SPA behavior)
- Keep Location header fallback for legacy behavior

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 21:42:25 +05:30
Thigazhezhilan J
a1b19b7431 Add debug logging to auto-login twofa step
Prints twofa response status, Location header, and body so we can see
exactly what Zerodha returns after TOTP submission.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 21:30:03 +05:30
Thigazhezhilan J
df137afcbd Fix auto-login: initialize Zerodha OAuth session before credentials
Without first GETting the connect/login URL with the api_key,
Zerodha doesn't know which app is logging in and never returns
a request_token after TOTP — causing the redirect loop to fail.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-25 20:59:18 +05:30
Thigazhezhilan J
02922adc9a Fix reconnect broker: share session cookie across subdomains
Frontend (app.quantfortune.com) fetches API (api.quantfortune.com).
With SameSite=Lax the browser won't send the cookie on cross-origin
fetch calls, so the server sees no session and the request fails.

Adding COOKIE_DOMAIN=.quantfortune.com makes the cookie valid for
all subdomains. Mohan needs to add this to .env and restart.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-20 23:31:46 +05:30
Thigazhezhilan J
74d8db1de0 Revert "Fix cross-subdomain cookie: add COOKIE_DOMAIN support"
This reverts commit c85fae2d07e04dd3b62ffb7d665de763f21de747.
2026-05-20 23:26:04 +05:30
Thigazhezhilan J
c85fae2d07 Fix cross-subdomain cookie: add COOKIE_DOMAIN support
Session cookie was scoped to api.quantfortune.com only.
Setting COOKIE_DOMAIN=.quantfortune.com in .env makes the
cookie shared across all subdomains so app.quantfortune.com
can authenticate against api.quantfortune.com.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-20 23:23:30 +05:30
2e0ba0060d Update backend/ecosystem.config.js 2026-05-15 18:13:25 +00:00
root
dd931a3d14 Chanegs by Mohan from the server fro the fixes new 2026-05-11 19:28:43 +00:00
root
6c3f2f8bcf Chanegs by Mohan from the server fro the fixes 2026-05-11 19:26:55 +00:00
Your Name
9c7c400b23 Add www versions to CORS origins and env config 2026-05-09 16:30:23 +05:30
Thigazhezhilan J
ae3a335ea1 Harden server restart: remove import-time crashes, centralise secret validation
- auth_service.py: RESET_OTP_SECRET no longer crashes at import; read lazily
  inside _hash_otp() so the module always loads cleanly
- main.py: _validate_runtime_secrets() now checks both BROKER_TOKEN_KEY and
  RESET_OTP_SECRET together, reporting all missing vars in one clear message
- .env.example: documents every required/optional env var with generation commands

With load_dotenv() + .env file, all secrets survive pm2 restarts automatically.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 12:58:22 +05:30
Thigazhezhilan J
0a7e038be9 Load .env file at startup and validate BROKER_TOKEN_KEY format
Adds python-dotenv so secrets survive pm2 restarts without relying on
PM2 env vars. Validates the Fernet key format at startup so a bad/missing
key fails immediately with a clear message rather than crashing mid-request.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 12:49:33 +05:30
Thigazhezhilan J
6027dd3c6f Make auto-login optional at startup so backend always boots 2026-05-02 15:38:36 +05:30
Thigazhezhilan J
1b3be0437d Gracefully handle missing pyotp so backend always starts 2026-05-02 15:34:09 +05:30
Thigazhezhilan J
f5762de2b4 Fix startup crash by importing pyotp lazily inside function 2026-05-02 15:31:13 +05:30
Thigazhezhilan J
e1759b14fa Fix CORS to allow quantfortune.com alongside app.quantfortune.com 2026-05-02 15:23:24 +05:30
Thigazhezhilan J
94f175668a Add automated daily Zerodha token refresh (auto-login)
- New auto_login_service.py: stores encrypted credentials (login ID,
  password, TOTP secret), performs headless Zerodha login via pyotp,
  and refreshes the session daily at 6:05 AM IST via background thread
- New auto_login router: setup, status, remove, and manual trigger endpoints
- Scheduler started at app boot alongside existing daemons
- Added pyotp==2.9.0 dependency

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 12:47:21 +05:30
Thigazhezhilan J
1b14e7b23e Fix broker session showing connected after Zerodha token expiry
- Set connected=FALSE (was TRUE) when expiring broker session so the
  dashboard correctly reflects disconnected state
- Notify user by email when their Zerodha session expires so they know
  to reconnect before the next SIP execution

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-01 13:38:45 +05:30
Thigazhezhilan J
247a1c5107 Make engine event payloads JSON safe 2026-04-15 09:54:35 +05:30
Thigazhezhilan J
a90603f4f6 Fix broker reconnect dashboard redirect 2026-04-14 09:54:19 +05:30
Thigazhezhilan J
b5b759c5ed Fix Zerodha callback state propagation 2026-04-12 20:17:48 +05:30
root
988443f40f Made Changes from server by mohan 2026-04-10 00:08:23 +00:00
root
78aa2bf201 Made Changes from server by mohan 2026-04-10 00:06:30 +00:00
Thigazhezhilan J
d857f9d703 Stabilize admin API access and overview metrics 2026-04-10 00:37:07 +05:30
Thigazhezhilan J
9c171ba799 Fix broker reconciliation, health readiness, and live equity safety 2026-04-09 23:46:13 +05:30
Thigazhezhilan J
519addd78f Harden backend auth, execution safety, and market session logic 2026-04-08 22:02:24 +05:30
Thigazhezhilan J
8d1eaafebb Exclude sold positions from portfolio value 2026-04-07 09:51:52 +05:30
Thigazhezhilan J
652e5504c3 Normalize live position market values 2026-04-07 09:36:59 +05:30
Thigazhezhilan J
565de64459 Add live broker positions to portfolio API 2026-04-06 11:31:29 +05:30
Thigazhezhilan J
c41f6f2411 Reconcile stale running strategy state 2026-04-06 11:10:03 +05:30
Thigazhezhilan J
760eb6ea1a Fix Groww funds parsing 2026-04-05 20:39:17 +05:30
Thigazhezhilan J
28ec6c9a4d Add Groww live broker integration 2026-04-05 19:42:08 +05:30
Thigazhezhilan J
d5fa17b30d 2026-04-05 17:23:48 +05:30
Thigazhezhilan J
102f145553 Hide stale errors after strategy stop 2026-04-02 10:25:01 +05:30
Thigazhezhilan J
88ea093525 Fix live market time handling in strategy engine 2026-04-02 09:59:09 +05:30
Thigazhezhilan J
302edc5b79 Suppress stale market closed warnings 2026-04-01 21:07:40 +05:30
Thigazhezhilan J
1660b13778 Use run lifecycle for live strategy status 2026-04-01 10:12:03 +05:30