Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to pass inputs into a custom factor together with the corresponding datetime index?

In the following example:

class PriceRange(CustomFactor):  
    """  
    Computes the difference between the highest high and the lowest  
    low of each over an arbitrary input range.  
    """  
    inputs = [USEquityPricing.high, USEquityPricing.low]  
    window_length = 252

    def compute(self, today, assets, out, highs, lows):  
        out[:] = np.nanmax(highs, axis=0) - np.nanmin(lows, axis=0)  

The inputs are ndarray objects, without the corresponding datetime index. How can we pass in the corresponding datetime index at the same time? Or is there a way to reconstruct the correct datetime index inside the compute method?

I would like to reconstruct the dataframe inside the compute method so as to do weekday resampling.

Thanks a lot for helping!

1 response

I am now using the following approach:

from zipline.utils.calendars import get_calendar

def get_trading_index(today, num_days):  
    up2today = get_calendar('NYSE').schedule[:today]  
    return up2today.iloc[len(up2today)-num_days:len(up2today)].index  

The returned index is well-aligned as checked in the reseach environment :)