<meta no_market>

当前时间

适用市场

市场品种
香港市场股票、期权、窝轮、牛熊证、界内证、ETFs、板块、期货、指数
美国市场股票、期权、ETFs、板块、期货、指数
A股市场股票、ETFs、板块、指数
新加坡市场期货、指数
日本市场期货、指数
加拿大市场指数
澳大利亚市场指数
外汇市场外汇
欧洲市场指数
韩国市场指数
印度市场指数
台湾市场指数
马来西亚市场指数

device_time

接口说明

获取当前时间。
在历史回测中,返回历史上的当前时间。在实盘运行中,返回当前设备的时间。

device_time(TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE-

返回

返回类型: datetime

示例说明

获取当前时间。

devicetime = device_time(TimeZone.DEVICE_TIME_ZONE)
print(devicetime)
print(devicetime.strftime("%Y-%m-%d %H:%M:%S"))  # 格式化日期和时间
print(devicetime.hour)  # 打印小时
print(devicetime.minute)  # 打印分钟
print(devicetime.second)  # 打印秒

示例返回值

2023-07-31 09:30:00-04:00
2023-07-31 09:30:00
9
30
0

使用 device_time() 与 datetime.datetime.now(),2种方法获取时间有什么区别?
前者是量化提供的函数,后者是python标准库中的函数。
在实盘运行中,2种方法没有差别,都是获取当前设备的时间。
在历史回测中,device_time() 返回历史上的当前时间,而datetime.datetime.now()仍返回当前设备的时间。
我们推荐使用device_time()。

is_the_time

接口说明

判断当前时间是否早于或晚于指定时间。

is_the_time(Orientation, hour, min, sec, year, month, day, time_zone=TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
OrientationTimeOrientation时间判断方向----
hourint--0-23
minint--0-59
secint--0-59
yearint--1970-2050
monthint--1-12
dayint--1-31
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE--

返回

返回类型: Boolean

示例说明

判断当前时间是否在 UTC+8 的 9:46:49 之后。

is_the_time(hour=9, min=46, orientation=TimeOrientation.LATER_THAN, sec=49, time_zone=TimeZone.UTC_PLUS_8)

示例返回值

True

is_the_day

接口说明

判断当前时间是否处于指定时间(日)。

is_the_day(day, time_zone=TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
daylist--1-31
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE--

返回

返回类型: Boolean

示例说明

判断当前时间是否在 UTC+8 的 1 日或 2 日。

is_the_day(day=[1,2], time_zone=TimeZone.UTC_PLUS_8)

示例返回值

True

is_the_week

接口说明

判断当前时间是否处于指定时间(周)。

is_the_week(week, time_zone=TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
weekweek--1-7
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE--

返回

返回类型: Boolean

示例说明

判断当前时间是否在 UTC+8 的周一或周二。

is_the_week(time_zone=TimeZone.UTC_PLUS_8, week=[1,2])

示例返回值

True

is_the_month

接口说明

判断当前时间是否处于指定时间(月)。

is_the_month(month, time_zone=TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
monthlist--1-12
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE--

返回

返回类型: Boolean

示例说明

判断当前时间是否在 UTC+8 的1月或2月。

is_the_month(month=[1,2], time_zone=TimeZone.UTC_PLUS_8)

示例返回值

True

is_the_year

接口说明

判断当前时间是否处于指定时间(年)。

is_the_year(year, time_zone=TimeZone.DEVICE_TIME_ZONE)

参数

参数名类型说明默认值范围
yearlist--1970-2050
time_zoneTimeZone时区TimeZone.DEVICE_TIME_ZONE--

返回

返回类型: Boolean

示例说明

判断当前时间是否在 UTC+8 的 2023 或 2024 年。

is_the_year(time_zone=TimeZone.UTC_PLUS_8, year=[2023,2024])

示例返回值

True