Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problems with Datetime

Hello all,

I've been writing an algorithm involving dividends and I'm attempting to configure the algo to purchase the stock on the ex-day. To do this I am currently using

present = datetime.now()  
present.strftime("%m/%d/%y")  
date_tx = datetime.strptime("5/6/16", "%m/%d/%y")  
if present == date_tx:  
      order_percent(symbol('TX'), 0.2)  

However, while the program backtests, datetime.now returns the actual date not the one the simulation is currently on. Clearly, this is a problem for backtesting with dividends so any advice on how to fix it would be appreciated.

1 response

Check out the get_datetime method ( https://www.quantopian.com/help#api-get-datetime ). Use that in place of 'datetime.now' and it returns the backtest datetime when backtesting or, if trading live, the current datetime.

present = get_datetime('US/Eastern')