Hi guys,
I was wondering if someone could show me how to compare the current time to a date time variable. I want to place an order 5 minutes after a trigger.
Thanks for the help!
Hi guys,
I was wondering if someone could show me how to compare the current time to a date time variable. I want to place an order 5 minutes after a trigger.
Thanks for the help!
You can get the current backtest time in an algo by using the "get_datetime()" function (see https://www.quantopian.com/help#api-get-datetime in the docs). Create an offset to that time using the "timedelta()" function. Then simply compare the current time to the event time plus the offset time and do whatever.
The code is below and also in the attached notebook. To see it work, run the first cell and last cell immediately after the first and it will print "still waiting". Wait a bit then re-run the last cell only and it will print "5 seconds have elapsed".
Hope that points you in a direction to capture event times and then compare to current times.
from datetime import datetime, timedelta
# Capture the time an event happened by setting variable equal to the current time at that instant
# In an algo use "get_datetime()" to get the backtest time (not current time) / in research use "datetime()"
event_time = get_datetime()
# create a time delta 5 seconds from the event time (or whatever delta you wish. the offset can also be in minutes or hours or even days)
delta_time = timedelta(seconds = 5)
# Check to see if current time is greater than event time + offset time
# Again, in an algo use "get_datetime()" to get the backtest time (not current time) / in research use "datetime()"
if get_datetime() > event_time + delta_time :
print "5 seconds have elapsed"
else:
print "still waiting"