Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Seeking help: Error " Argument has incorrect type"

Hi everyone.

I'm trying to use the ADX indicator in my Algo by i'm stuck with this error "Argument 'high' has incorrect type (expected numpy.ndarray, got numpy.float64).

        high = history(15, "1d", "high")  
        low = history(15, "1d", "low")  
        close= history(15, "1d", "price")  
        adx = talib.ADX(high[s][-1], low[s][-1], close[s][-1] , timeperiod=14)

         if adx > 30:  

Another question: is there a way to use the ADX indicator as a filter in the Pipline Api? ( for example we want only the stocks with adx>35)

Thank you in advance

7 responses

I tried the code below, and I got another error : "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

        high1 = history(15, "1d", "high")[:-1]  
        low1 = history(15, "1d", "low")[:-1]  
        close= history(15, "1d", "price")[:-1]  
        adx = talib.ADX(high1[s], low1[s], close[s] , timeperiod=14)

         if adx > 30:  

Hi Youness,

Here's a version of talib.ADX that works. Unfortunately, talib functions don't work well in pipeline because you would have to iterate through each security in a for loop in a custom factor calling the talib function. This would be very inefficient and lead to a very slow backtest.

I hope this helps.

Cheers,
Jamie

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Jamie.

I'm using "set_universe"and I'm getting this error: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

import talib

def initialize(context):  
    set_universe(universe.DollarVolumeUniverse(90, 91))  
    #context.size = float(context.portfolio.cash / 30.0)  
    #context.shorts =  context.size * 0.5  
    context.shorting= True  
    context.max_positions = 10  
    #context.max_positions2 = 6

    #schedule_function(logic,date_rules.every_day(),time_rules.market_close())  

def handle_data(context, data):  
    context.l = float(( 0.5 * context.portfolio.portfolio_value) /7)  
    #context.s =  float(( -0.5 * context.portfolio.portfolio_value) /7)  
    open_orders = get_open_orders()  
    position_count = get_position_count(context)  
    for s in data:  
        if s in open_orders:  
            continue  
        #price = data[s].price  
        current_position = context.portfolio.positions[s].amount  
        price = data[s].price  
        high1 = history(15, "1d", "high")  
        low1 = history(15, "1d", "low")  
        close= history(15, "1d", "price")  
        #highs=high1[s][-1]  
        #lows=low1[s][-1]  
        #closes=close[s][-1] 

        adx = talib.ADX(high1[s], low1[s], close[s] , timeperiod=14)  
        if position_count < context.max_positions:  
            if  (current_position == 0)    :  
                        if (adx > 35) :  
                            order_target_value(s, context.l )  
                            log.info("LONG " + str(s.symbol))  
                            position_count +=1  

    record(Leverage = context.account.leverage)  
def get_position_count(context):  
    n = 0  
    for position in context.portfolio.positions.itervalues():  
        if position.amount != 0:  
            n += 1  
    return n


Hi Youness,

This error is being raised because you are comparing adx which is an array of rolling values to an int (35). Check out the documentation on talib. You probably want adx[-1] in that line!

Thanks again Jamie.

I've added [-1], it runs for a while then " Exception: inputs are all NaN ".

        high1 = history(15, "1d", "high")  
        low1 = history(15, "1d", "low")  
        close= history(15, "1d", "price")  
        adx = talib.ADX(high1[s], low1[s], close[s] , timeperiod=14)  
        if position_count < context.max_positions:  
            if  (current_position == 0)    :  
                        if (adx[-1]  > 35) :  

Hi Youness,

The talib.ADX function calculates rolling ADX values, and timeperiod corresponds to the rolling window length. It seems your timeperiod is too long for the length of data you're providing. Try increasing your history lookback, or decreaseing the timeperiod!

Hi Jamie.

I've fixed it by adding:


        high1= high1.fillna(0)  
        low1= low1.fillna(0)  
        close = close.fillna(0)  

Thanks!