backtrader.tradingcal module

Trading Calendar Module - Market calendar and session handling.

This module provides trading calendar functionality for handling market sessions, holidays, and trading days. It supports custom calendars and pandas market calendar integration.

Classes:

TradingCalendarBase: Base class for trading calendars. TradingCalendar: Standard trading calendar implementation. PandasMarketCalendar: Wrapper for pandas_market_cal calendars.

Constants:

MONDAY-SUNDAY: Weekday constants. WEEKEND: Weekend days (Saturday, Sunday). ONEDAY: Timedelta of one day.

Example

Using a trading calendar: >>> cal = bt.TradingCalendar() >>> next_day = cal.nextday(datetime.date(2020, 1, 1))

class backtrader.tradingcal.TradingCalendarBase[source]

Bases: ParameterizedBase

Base class for trading calendars.

Provides methods for calculating trading days, session times, and determining if a day is the last trading day of a week or month.

_nextday(day)[source]

Returns next trading day and isocalendar components.

schedule(day)[source]

Returns opening and closing times for a day.

nextday(day)[source]

Returns next trading day.

last_weekday(day)[source]

Returns True if day is last trading day of week.

last_monthday(day)[source]

Returns True if day is last trading day of month.

schedule(day)[source]

Returns a tuple with the opening and closing times (datetime.time) for the given date (datetime/date instance)

nextday(day)[source]

Returns the next trading day (datetime/date instance) after day (datetime/date instance)

nextday_week(day)[source]

Returns the iso week number of the next trading day, given a day (datetime/date) instance

last_weekday(day)[source]

Returns True if the given day (datetime/date) instance is the last trading day of this week

last_monthday(day)[source]

Returns True if the given day (datetime/date) instance is the last trading day of this month

last_yearday(day)[source]

Returns True if the given day (datetime/date) instance is the last trading day of this month

class backtrader.tradingcal.TradingCalendar[source]

Bases: TradingCalendarBase

Wrapper of pandas_market_calendars for a trading calendar. The package pandas_market_calendar must be installed # In this class, it seems that pandas_market_calendar is not strictly required Params:

  • open (default time.min)

    Regular start of the session

    # open, trading day start time, default is minimum time

  • close (default time.max)

    Regular end of the session # close, trading day end time, default is maximum time

  • holidays (default [])

    List of non-trading days (datetime.datetime instances)

    # holidays, holidays, list of datetime times

  • earlydays (default [])

    List of tuples determining the date and opening/closing times of days which do not conform to the regular trading hours when each tuple has (datetime.datetime, datetime.time, datetime.time) # earlydays, trading days with non-standard trading start and end times

  • offdays (default ISOWEEKEND)

    A list of weekdays in ISO format (Monday: 1 -> Sunday: 7) in which the market doesn’t trade. This is usually Saturday and Sunday and hence the default

    # offdays, non-trading dates from Monday to Sunday, usually Saturday and Sunday

params = (('open', datetime.time(0, 0)), ('close', datetime.time(23, 59, 59, 999990)), ('holidays', []), ('earlydays', []), ('offdays', [6, 7]))
__init__(**kwargs)[source]

Initialize the TradingCalendar.

Parameters:

**kwargs – Keyword arguments for calendar parameters.

schedule(day, tz=None)[source]

Returns the opening and closing times for the given day. If the method is called, the assumption is that day is an actual trading day

The return value is a tuple with 2 components: opentime, closetime

class backtrader.tradingcal.PandasMarketCalendar[source]

Bases: TradingCalendarBase

Wrapper of pandas_market_calendars for a trading calendar. The package pandas_market_calendar must be installed # pandas_market_calendar must be installed Params:

  • calendar (default None)

    The param calendar accepts the following:

    • string: the name of one of the calendars supported, for example, NYSE. The wrapper will attempt to get a calendar instance

    • Calendar instance: as returned by get_calendar('NYSE')

    # calendar information, can be string or calendar instance

  • cachesize (default 365)

    Number of days to cache in advance for lookup

    # How many dates to cache in advance for convenient lookup

params = (('calendar', None), ('cachesize', 365))
__init__(**kwargs)[source]

Initialize the PandasMarketCalendar.

Parameters:

**kwargs – Keyword arguments for calendar parameters.

schedule(day, tz=None)[source]

Returns the opening and closing times for the given day. If the method is called, the assumption is that day is an actual trading day

The return value is a tuple with 2 components: opentime, closetime