Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Impossible to follow change in fundamentals (e.g. change in inventory / sales) over time with get_fundamentals?

Hi,

I think the answer to this is "it's impossible", but I wanted to confirm:

Is it possible to rank stocks / select stocks based on changes in their fundamental data over time? For example, I want to look at stocks whose own ratio of inventory / sales has changed (i.e. not the stocks with the highest inventory / sales at a given point in time, but stocks with a high change in their own inv / sales). From what I understand we can't yet do this with fundamentals.

I'm relatively new to Python but thought maybe it would be possible to create a list of, say, sales over time. Then run get_fundamentals in before_trading_start to pull the sales figure for the stock and .append it to the list. Would that work?

Thanks!!

3 responses

Yes, or you could query fundamentals within Pipeline and inside your CustomFactor code, you have access to a numpy array the N previous values of any number, including the fundamental number you care about...

As Simon said, you have to build your own CustomFactor to be used with Pipeline (code not tested):


class RatioChange(CustomFactor):  
    """  
    Calculates the percent change of the ratio of the two inputs over the given window_length.  
    """  
    inputs = [morningstar.something1,  
                      morningstar.something2]

    def compute(self, today, assets, out, something1_data, something2_data):  
        last  = something1_data[-1] / something2_data[-1]  
        first  = something1_data[0] / something2_data[0]  
        out[:] = (last - first) / first        # we divide by first to normalize the change (percentage): easier to rank


You should also be able to easily calculate the moving average and exponential moving average of the ratio change (as usual, code not tested):

from quantopian.pipeline.factors import SimpleMovingAverage, ExponentialWeightedMovingAverage

class SimpleMovingAverageRatioPercentChange(SimpleMovingAverage):

    inputs = [morningstar.something1,  
                      morningstar.something2]

    def compute(self, today, assets, out, something1_data, something2_data):  
        data = something1_data / something2_data  
        prct_change = np.diff(data, axis = 0)   # absolute change in the ratio  
        prct_change /= data[:-1]                        # percentage change  
        SimpleMovingAverage.compute(self, today, assets, out, prct_change)      


class ExponentialWeightedMovingAverageRatioPercentChange(ExponentialWeightedMovingAverage):

    inputs = [morningstar.something1,  
                      morningstar.something2]

    def compute(self, today, assets, out, something1_data, something2_data, decay_rate):  
        data = something1_data / something2_data  
        prct_change = np.diff(data, axis = 0)   # absolute change  
        prct_change /= data[:-1]                # percentage change  
        ExponentialWeightedMovingAverage.compute(self, today, assets, out, prct_change, decay_rate)