Hello Robby,
Scott definitely has more experience, but here's my angle on your question. Using the Pandas pivot table functionality, I reformat the data into a Pandas DataFrame with ascending date along the vertical axis (index) and ascending trading minute for each date along the horizontal axis (columns). Rather than using the Pandas DataPanel approach, I used a Python dictionary keyed by sid to store each DataFrame.
I did not do any spot checks for accuracy, but I think the code is correct. The only problem I see is that it appears to be bogging down the backtester (I suspect due to the pivot table computation every simulated minute). So, you could try just calling the routine at market open, or 30 minutes into the trading day, for example.
As a note to Scott, it'd be nice if history had a switch to natively return the data in this format, with trade date as an index and trade minute as columns (in a DataPanel, I suppose, when there are multiple sids).
Grant
from pytz import timezone
def initialize(context):
context.stocks = [sid(8554),sid(19920)] # SPY & QQQ
def handle_data(context, data):
prices = history(3900,'1m','price',ffill=False)
prices['date'] = prices.index.date
prices['time'] = prices.index.tz_convert(timezone('US/Eastern')).time
prices_pivot = {} # dict of pivot tables, keyed to sid
for stock in context.stocks:
prices_pivot[stock] = prices.pivot(index='date', columns='time',values=stock)
for stock in context.stocks:
print stock.symbol
print prices_pivot[stock].tail(3).iloc[:,0:3]