def hist_data_req_start_end(req: HistDataReq, xchg_tz: pytz.tzinfo):
"""
Calculate start and end datetime for a historical data request.
If req.TimeDur and BarSize both are in h/m/s, data range is limited to
the intraday data of req.TimeEnd.date().
:param xchg_tz: Time zone info of the security exchange for req.
"""
time_dur = req.TimeDur
end_dt = xchg_tz.normalize(req.TimeEnd)
if time_dur[-1] in ('W', 'M', 'Y'):
start_dt = xchg_tz.normalize(end_dt - timedur_to_reldelta(time_dur))
trd_days = trading_days(end_dt, time_start=start_dt)
elif time_dur[-1] is 'd':
# trd_days is a DateTimeIndex, with consecutive integer index.
trd_days = trading_days(end_dt, time_dur)
_logger.debug('trd_days: \n%s', trd_days)
start_date = trd_days.iloc[0].to_pydatetime()
start_dt = tzcomb(start_date, end_dt.time(), xchg_tz)
else: # TimeDur in h/m/s.
trd_days = trading_days(end_dt, time_dur)
_logger.debug('trd_days: \n%s', trd_days)
if req.BarSize[-1] is 'd':
# BarSize in d. Start time set to 00:00:00 of start date.
start_date = trd_days.iloc[0].to_pydatetime()
start_dt = tzmin(start_date, tz=xchg_tz)
else:
# BarSize in h/m/s; Limit to intraday data.
_logger.warning(
'req.TimeDur and req.BarSize are both in h/m/s.'
'Time range limit to intraday.')
start_date = trd_days.iloc[-1].to_pydatetime()
start_dt = max(tzmin(start_date, tz=xchg_tz),
xchg_tz.normalize(
end_dt-timedur_to_reldelta(req.TimeDur)))
return start_dt, end_dt, trd_days
评论列表
文章目录