Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Logging time of intraday trades, in EST timezone?

Hey all – I feel as though there must be a very simple solution to this problem, but I'm stumped for what that might be...

I'm writing an algorithm that enters trades intraday, and I would like to log the time (hour and minute) those trades are entered during a backtest in EST timezone. Here is the snippet of code for my entries:

    # LONG ENTRY ORDER  
    if (currentPrice > sixtyBars) and currentPositions == 0:  
        positionSize = int((cash * 0.2) / currentPrice)  
        order(context.stock, positionSize)  
        log.info('Long Entry: Bought {0} shares at ${1}.'.format(positionSize, currentPrice))  

        # TIME LOG  
        eastern = timezone('US/Eastern')   # Sets EST timezone  
        entryTime = data[context.stock].datetime   # Gets time of entry -- but needs to be eastern!  
        log.debug(entryTime.strftime('%x - %H:%M - %Z'))  # Logs time of entry -- displaying date, hour, minute, and timezone  

Here are the current logs from the code above:

2015-12-04  firstHour:56  INFO  Long Entry: Bought 84 shares at $118.1.  
2015-12-04  firstHour:62  DEBUG  12/04/15 - 15:30 - UTC  

If only I could somehow apply the 'eastern' variable to the 'entryTime' variable – just not sure how to do this?

Any help/suggestions would be hugely appreciated.

2 responses

You're right, simple ... data[stock].datetime.astimezone(timezone('US/Eastern'))
Like -ish ...

from pytz import timezone

def initialize(context):  
    symbols('TSLA', 'AAPL')

def handle_data(context, data):  
    for s in data:  
        # LONG ENTRY ORDER  
        if 1:  
            currentPrice = data[s].price  
            order(s, 10)  
            log.info('Long Entry: Bought {0} {1} shares at ${2}.'.format(  
                    10, s.symbol, currentPrice))  

            # TIME LOG  
            eastern = timezone('US/Eastern')   # Sets EST timezone  
            entryTime = data[s].datetime.astimezone(eastern)  
            log.debug(entryTime.strftime('%x - %H:%M - %Z'))  # Logs time of entry -- displaying date, hour, minute, and timezone 

2015-12-02 handle_data:14 INFO Long Entry: Bought 10 AAPL shares at $117.328.  
2015-12-02 handle_data:19 DEBUG 12/02/15 - 09:31 - EST  
2015-12-02 handle_data:14 INFO Long Entry: Bought 10 TSLA shares at $236.47.  
2015-12-02 handle_data:19 DEBUG 12/02/15 - 09:31 - EST  
2015-12-02 handle_data:14 INFO Long Entry: Bought 10 AAPL shares at $117.285.  
2015-12-02 handle_data:19 DEBUG 12/02/15 - 09:32 - EST  
2015-12-02 handle_data:14 INFO Long Entry: Bought 10 TSLA shares at $235.79.  
2015-12-02 handle_data:19 DEBUG 12/02/15 - 09:32 - EST  

Gary – thank you! Huge help.