Back to Community
Runtime Error on Algo Identifying Pull-Back Extremes

I made an algorithm that attempts to identify extreme pull backs. However, I received the following runtime error.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I believe the following if-then statement may have triggered the runtime error:

if RSI[stock] < context.Low_RSI and open > close and close < ema_result and (open-close) > 0.7*(high-low) and current_position == 0 :
number_of_shares = int(cash/current_price)
order(stock, number_of_shares)

How would I incorporate a.any() or a.all() into my code in order to stop getting the runtime error?

5 responses

Could you share more of the code so we can help debug? If you don't want to share the entire algorithm, can you share how you're creating RSI and ema_result?

This will give more context to the error and then we'll get your algo back up and running :)

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.

Bump!

I have the same issue and it's happening with the RSI trigger.

I'd also like to know how to have my RSI trigger only be assessed when i'm actually scheduling the function to run (usually about every 60 minutes). When i plug in any(...rsi trigger language...) it seems to trade a lot more frequently than the indicated schedule function times.

Thanks!

Clone Algorithm
20
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a0b62b2e92c0f41c9d76329
There was a runtime error.

Hello David,

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

I cloned your algo and ran it. 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:69, in myfunc  
if RSIvalue > RSIbuy and context.portfolio.positions[context.xiv].amount == 0 and len(get_open_orders()) == 0:  

The error tells us the problem is in line 69. 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:

RSIvalue  
> RSIvalue: ndarray

RSIbuy  
>   30  

So what you've got there is you're trying to evaluate if an array is larger than an integer - and that isn't something that Python is going to do for you. You need to specific which term in that array is the one you want to compare to "30". RSIvalue[0] gets your code to run, but I doubt that's exactly what you want. (I note that all of the values of that array are NaN, so I think you're going to have another problem to figure out pretty soon!)

P.S. It's generally better to ask a new question than re-open one that is old - lots has changed on the platform. In this case, it's just a generic Python error, so it's not a big deal.

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 Dan,

Wasn't able to quite follow what you were getting at other than the RSIvalue created an array that had no numbers in it to even compare.

I was trying to compare the last value in the RSI array (the latest 10-day RSI close value based on current price of XIV) to see if that was crossing above 30 or crossing below 75 to trigger a trade. After googling a bit more i figured out what you meant by putting [0] at then. Given I want the last/latest value in the array though i'm going to use [-1]. However, I still don't think the algo is working as intended...

Clone Algorithm
20
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a0c5e6d63b904417cc27cd0
There was a runtime error.

If you intended to test whether or not the strategy makes money, then it is working exactly as intended! You learned this strategy is truly terrible.

This is the way it usually works. Most ideas don't pan out - there are far more unsuccessful strategies than successful ones. The question is, can you learn from the failures and test new ideas fast enough to succeed?