Back to Community
Custom factor for days since IPO date

I'm trying to develop a value oriented algorithm where I want to drop all stocks that were listed within a certain period since the IPO date. I can't seem to get a custom factor working right that gets the age in days of a stock. Here's what I tried...

class StockAge(CustomFactor):  
    inputs = [morningstar.share_class_reference.ipo_date]  
    window_length = 1

    def compute(self, today, assets, out, ipo_date):  
        delta = today - ipo_date[-1]  
        out[:] = delta.days  

But I can't seem to add this to my pipeline. I get an error when I do so. Could someone please suggest how I could do this?

Thanks,

Sunil

5 responses

If I remember correctly, Pipeline currently supports only fundamentals that are numbers and not dates or strings.

Thanks Luca, this makes sense. I put in a different approach to filtering out recently listed stocks.

I was able to extract some values using this code.

class StockAge(CustomFactor):
inputs = [morningstar.share_class_reference.ipo_date]
window_length = 1
def compute(self, today, assets, out, ipo_date):
date = ipo_date[0]
out[:] = date

However, it is not clear to me what format the output is in.

stockAge
Equity(23709 [NFLX]) 1.022112e+18
Equity(4831 [MGM]) -9.223372e+18
Equity(42596 [YELP]) 1.330646e+18
Equity(33070 [MPEL]) 1.166486e+18
Equity(46907 [WPG]) 1.400026e+18

Has someone figured out what the output of ipo_date means? is it seconds since IPO?

You need to convert the ipo datetime to a panda Timestamp with the same timezone for subtraction. Seems to work for a few I tested it on.

class StockAge(CustomFactor):  
    inputs = [morningstar.share_class_reference.ipo_date]  
    window_length = 1

    def compute(self, today, assets, out, ipo_date):  
        for ii, d in enumerate(ipo_date[0,:]):  
            ipo = pd.Timestamp(d, tz='UTC', offset='C')  
            delta = today - ipo  
            out[ii] = delta.days