classTradingCalendarBase(with_metaclass(MetaParams, object)): def_nextday(self, day): ''' Returns the next trading day (datetime/date instance) after ``day`` (datetime/date instance) and the isocalendar components The return value is a tuple with 2 components: (nextday, (y, w, d)) where (y, w, d) ''' raise NotImplementedError
defschedule(self, day): ''' Returns a tuple with the opening and closing times (``datetime.time``) for the given ``date`` (``datetime/date`` instance) ''' raise NotImplementedError
classPandasMarketCalendar(TradingCalendarBase): ''' Wrapper of ``pandas_market_calendars`` for a trading calendar. The package ``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')`` - ``cachesize`` (default ``365``) Number of days to cache in advance for lookup See also: - https://github.com/rsheftel/pandas_market_calendars - http://pandas-market-calendars.readthedocs.io/ ''' params = ( ('calendar', None), # A pandas_market_calendars instance or exch name ('cachesize', 365), # Number of days to cache in advance )
classTradingCalendar(TradingCalendarBase): ''' Wrapper of ``pandas_market_calendars`` for a trading calendar. The package ``pandas_market_calendar`` must be installed Params: - ``open`` (default ``time.min``) Regular start of the session - ``close`` (default ``time.max``) Regular end of the session - ``holidays`` (default ``[]``) List of non-trading days (``datetime.datetime`` instances) - ``earlydays`` (default ``[]``) List of tuples determining the date and opening/closing times of days which do not conform to the regular trading hours where each tuple has (``datetime.datetime``, ``datetime.time``, ``datetime.time`` ) - ``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 ''' params = ( ('open', time.min), ('close', _time_max), ('holidays', []), # list of non trading days (date) ('earlydays', []), # list of tuples (date, opentime, closetime) ('offdays', ISOWEEKEND), # list of non trading (isoweekdays) )
使用方式
全局交易日历
通过Cerebro可以添加全局日历,该日历是所有数据源的默认日历,除非为数据源指定了日历:
1 2 3 4 5 6 7 8 9 10 11 12
defaddcalendar(self, cal): '''Adds a global trading calendar to the system. Individual data feeds may have separate calendars which override the global one ``cal`` can be an instance of ``TradingCalendar`` a string or an instance of ``pandas_market_calendars``. A string will be will be instantiated as a ``PandasMarketCalendar`` (which needs the module ``pandas_market_calendar`` installed in the system. If a subclass of `TradingCalendarBase` is passed (not an instance) it will be instantiated '''
为数据源指定交易日历
通过calendar参数遵循与addcalendar方法相同的约定来指定。
例如:
1 2 3 4
... data = bt.feeds.YahooFinanceData(dataname='YHOO', calendar='NYSE', ...) cerebro.adddata(data) ...