Back to Community
batch transform decorator w/ global variables?

In this discussion, it was suggested that the batch transform decorator would take global variables. For example:

@batch_transform(refresh_period=r_p, window_length=w_l)  
def get_avg(datapanel,sid):  
    prices = datapanel['price']  
    avg = prices[sid].mean()  
    return avg  

Anybody know how to make this work? An example would be appreciated.

4 responses

Hello Grant,

This code builds for me. It it the example you're looking for? It's the basic batch transform example, but with the comment stripped out and added the variables.


def initialize(context):  
    context.stock = sid(16841)  
    context.bet_amount = 100000  
    context.long = 0

def handle_data(context, data):  

    if minmax(data, context.stock) is not None:  
        curr_max, curr_min = minmax(data, context.stock)  
    else: 'minmax returned none')  
    if curr_max is not None and context.long <=0 and data[context.stock].price >= curr_max:  
        order_amount = calculate_order_amount(context, -1, data[context.stock].price) 'Selling {n} shares of {s} at {p}'.format(n=order_amount,s=context.stock,p=data[context.stock].price))  
        order(context.stock, order_amount)  
    elif curr_min is not None and context.long>=0 and data[context.stock].price <= curr_min:  
        order_amount = calculate_order_amount(context, 1, data[context.stock].price) 'Buying {n} shares of {s} at {p}'.format(n=order_amount,s=context.stock,p=data[context.stock].price))  
        order(context.stock, order_amount)  
@batch_transform(refresh_period=R_P, window_length=W_L)  
def minmax(datapanel, sid):

    prices_df = datapanel['price']  
    min_price = prices_df[sid].min()  
    max_price = prices_df[sid].max()  
    recent_price = prices_df[sid][-1]'price is {p}, max is {max}, min is {min}'.format(  
    if min_price is not None and max_price is not None:  
        return (max_price, min_price)  
        return (None, None)    

def calculate_order_amount(context, signal_val, cur_price):

    current_amount = context.portfolio.positions[context.stock].amount  
    abs_order_amount = int(context.bet_amount / cur_price)'Current amount is: {a}.'.format(a=current_amount))'Desired amount is: {d}'.format(d=(abs_order_amount*signal_val)))  
    if signal_val == -1:  
        return (-1 * abs_order_amount) - current_amount  
    elif signal_val == 1:  
        return abs_order_amount - current_amount  
        return 0  

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. 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.

Thanks Dan,

The code below works. Would there be any way to put the global variables into def initialize(context)? I'd prefer to have them there with other backtest initialization settings.


R_P = 1  
W_L = 3

def initialize(context):  
    context.stocks = sid(16841)

def handle_data(context, data):  
    stock = context.stocks  
    avg = get_avg(data,stock)  
    event_time = data[stock].datetime  
@batch_transform(refresh_period=R_P, window_length=W_L)  
def get_avg(datapanel,sid):  
    prices = datapanel['price']  
    avg = prices[sid].mean()  
    return avg  

No, that doesn't work. The context object isn't available in the batch transform. It has to be global.

Thanks...I'm all set then!