Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
MA1 VS MA2

Hello All, I'm new to programming. I did find this and it makes sense to me, but when I run it. It does give me the back test, but not giving me the opportunity to trade the algorithm it does say go to this page https://www.quantopian.com/quantopian2/migration But I did try to figure it out and it doesn't make sense to me at all...

I do get a window it dose say this

Line 7: data[sid(N)] is deprecated. Use data.current.
Line 7: The mavg method is deprecated.
Line 8: data[sid(N)] is deprecated. Use data.current.
Line 8: The mavg method is deprecated.
Line 10: data[sid(N)] is deprecated. Use data.current.

The code that I'm working with

def initialize(context):  
    context.security = symbol('SPY')  

def handle_data(context, data):  
    MA1 = data[context.security].mavg(50)  
    MA2 = data[context.security].mavg(100)  
    current_price = data[context.security].price  
    current_positions = context.portfolio.positions[symbol('SPY')].amount  
    cash = context.portfolio.cash  
    if (MA1 > MA2) and current_positions == 0:  
        number_of_shares = int(cash/current_price)  
        order(context.security, number_of_shares)  
        log.info("Buying shares")  
    elif (MA1 < MA2) and current_positions != 0:  
        order_target(context.security, 0)  
        log.info("Selling shares")  

    record(MA1 = MA1, MA2 = MA2, Price= current_price)  
2 responses
def initialize(context):  
    context.stock = symbol('BAC')  

def handle_data(context, data):  
    price_hist_25 = data.history(context.stock, 'price', 25, '1d')  
    price_hist_50 = data.history(context.stock, 'price', 50, '1d')  
    MA1 = price_hist_25.mean()  
    MA2= price_hist_50.mean()  
    current_price = data.current(context.stock, 'price')  
    current_positions = context.portfolio.positions[symbol('BAC')].amount  
    cash = context.portfolio.cash  
    if (MA1 > MA2) and current_positions == 0:  
        number_of_shares = int(cash/current_price)  
        order(context.stock, number_of_shares)  
        log.info("Buying shares")  
    elif (MA1 < MA2) and current_positions != 0:  
        order_target(context.stock, 0)  
        log.info("Selling shares")  

    record(MA1 = MA1, MA2 = MA2, Price= current_price)  

Here you go :)

Thanks! ;)