Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem with a simple day average algorithm

I'm new for quantopian and try a simple day average algorithm that buy when 5 day moving average is higher than 12 day moving average and sell short when 5 day moving average is lower than 12 day moving average. But I face an strange problem that return go extremely up and down which is crazy and impossible in real world, Why?

1 response

If you run it in minute mode handle_data will get called every minute and execute the orders. Was that the intention?

If you want to place the orders daily use schedule_function as shown below. You can look at your transactions in "Transaction Details".

def initialize(context):  
    set_benchmark(symbol('AAPL'))  
    context.stock=[sid(24),sid(35920),sid(25006),sid(2754),sid(5261),sid(3788)]  
    set_commission(commission.PerTrade(cost=7))  
    schedule_function(func=execute_daily_order,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open())

def execute_daily_order(context, data):  
    for stock in context.stock:  
#mvag of 5, 12, 30 and 120 mins to evaluate mins level trend  
       bench5=history(5,'1d','price').mean()[stock]  
       bench12=history(12,'1d','price').mean()[stock]  
       bench30=history(30,'1d','price').mean()[stock]  
       bench120=history(120,'1d','price').mean()[stock]  
       if bench5>bench12:  
            order_target_percent(stock,0.2,style=LimitOrder(data[stock].price))  
       if bench5<bench12:  
            order_target_percent(stock,-0.2,style=LimitOrder(data[stock].price))  
# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    pass