So I found the problem, and here is the fixed code for rebalance:
# Rebalance every 15mins.
def rebalance(context, data):
# Load historical data for the stocks
historyperiod = 2 * context.timeperiod * context.ATRperiod
hist = data.history(context.stocks, ['high', 'low', 'close'], historyperiod, '1m')
for stock in context.stocks:
# Calculate the ATR for the stock
high = hist['high'][stock].resample('15min', how='max').dropna()
low = hist['low'][stock].resample('15min', how='min').dropna()
close = hist['close'][stock].resample('15min', how='last').dropna()
atr = talib.ATR(high.values, low.values, close.values, timeperiod=context.ATRperiod)[-1]
print high.index[-3:-1]
But now I see another problem. Here when we print the high.index[-3:-1], I get
PRINT DatetimeIndex(['2011-01-03 21:00:00+00:00', '2011-01-04 14:30:00+00:00'], dtype='datetime64[ns]', freq=None, tz='UTC')
Here resample is creating a timestamp for '2011-01-04 14:30:00+00:00' which is the market open time, if I am not wrong.
Now we dont want a 15min candle for this time. Rather our first 15min candle should be at '2011-01-04 14:45:00+00:00', so now how do I get rid of this '2011-01-04 14:30:00+00:00' candle?
Any help is really appreciated :)