Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to Create an Aroon Oscillator by Stock

I'm trying to slice an Aroon factor by the stocks I'm wanting to buy. When I slice it, I'm no longer allowed to get the up/down attributes. How would I be able to create an Aroon oscillator outside of the pipeline if I can't slice the Aroon and get the up/down attributes.

def before_trading_start(context, data):  
    context.output = pipeline_output('defensive_consumer_pipeline')  
    context.security_list = context.output[context.output['longs']].index  
    context.equities = context.output[context.output['longs']]

def get_aroon(s):  
    aroon = Aroon(window_length=15, mask=context.equities)  
    up = aroon[s].up  
    down = aroon[s].down  
    osc = up - down  
    return osc  

I call the get_aroon(s) function in my selling and buying methods. What I get is slice it and use the .up attribute or the .down attribute, I get a runtime error saying " 'Slice' object has no attribute 'up'." or " 'Slice' object has no attribute 'down'."

12 responses

There is a predefined class in zipline. That might be easier as that exports all needed vars

Could you put a link to the documentation for this? Would be very helpful :)

That shows the Aroon class that I'm using in my get_aroon(s) method. But for this to work, it has to be within the Pipeline. If you try to slice the stock data you want out of it, it returns the error I mentioned above.

Like in the pipeline, you can do

aroon = Aroon(mask=base_universe, window_length=15)  
up = aroon.up  
down = aroon.down  
osc = up - down  

And then add it to your pipeline. I'm wanting this for trading decisions so I'm trying to slice to values for the calculation of the current stock that needs to be determined as you can see where I did aroon[s].up. Only problem is that the .up and .down aren't available when you slice it.

Dustin ,

If you want to create Aroon oscillator outside of the pipeline
then try this:

# Aroon Oscilator

import talib

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

def trade(context, data):  
    assets, period, lev = symbols('AAPL', 'AMZN', 'TLT'), 14, 1.0  
    wt = lev/len(assets)

    for asset in assets:  
        H = data.history(asset, 'high', period + 1, '1d')  
        L = data.history(asset, 'low', period + 1, '1d')  
        AroonDown, AroonUp = talib.AROON(H, L, period)  
        Aroon_Osc = AroonUp[-1] - AroonDown[-1]

        if get_open_orders(asset): continue  
        if data.can_trade(asset):  
            if Aroon_Osc > 0:  
                order_target_percent(asset, wt)  
            else:  
                order_target_percent(asset, 0)  

I get an "init() takes exactly 4 arguments (3 given)" error with this

Dustin ,

Can you attach the backtest of the algo I just published above?

Here's the backtest of your code you wrote. Not sure why it's not working for me.....

I got it working finally. Had to do something with me trying to use RSI as well.

Dustin ,

Can you attach the backtest ?

After getting rid of the code for RSI checking, I added it back in and now it's working. Don't know what is different because I wrote it the same other than the aroon oscillator being the first in the check rather than the RSI