backtrader.utils.date module¶
Date/Time Conversion Utilities Module.
This module provides functions for converting between datetime objects and numeric representations used internally by backtrader. It also provides timezone handling utilities.
- Exports:
date2num: Convert datetime to internal float representation. num2date: Convert internal float to datetime. num2dt: Alias for num2date. time2num: Convert time to internal float representation. num2time: Convert internal float to time. tzparse: Parse timezone string. Localizer: Timezone localization helper. TZLocal: Local timezone instance. UTC: UTC timezone instance. TIME_MAX/TIME_MIN: Maximum/minimum time values.
示例
>>> from backtrader.utils import date2num, num2date
>>> import datetime
>>> dt = datetime.datetime(2020, 1, 1)
>>> num = date2num(dt)
>>> print(num2date(num))
2020-01-01 00:00:00+00:00
- backtrader.utils.date.num2date(x, tz=None, naive=True)[源代码]¶
x is a float value that gives the number of days (fraction part represents hours, minutes, seconds) since 0001-01-01 00:00:00 UTC plus one. The addition of one here is a historical artifact. Also, note that the Gregorian calendar is assumed; this is not universal practice. For details, see the module docstring. Return value is a: class:datetime instance in timezone tz (default to rcparams TZ value). If x is a sequence, a sequence of: class:datetime objects will be returned.
- backtrader.utils.date.num2dt(num, tz=None, naive=True)[源代码]¶
Convert numeric date to date object.
- 参数:
num -- Numeric date value (days since 0001-01-01 UTC + 1).
tz -- Timezone for the result (optional).
naive -- If True, return naive date without timezone info.
- 返回:
Date object extracted from the datetime.
- 返回类型:
date
- backtrader.utils.date.date2num(dt, tz=None)[源代码]¶
Convert: mod:datetime to the Gregorian date as UTC float days, preserving hours, minutes, seconds and microseconds. Return value is a: func:float.
- backtrader.utils.date.time2num(tm)[源代码]¶
Converts the hour/minute/second/microsecond part of tm (datetime.datetime or time) to a num
- backtrader.utils.date.num2time(num, tz=None, naive=True)[源代码]¶
Convert numeric date to time object.
- 参数:
num -- Numeric date value (days since 0001-01-01 UTC + 1).
tz -- Timezone for the result (optional).
naive -- If True, return naive time without timezone info.
- 返回:
Time object extracted from the datetime.
- 返回类型:
time
- backtrader.utils.date.get_last_timeframe_timestamp(timestamp, time_diff)[源代码]¶
Get previous whole minute timestamp based on current timestamp :params timestamp int, calculate from int(time.time()) :params time_diff int, e.g. 1m timeframe using 60 :returns timestamp int
- backtrader.utils.date.Localizer(tz)[源代码]¶
Add a localize method to a timezone object.
This function adds a localize method to tz objects that don't have one, allowing consistent timezone localization across different timezone implementations.
- 参数:
tz -- Timezone object to add localize method to.
- 返回:
The same timezone object with a localize method added.
- backtrader.utils.date.tzparse(tz)[源代码]¶
Parse a timezone specification into a tzinfo object.
- 参数:
tz -- Timezone specification (string, tzinfo object, or None).
- 返回:
A tzinfo object. If pytz is available and tz is a string, returns the corresponding pytz timezone. Otherwise returns a Localizer-wrapped tz object.
- backtrader.utils.date.timestamp2datetime(timestamp)[源代码]¶
Convert timestamp to datetime param: timestamp timestamp param: string_format (str): string format Return: formatted_time (Str): timestamp
- backtrader.utils.date.datetime2timestamp(time_date, string_format='%Y-%m-%d %H:%M:%S.%f')[源代码]¶
Convert datetime to timestamp param: datetime_string (str): timezone in pytz.common_timezones param: string_format (str): string format Return: timestamp