Back to Posts
Listen to Thread

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.

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.

R_P=1  
W_L=10

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

def handle_data(context, data):  
    curr_max=None  
    curr_min=None

    if minmax(data, context.stock) is not None:  
        curr_max, curr_min = minmax(data, context.stock)  
    else:  
        log.info('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)  
        log.info('Selling {n} shares of {s} at {p}'.format(n=order_amount,s=context.stock,p=data[context.stock].price))  
        order(context.stock, order_amount)  
        context.long=1  
    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)  
        log.info('Buying {n} shares of {s} at {p}'.format(n=order_amount,s=context.stock,p=data[context.stock].price))  
        order(context.stock, order_amount)  
        context.long=-1  
@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]  
    log.info('price is {p}, max is {max}, min is {min}'.format(  
                                                            p=recent_price,  
                                                            max=max_price,  
                                                            min=min_price)  
                                                          )  
    if min_price is not None and max_price is not None:  
        return (max_price, min_price)  
    else:  
        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)  
    log.info('Current amount is: {a}.'.format(a=current_amount))  
    log.info('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  
    else:  
        return 0  

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.

Grant

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  
    log.debug(event_time)  
    log.debug(avg)  
@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!

Log in to reply to this thread.
Not a member? Sign up!