Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
API Tool: Time/Event Management for minute data

Hello all,
I wanted to share some useful code to make switching to, and working with, minute data easier.

The aim is to make it easy to get from one day to the next, so only your entry point during the day needs to be worried about. It tends to shorten your handle_data function too, which is nice.

This backtest was run on daily data with batch_transform, the next backtest is the same algorithm switched to minute data to give an example of making the switch.

The algorithm is an MPT tangency portfolio. It imports the 3 month t-bill rates from Quandl to be used as the risk free rate.

Dave

4 responses

This is the version run on minute data. It also shows how you can use the EventManager class to manage several different tasks.
I'd like to get some feedback on this, there are a few issues, but it's a good start.

Currently the most glaring problem with this has to to with setting the maximum number of hits per day. It will not update to a new trade date until the maximum number of hits per day has been satisfied. If the max daily hits is greater than the number of times the entry_func is satisfied in one day, it will cause problems.

I plan to make another version that uses the trading calendar as well so keep your eyes peeled.

Hey David,

"Business days" is pretty close to the trading calendar for most purposes.

-Russell

import pandas as pd  
from pandas.tseries.offsets import BDay

def getBDayHeld(context, data, stock):  
    BDayHeld = 0  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    try:  
        if context.purchaseDate.has_key(stock.symbol):  
            s = pd.date_range(context.purchaseDate[stock.symbol],exchange_time.date(),freq='D')  
            df = pd.DataFrame(0,index=s,columns=list('N'))  
            BDayHeld = len(df.asfreq(BDay())) - 1  
        else:  
            log.info (" in getBDayHeld context.purchaseDate.has_key MISSING " + stock.symbol)  
    except:  
        log.info (" EXCEPTION: in getBDayHeld context.purchaseDate.has_key MISSING " + stock.symbol)  
    return BDayHeld  

Here is a version that uses the trading calendar in Zipline. It is a little simpler and the max_daily_ hits works as intended. It makes sure that the current date is between the open and close of the event date, then it calls the rule_func to get an entry decision.