Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Historical Averages for Custom Fundamental Metrics

I'm somewhat new to Python and new to NumPy and Pandas. I wanted to post a question about averaging together fundamental data. I couldn't find any posts on this topic specifically, but I could be mistaken. I would think it could be helpful for others as well.

The important thing to learn is how to pull in and manipulate several fundamental data points into a custom metric and then how to rank the top and bottom stocks according to this metric, to order short and long positions. In other words, top ranked (highest value) custom PE metric stocks are shorted; bottom ranked custom metric PE stocks are purchased long.

Let's say I want to create my own custom metric (Marketcap/Net Income) for Price/Earnings - assume morningstar doesn't have a built in PE metric.

I want to get the average of my custom PE metric over the past 2 years. In other words, the average marketcap over 2 years (let's say 500 trading days) divided by the average net income over the past 8 quarters.

Eventually, I may want to compare this historical average custom metric to a current custom metric - let's say the same custom PE metric above, but just for the most recent period, divided by the average historical custom PE metric. I haven't factored this comparison into the attached model, but it's where I would go next if I can figure it out.

I've attached kind of a Frankenstein algorithm pieced together from Jamie and Karen Rubin's code. It doesn't work as is. My question is: how can I make it work, and how can I improve on it?

1 response

I dont' have time to code this out for you, but you need to do something like this, which would be much simpler:

  • Compute mean values for your individual fundamental factors over a period of time. In the docs https://www.quantopian.com/help#quantopian_pipeline_CustomFactor there is an example for computing median. You could easily adapt it to compute the mean.
  • Then divide the two factors. This will give you a third factor that you can then use in your pipeline.

Hope that helps.

Sunil