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. 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.

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!