Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Momentum Strategy by Fred Piard, Help please

Hello,

I am trying to build a momentum strategy. Basically what it is supposed to do is:
If SPY's 50 day SMA is below or equal to the 200 day SMA, then it shall buy IEF.
If that's not the case, it should choose the top 6 ETF's ranked by a 20 day Momentum and buy them, if the 50 day SMA is higher than the 200 day SMA.
I am supposed to use the chosen template for all my algorithms so I get used to using it.
I scrambled a few things together and got the attached algorithm. It now gives me the Error:

"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). There was a runtime error on line 208."

In the initialize function are all ETF's, that are used in the book's strategy.

I am pretty new to Python and Quantopian and would appreciate all help given. Thanks !

3 responses

Maximilian,

Try this very similar momentum strategy from my library.

# Momentum Strategy

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

def trade(context, data):  
    # --------------------------------------------------------------------------------------------  
    market, bond = symbol('SPY'), symbol('IEF')  
    mom, n, ma_f, ma_s, lev = 80, 5, 50, 200, 1.0  
    stocks = symbols('XLV', 'XLK', 'XLI', 'XLP', 'XLE', 'XLU', 'XLB', 'XLY', 'XLF', 'IYR', 'TLT',)  
    # --------------------------------------------------------------------------------------------  
    if get_open_orders(): return 

    C = data.history(stocks, 'price', mom + 1, '1d')  
    M = C.iloc[-1] / C.iloc[0]  
    M = M.dropna()  
    M.sort_values(ascending = False, inplace = True)  
    picks = M.head(n)  
    wt = lev / len(picks) if len(picks) != 0 else 0

    uptrend = data.history(market, 'price', ma_f, '1d').mean() > data.history(market, 'price', ma_s, '1d').mean()

    for asset in stocks:  
        if data.can_trade(asset):  
            if asset in picks and uptrend:  
                order_target_percent(asset, wt)  
            else:  
                order_target(asset, 0)

    if data.can_trade(bond):  
        if not uptrend:  
            order_target_percent(bond, lev)  
        else:  
            order_target(bond, 0)

def before_trading_start(context, data):  
    record(leverage = context.account.leverage)  
        for l in MomList:  
            stock = l[0]  
            hist = data.history(**context.stocks**, "price", 200, "1d")  
            log.info(hist.head())  
            sma1_200 = hist.mean()  
            sma1_50 = hist[-50:].mean()  
            if sma1_50 > sma1_200:  
                order_target_percent(stock, 1.0)  

Problem in bold. You're getting the price history of all yours stocks, but comparing it as if it were just one stock.

Does this fix it:

hist = data.history(stock, "price", 200, "1d")  

Thanks Vladimir,

really helped out a lot.
Just one problem left, which is, that I only want to use those of the highest 20 day momentum, that also have a 50 day sma > 200 day sma. And that needs to be checked individually. The problem is, that I only know how to test, if the rule applies, but then it just combines all the True and False and gives me one in total instead of one for each stock. Obviously it could be done one by one, that isn't really what I was going for though.

Hope you can maybe help me out a little more, thanks.