Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
basic moving average algorithm

been playing around with basic algorithms, and i keep running into a lot of trouble with "deprecated" warnings for a simple moving average, basically having trouble figuring out how to write a simple moving average for 10 and 30 days in Quantopian 2.
I would really appreciate someone showing an example of how to apply the most recent moving average to a basic algorithm that that makes trades based on the crossover of 10 and 30 days. i attached a basic algo that uses the moving average which i am referring to.

2 responses

Try this:

def initialize(context):  
    schedule_function(myfunc, date_rules.every_day(),  time_rules.market_open(minutes = 15))

def myfunc(context, data):  
    stk = symbol('AAPL')  
    MovingAvg1 = data.history(stk, 'price', 10,'1d').mean()  
    MovingAvg2 = data.history(stk, 'price', 30,'1d').mean()  
    current_positions = context.portfolio.positions[stk].amount  
    if (MovingAvg1 > MovingAvg2) and current_positions == 0:  
        order_target_percent(stk, 0.25)  
    elif (MovingAvg1 < MovingAvg2) and current_positions != 0:  
        order_target(stk, 0)  

thank you! that seemed to do the trick, and i figured out what i was doing wrong