Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issues calculating rolling max value

Overall, what I am trying to accomplish is to calculate the drawdown percentage from the 52 week high (ATHs might work too). It was not obvious to me if there was a factor to do this already so I attempted to create a custom factor. I am having issues calculating the trailing max value, which I would use as the 52 week high. I am still getting the hang of quantopian so apologies if I am missing something obvious.

I am receiving the below error. I believe this is more of a numpy error but I am curious if anyone has advice for either fixing this issue (I'm still looking into it) or demonstrating an alternative way of calculating the drawdown percent.

ValueError: could not broadcast input array from shape (252,8474) into shape (8474)

2 responses

The error ValueError: could not broadcast input array from shape (252,8474) into shape (8474) stems from the following code

out[:] = (t_max - values) / t_max

The issue is (t_max - values) / t_max is an array with 252 rows (one for each day set by the window_length parameter) and 8474 columns (one for each asset). The out variable expects a single value for each asset so a 1 dimensioned array with length 8474 is expected. That is why the error is saying it cannot 'broadcast' one shape array into another shape.

There is a built in factor called MaxDrawdown (https://www.quantopian.com/docs/api-reference/pipeline-api-reference#quantopian.pipeline.factors.MaxDrawdown) however, that doesn't seem to be what you are looking for? If I understand correctly, the goal is to find the percent change from the 52 week high to the current close price? Something like this

(last_close - high_52_week) / high_52_week

This could be done in a custom factor like this

class DrawdownFactor(CustomFactor):  
    # Input the high and the close prices  
    inputs = [USEquityPricing.high, USEquityPricing.close]

    # Look back 1 year for the 52 week high  
    window_length = 252

    def compute(self, today, asset_ids, out, highs, close_price):  
        # Get the highest high for each asset  
        # Specify axis=0 since days are the rows and asset values are in each column  
        # The result will be a 1 dimensioned array  
        highest_high = np.nanmax(highs, axis=0)

        # Get the last row of close_prices which will be a 1 dimensioned array of the latest close price  
        latest_close = close_price[-1]

        # Output the percent change. The result will be a 1 dimension array with a value for each asset  
        out[:] = (latest_close - highest_high) / highest_high

You could also incorporate the number of days since the high. Here is a post which shows a factor for that which may be helpful
https://www.quantopian.com/posts/days-since-52-week-hi-slash-lo

Hope that helps.

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 @dan, this is exactly what I was looking for. I also realized my math was wrong on the percentage change. I appreciate the help.