src/utils/marketSessionManager.ts
Orchestrates when recurring buy, trailing sell, and market data services start and stop based on exchange trading hours.
Core functions
Starting order services for a symbol
startOrderServicesForSymbol checks isExchangeMarketOpen(exchange) first. If closed, it skips.
For each active index setting:
- Schedule orders —
ScheduleOrderController.startScheduleOrder(userId, indexName) - Recurring buy — based on
activeSegment:
- Calls
RecurringOrderController.handleRecurringOrderwithrecurringStatus: true
Stopping order services
stopOrderServicesForExchanges:
- Calls
resetSymbolsForExchangeswhich for each active setting:- Stops intervals:
scheduleOrder_{index},recurringOrder_{index}_CALL,recurringOrder_{index}_PUT - Sets
recurringStatus: falseon the latestRecurringOrderrecord
- Stops intervals:
- Calls
syncTrailingSellInterval
Trailing sell interval sync
syncTrailingSellInterval:
- If any active index setting’s exchange is open →
ScheduleOrderController.startInterval() - Otherwise →
ScheduleOrderController.stopInterval()
Session close handlers
syncIndexSettings refreshes instrument chains and expiry maps from Kite. See Index settings lifecycle for preserved admin fields, expiry rollover, and post-close WebSocket resubscribe.
Market data auto-login
initializeMarketDataIfNeeded:
- Calls
ensureMarketDataRunning()— returns if streaming is active - Skips if 6 AM Zerodha refresh is in progress
- Attempts
autoZerodhaLogin()as fallback
Exchange hours
Market open/close times are defined insrc/utils/marketHours.ts: