Recently, I noted some odd behavior by the batch transform. I found that it apparently re-orders the sids if context.stocks is a list. The order is maintained if context.stocks is a set. The outputs for the respective cases are below:
context.stocks = [sid(351), sid(1419), sid(1787), sid(25317), sid(3321), sid(3951), sid(4922)]
2013-02-26handle_data:25DEBUG<type 'list'>
2013-02-26handle_data:26DEBUG[ 2.4601 87.18 99.43 13.89 31.8 20.58 102.35 ]
2013-02-26handle_data:27DEBUG[[ 13.89 87.18 20.58 31.8 102.35 99.43 2.4601]]
context.stocks = {sid(351), sid(1419), sid(1787), sid(25317), sid(3321), sid(3951), sid(4922)}
2013-02-26handle_data:25DEBUG<type 'set'>
2013-02-26handle_data:26DEBUG[ 13.89 87.18 20.58 31.8 102.35 99.43 2.4601]
2013-02-26handle_data:27DEBUG[[ 13.89 87.18 20.58 31.8 102.35 99.43 2.4601]]
Is this the expected behavior, or is it a bug? If it is not a bug, I suggest making this clear in your documentation and training, since it is a subtle difference that can result in erroneous output (as I learned).
The "Add Backtest" button is not available, so I am posting the code in-line:
import numpy as np
# globals for get_avg batch transform decorator
R_P = 1 # refresh period
W_L = 1 # window length
def initialize(context):
#context.stocks = [sid(351), sid(1419), sid(1787), sid(25317), sid(3321), sid(3951), sid(4922)]
context.stocks = {sid(351), sid(1419), sid(1787), sid(25317), sid(3321), sid(3951), sid(4922)}
def handle_data(context, data):
prices = np.zeros(len(context.stocks))
# get prices
for i, stock in enumerate(context.stocks):
prices[i] = data[stock].price
# get batch transform prices
if get_prices(data,context) == None:
log.debug("get_prices(data,context) == None")
return
prices_bt = get_prices(data,context)
log.debug(type(context.stocks))
log.debug(prices)
log.debug(prices_bt)
@batch_transform(refresh_period=R_P, window_length=W_L) # set globals R_P & W_L above
def get_prices(datapanel,sids):
return datapanel['price'].values