Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to calculate the 5 year mean revenue growth?

Hi everybody,

I'm quite new to quantopian and I am trying to build a custom pipeline factor that calculates the mean total revenue growth over 5 years.
But whenever I want to pass the quantopian.pipeline.data.morningstar.total_revenue.latest - Object into my factor, I get a big error : "Can't compute windowed expression MeanUniques([Latest(...)], 1275) with windowed input Latest([Fundamentals.total_revenue], 1)."

My approach was:

import numpy as np  
from quantopian.pipeline import CustomFactor, Pipeline  
from quantopian.research import run_pipeline  
from quantopian.pipeline.data.morningstar import Fundamentals as morningstar

class MeanUniques(CustomFactor):  
    inputs = [morningstar.total_revenue.latest]  
    window_length=255*5  
    def compute(self, today, asset_ids, out, values): # values = 2D-Array(Size: window_length * nr_symbols)  
        out[:] = np.mean( np.unique(values, axis=0), axis=0) # ok this here would only calculate the 5 yr mean revenue, but understanding it would be a starting point for me... In the end I want to calculate the growth

def make_pipeline():  
    mean_growth = MeanUniques()  
    return Pipeline(columns={"growth" : mean_growth }  
                   )  

Does anybody have a better idea how to get the data of older fundamental data (that isn't "latest")? If it is possible to just read out the older data (e.g. 1 years, 2 years, 3 years ago) without needing to import all the window_length in between, would also make the computation much smaller I guess.

Unfortunately in the custom pipeline examples, there is no example utilizing fundamental data...

Thank's a lot in advance for any help, I'm kind of stuck here.

4 responses

ah, short addon: I want to calculate the ANNUAL 5 year revenue growth. (I didn't find yet, where to differ between quarterly data and annual data)

Would this work for you?

def make_pipeline():
tr1 = GetHistoricalVal(inputs=[Fundamentals.total_revenue], window_length=1)
tr1250 = GetHistoricalVal(inputs=[Fundamentals.total_revenue], window_length=1275)
tr5y_avg = (tr1/tr1250)/5

Then somewhere else you can either make quantiles with the info or sort by tr5y_avg.
I have have used both of those methods in an algorithm.

Or if you want to keep the factor you could use something like this:
class Factor_2(CustomFactor):
inputs = [Fundamentals.total_revenue]
window_length=255*5
def compute(self, today, asset, out, inputs):
out[:] = np.mean(inputs, axis=0)

Ah, you mean defining GetHistoricalVal() like in this post:

class GetHistoricalVal(CustomFactor):  
    def compute(self, today, assets, out, data):  
        out[:] = data[0]  

(from: https://www.quantopian.com/posts/long-term-fundamental-strategy-30-percent-annual-return )

Thanks, that could work indeed...