Have you tried using the .isoformat() function? It automatically converts the date into the standard/required format.
I just wrote a function that should help you:
import datetime as dt
import pytz
def get_todays_trades(_symbol):
_currentTime = dt.datetime.now(pytz.timezone('US/Eastern'))
# As the market hasn't opened yet (at time of writing), I pushed the date back 3 days. The below line is just for testing purposes.
_currentTime = _currentTime - dt.timedelta(days=3)
# Calculate the start and end time by modifying today's full timestamp with specific hours/minutes/seconds. I've used 4pm (16:00) to 4:01pm (16:01) for this example. You could use variables to make this more dynamic.
_startTime = pd.Timestamp(year=_currentTime.year, month=_currentTime.month, day=_currentTime.day, hour=16, minute=00, second=00, tz='US/Eastern')
_endTime = pd.Timestamp(year=_currentTime.year, month=_currentTime.month, day=_currentTime.day, hour=16, minute=00, second=1, tz='US/Eastern')
print(f"\nToday's {_symbol} trades between {_startTime} and {_endTime}:\n")
data = rest_api.get_trades(_symbol, start=_startTime.isoformat(), end=_endTime.isoformat()) # Don't forget to use .isoformat() !!!
print(data.df)
return data
# Start:-----------------------
get_todays_trades('TSLA')
Output:
Today's TSLA trades between 2022-02-04 16:00:00-05:00 and 2022-02-04 16:01:00-05:00:
exchange price ... id tape
timestamp ...
2022-02-04 21:00:00+00:00 D 923.3200 ... 172349 C
2022-02-04 21:00:00.076319488+00:00 P 922.8500 ... 73708 C
2022-02-04 21:00:00.204499262+00:00 Q 922.8600 ... 156873 C
2022-02-04 21:00:00.221063766+00:00 Q 923.1500 ... 156874 C
2022-02-04 21:00:00.534677+00:00 Z 923.0400 ... 40080 C
... ... ... ... ... ...
2022-02-04 21:00:59.006858687+00:00 D 922.4200 ... 70843 C
2022-02-04 21:00:59.233357642+00:00 V 922.6700 ... 17313 C
2022-02-04 21:00:59.234561+00:00 Z 922.3600 ... 40086 C
2022-02-04 21:00:59.235063381+00:00 D 922.3123 ... 70844 C
2022-02-04 21:00:59.498821+00:00 D 922.8000 ... 170637 C
[299 rows x 6 columns]
Note that the dates in the output dataframe are in UTC timezone