Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem in calculating percentile score of current value compared to its history

Hi All,

I am new to quantopian. I am finding problem in calculating current percentile score of price to cash flow compared to its 5 year historical value. I have used following code in the 'customfactor'.

class PCFPecentile(CustomFactor):
inputs = [morningstar.valuation_ratios.pcf_ratio]
window_length = 1250
def compute(self, today, assets, out, ratio):
out[:] = stats.percentileofscore(ratio,ratio(1))

Could you please help me figure out my mistake.

Thanks

3 responses

Hi Mohit,

Welcome to Quantopian! This problem doesn't necessarily have a simple solution, but I can try to help you get started. First, do you think you could elaborate on how you want to compare the price to cash flow between yesterday and 5 years ago? Are you interested in how it's rank/percentile has changed?

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.

Hi Jamie,

Thanks for your reply.

I am trying to calculate the percent rank score of current price to cash flow to its 5 year history. I mean to say in which perecentile current P/CF lies if we consider the last 5 year data.

To give an example, for a data series of [ 5, 2,3,6,7,8,1], percentile score/rank of latest data point '1' would be 14.28.

Regards,
Mohit

Hi Mohit,

The inputs passed to the compute() function of a custom factor are each their own pandas DataFrame with securities along one axis, and datetimes along the other. According to the documentation for percentileofscore, the input to percentileofscore needs to be an array, so passing a DataFrame won't work, you'll have to use a for loop and look at the pcf_ratio of each stock in your universe, one at a time.