Source: 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:
  1. Schedule ordersScheduleOrderController.startScheduleOrder(userId, indexName)
  2. Recurring buy — based on activeSegment:
  1. Calls RecurringOrderController.handleRecurringOrder with recurringStatus: true

Stopping order services

stopOrderServicesForExchanges:
  1. Calls resetSymbolsForExchanges which for each active setting:
    • Stops intervals: scheduleOrder_{index}, recurringOrder_{index}_CALL, recurringOrder_{index}_PUT
    • Sets recurringStatus: false on the latest RecurringOrder record
  2. 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.

Market data auto-login

initializeMarketDataIfNeeded:
  1. Calls ensureMarketDataRunning() — returns if streaming is active
  2. Skips if 6 AM Zerodha refresh is in progress
  3. Attempts autoZerodhaLogin() as fallback

Exchange hours

Market open/close times are defined in src/utils/marketHours.ts:

Integration with app.ts