trading_agent.scheduling.market_calendar
¶
Which days the Indian exchanges trade, read from UBI's own exchange calendar files.
UBI keeps one YAML file a year, generated from the exchanges' own publications (NSE, BSE, MCX and NCDEX), listing each holiday by exchange and calendar and each special session such as Muhurat trading. Reading the same files keeps trading-agent and UBI in agreement about every holiday.
A calendar is a set of segments that close together: equity (cash, indices and equity derivatives), currency (currency derivatives, which also close on bank holidays) and commodity. A commodity holiday can close only the morning or only the evening session.
Typical usage example:
calendar = MarketCalendar(Path('.../calendars')) calendar.is_trading_day(datetime.date(2026, 10, 20)) # False: Dussehra
Closure
dataclass
¶
One holiday.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The holiday's name, as the exchange publishes it. |
closed |
str
|
"all", or "morning" or "evening" for a commodity calendar. |
SpecialSession
dataclass
¶
A session on a day that would otherwise be closed, such as Muhurat trading.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The session's name. |
opens |
time
|
When it opens, India time. |
closes |
time
|
When it closes, India time. |
MarketCalendar
¶
The exchanges' holidays and special sessions.
Reads every year's file in the folder.
A missing folder or unreadable file is logged; the calendar then knows weekends only, and the doctor reports it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calendar_directory
|
Path | None
|
UBI's calendars folder, or None to know weekends only. |
required |
Source code in src/trading_agent/scheduling/market_calendar.py
calendar_for
¶
The calendar an instrument's segment follows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exchange
|
str
|
The exchange, such as nse or mcx. |
required |
segment
|
str
|
UBI's segment, such as nse_equities or mcx_commodities. |
required |
Returns:
| Type | Description |
|---|---|
str
|
"equity", "currency" or "commodity". |
Source code in src/trading_agent/scheduling/market_calendar.py
closure
¶
closure(day, exchange='nse', calendar=EQUITY)
The holiday on a day, if there is one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
date
|
The date. |
required |
exchange
|
str
|
The exchange. |
'nse'
|
calendar
|
str
|
"equity", "currency" or "commodity". |
EQUITY
|
Returns:
| Type | Description |
|---|---|
Closure | None
|
The holiday, or None. |
Source code in src/trading_agent/scheduling/market_calendar.py
special_session
¶
special_session(day, exchange='nse', calendar=EQUITY)
The special session on a day, if there is one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
date
|
The date. |
required |
exchange
|
str
|
The exchange. |
'nse'
|
calendar
|
str
|
"equity", "currency" or "commodity". |
EQUITY
|
Returns:
| Type | Description |
|---|---|
SpecialSession | None
|
The session, or None. |
Source code in src/trading_agent/scheduling/market_calendar.py
is_trading_day
¶
is_trading_day(day, exchange='nse', calendar=EQUITY)
Says whether any part of a day trades.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
date
|
The date. |
required |
exchange
|
str
|
The exchange. |
'nse'
|
calendar
|
str
|
"equity", "currency" or "commodity". |
EQUITY
|
Returns:
| Type | Description |
|---|---|
bool
|
True on a weekday that is not a full holiday, or on a day with a special session. |
Source code in src/trading_agent/scheduling/market_calendar.py
next_trading_day
¶
next_trading_day(after, exchange='nse', calendar=EQUITY)
The first trading day after a date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
after
|
date
|
The date to count from, not itself included. |
required |
exchange
|
str
|
The exchange. |
'nse'
|
calendar
|
str
|
"equity", "currency" or "commodity". |
EQUITY
|
Returns:
| Type | Description |
|---|---|
date
|
The next trading day. |
Raises:
| Type | Description |
|---|---|
ValueError
|
None of the next thirty days trades, which means the calendar is wrong. |