Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Systematic trading strategies - ADX and WVF

Hello, I have been trying to write this ADX trading strategy for a course I am taking but since I am not a programmer , I am not able to correct my mistake . Can someone please check the below algorithm and maybe show me how to correct it ?

The following error is in the last part of the algo (from if function onwards) : 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):  
    schedule_function(record_ADX, date_rules.every_day(), time_rules.market_close())  
def record_ADX(context, data):   

    context.security = sid(5061)  
    period = 14  
    H = data.history(context.security,'high', 2*period, '1d').dropna()  
    L = data.history(context.security,'low', 2*period, '1d').dropna()  
    C = data.history(context.security,'price', 2*period, '1d').dropna()  
    ta_ADX = talib.ADX(H, L, C, period)  
    ta_nDI = talib.MINUS_DI(H, L, C, period)  
    ta_pDI = talib.PLUS_DI(H, L, C, period)  
    if (ta_ADX> 20) and (ta_nDI > ta_pDI):  
            order_target_percent(context.security, -1)  
    elif (ta_ADX>20) and (ta_nDI < ta_pDI):  
            order_target_percent(context.security, 1)
4 responses

Hello Pascale,

Two people with this Python error in one day!

This is a case where using the in-IDE debugger is a great tool.

I copied your code and ran it. (It's better if you share a backtest than copied code - the copying process is fraught with risk of typos and whitespace issues) The error you get is pretty explicit:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()  
...
USER ALGORITHM:15, in record_ADX  
if (ta_ADX> 20) and (ta_nDI > ta_pDI):  

The error tells us the problem is in line 15. So I click on the number '69' in the left-hand margin of the IDE and the debugger is enabled. I press "build" and wait for it to get to 69. When it gets there, I type in the variable names:


ta_ADX  
> ta_ADX: ndarray

ta_nDI  
> ta_nDI: ndarray

ta_pDI  
> ta_pDI: ndarray  

So what you've got there is you're trying to evaluate if an array is larger than an integer in one case, and if an array is larger than an array in another - and Python doesn't do that. You need to specify which term in that array is the one you want to compare in each case. ta_ADX[-1] gets your code to run, but I don't know if that is what you want.

Attached is a running version, but I make no promises it does what you want.

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.

Thank you so much!!

I didn't know that this is the issue ... Here is how the strategy looks like now .

Attached here is the William VIX fix strategy but used in the opposite direction on the Boeing stock. Wen I used it in it's normal setting I made losses but using the opposite strategy made an average of 19.4% profits per year.

So here I tried to have only the abnormal results from my trend therefore I invested beta in SPY to kind of reduce the correlation with market returns. I was able to reach a beta of 0.07 with ADX