Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to determine if two dates are the same? (To prevent closing a position on the same day as entry)

When I buy a stock, I record the date and time like this:

# Record date  
                context.Start_Date = data[stock].datetime  

I also perform stop-loss checks every tick in "handle_data()". I want to prevent my algo from performing a stop-loss calculation if today is the same day I bought the stock. How can I do this?

# Stop Loss logic  
        # Skip the stop loss check if there is an open order for the stock  
        if get_open_orders(stock):  
            return

        # Don't process stop loss on entry day  
        # This code gives an error "Runtime exception: KeyError:"  
        # I only want to compare the date, not time.  Can I do this?  "data[stock].datetime.day"  
        if data[stock].datetime == context.Start_Date:  
            return  
2 responses

Tristan,

When you buy the stock do context.Start_Date = data[stock].datetime.date() so that you've got the date not the time.
Then when you want to check for same day use if get_datetime().date() == context.Start_Date: so that you're comparing a date with a date.

so with minutely data you'll get something like...

2011-01-04 PRINT 2011-01-04 16:58:00+00:00 --- can do stop loss logic now  
2011-01-04 PRINT 2011-01-04 16:59:00+00:00 --- can do stop loss logic now  
...............
2011-01-04 PRINT 2011-01-04 20:58:00+00:00 --- can do stop loss logic now  
2011-01-04 PRINT 2011-01-04 20:59:00+00:00 --- can do stop loss logic now  
2011-01-04 PRINT 2011-01-04 21:00:00+00:00 --- can do stop loss logic now  
2011-01-05 PRINT 2011-01-05 14:31:00+00:00 --- buying AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:31:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:32:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:33:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:34:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:35:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:36:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:37:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:38:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:39:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:40:00+00:00 --- open order for AAPL(24)  
2011-01-05 PRINT 2011-01-05 14:41:00+00:00 --- not doing stop loss check right now  
2011-01-05 PRINT 2011-01-05 14:42:00+00:00 --- not doing stop loss check right now  
2011-01-05 PRINT 2011-01-05 14:43:00+00:00 --- not doing stop loss check right now  
...............
2011-01-05 PRINT 2011-01-05 20:57:00+00:00 --- not doing stop loss check right now  
2011-01-05 PRINT 2011-01-05 20:58:00+00:00 --- not doing stop loss check right now  
2011-01-05 PRINT 2011-01-05 20:59:00+00:00 --- not doing stop loss check right now  
2011-01-05 PRINT 2011-01-05 21:00:00+00:00 --- not doing stop loss check right now  
2011-01-06 PRINT 2011-01-06 14:31:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:32:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:33:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:34:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:35:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:36:00+00:00 --- can do stop loss logic now  
2011-01-06 PRINT 2011-01-06 14:37:00+00:00 --- can do stop loss logic now  

Hope it helps!

You should also remember that the date depends on the time zone. It's better always to use get_datetime("US/Eastern") to get the date and time in NYC. You can then append .date() as James described, to refer to just the date without the time.