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.
示例
Using a trading calendar: >>> cal = bt.TradingCalendar() >>> next_day = cal.nextday(datetime.date(2020, 1, 1))
- class backtrader.tradingcal.TradingCalendarBase[源代码]¶
-
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.
- schedule(day)[源代码]¶
Returns a tuple with the opening and closing times (
datetime.time) for the givendate(datetime/dateinstance)
- nextday(day)[源代码]¶
Returns the next trading day (datetime/date instance) after
day(datetime/date instance)
- nextday_week(day)[源代码]¶
Returns the iso week number of the next trading day, given a
day(datetime/date) instance
- last_weekday(day)[源代码]¶
Returns
Trueif the givenday(datetime/date) instance is the last trading day of this week
- class backtrader.tradingcal.TradingCalendar[源代码]¶
-
Wrapper of
pandas_market_calendarsfor a trading calendar. The packagepandas_market_calendarmust be installed # In this class, it seems that pandas_market_calendar is not strictly required Params:open(defaulttime.min)Regular start of the session
# open, trading day start time, default is minimum time
close(defaulttime.max)Regular end of the session # close, trading day end time, default is maximum time
holidays(default[])List of non-trading days (
datetime.datetimeinstances)# 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 timesoffdays(defaultISOWEEKEND)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]))¶
- class backtrader.tradingcal.PandasMarketCalendar[源代码]¶
-
Wrapper of
pandas_market_calendarsfor a trading calendar. The packagepandas_market_calendarmust be installed # pandas_market_calendar must be installed Params:calendar(defaultNone)The param
calendaraccepts 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(default365)Number of days to cache in advance for lookup
# How many dates to cache in advance for convenient lookup
参见
- params = (('calendar', None), ('cachesize', 365))¶