Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Batch transform question / problem?

WRT quantopian.com/help#ide-batch-transforms, why does the value returned from the transform not have the same structure as the value inside the transform? In the example, below, the log statements' outputs are identical. When prices[sid(24)][0] is accessed within the transform function, the code works. But when prices[sid(24)][0] is accessed in the caller, there is a runtime error.

import datetime
import pytz

def initialize(context):
context.stocks = [sid(24), sid(8347), sid(3766)]
utc = pytz.timezone('UTC')
context.d = datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)

def handle_data(context, data):
prices = endpoints(data, context.stocks)
log.info('p2: {p}'.format(p=prices))
firstPrice = prices[sid(24)][0]
log.info('end-of-day')

@batch_transform(refresh_period=1, window_length=5)
def endpoints(data, stocks):
prices = data['price']
log.info('p1: {p}'.format(p=prices))
firstPrice = prices[sid(24)][0]
return prices

2 responses

Kevin:

The batch_transform class has the default behavior of computing only when the window is full, furthermore even when that behavior is disabled by setting compute_only_full to False, the BatchTransform currently returns None until the first trading day is full.

Open to discussing about the correctness/intuitiveness of this behavior.

Your algorithm should run if make sure that the transform has returned values before accessing it.
If you change:

firstPrice = prices[sid(24)][0]  

to:

if prices is not None:  
    firstPrice = prices[sid(24)][0]  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Your suggestion worked. The log reveals that is not called for the first 5 days of the simulation. That wasn't obvious to me -- when I call a subroutine, I expect it to be executed. Thanks.