Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
KAMA Crossover Buy/Sell

I've been trying to make an algo that will buy SPY when price moves above the Kaufman's Moving Average (KAMA) and sell when it moves below. I've been able to piece together enough code to get a chart showing when those moves would be made but everything I have tried thus far to create orders has resulted in errors... I'm not sure if it has something to do with using talib.KAMA but that was the only method I could come up with for getting the KAMA. Any help/pointers would be greatly appreciated. Thanks

1 response

David,

Try something like this:

import talib

def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(hours=1))

def trade(context, data):  
    etf = symbol('SPY')  
    period = 10

    price = data.current(etf, 'price')  
    history = data.history(etf, 'price', period + 2, '1d')  
    kama = talib.KAMA(history, period)

    record(price = price, kama = kama[-1])

    if get_open_orders(): return

    if data.can_trade(etf):  
        if price <= kama[-1]:  
            order_target_percent(etf, 1.0)  
        elif price > kama[-1]:  
            order_target_percent(etf, 0.0)