Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ema momentum algo HELP NEEDED!!!!

ideally this algo would buy when current price crosses above 10 day EMA but also the 25 day ema has to be above the 50 ema and the 50ema above 100ema and 100ema above the 200ema and then it would sell when the current price falls below the 5 day ema. I thought it would be as simple as
if (current_price > 10ema and 10ea > 25ema and 25ea > 50ema and 50ema> 100ema and 100ema > 200ema )
but it keeps coming up with ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
i have now basically tore my entire algo apart just to get any sort of crossover to work but nothing... now i don't know whats what i don't know what to add in what to take out. ANYONE PLEASE HELP!!!!!

1 response

This may help.

import talib

def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65)) 

def trade(context, data):  
    stock = symbol('AAPL'); bond = symbol('TLT');  
    if get_open_orders(stock): return

    prices = data.history(stock,'price', 1000, '1d')  
    ema_1 = talib.EMA(prices, timeperiod=2)[-1]  
    ema_2 = talib.EMA(prices, timeperiod=10)[-1]  
    ema_3 = talib.EMA(prices, timeperiod=25)[-1]  
    ema_4 = talib.EMA(prices, timeperiod=50)[-1]  
    ema_5 = talib.EMA(prices, timeperiod=100)[-1]  
    ema_6 = talib.EMA(prices, timeperiod=200)[-1] 

    if (ema_1 > ema_2 and ema_2 > ema_3 and ema_3 > ema_4 and ema_4 > ema_5 and ema_5 > ema_6):  
        if context.portfolio.positions[stock].amount == 0:  
            order_target_percent(stock, 1.0)  
            order_target_percent(bond, 0.0)

    elif (ema_1 < ema_2) and context.portfolio.positions[stock].amount > 0:  
        order_target_percent(stock, 0.0)  
        order_target_percent(bond, 1.0)

    record(ema_1 = ema_1, ema_2 = ema_2, ema_3 = ema_3, ema_4 = ema_4, ema_5 = ema_5)