Back to Community
Adding a Custom Factor to Pipeline

from previous X periods to the current period and use this as a factor in a pipeline.

6 responses

Maybe something like this...

from quantopian.pipeline import Pipeline, CustomFactor  
from import USEquityPricing  
import numpy as np

class LowestLow(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.low]  
    # Set window_length to whatever number of days to look back  
    # As a default it is set to 10 days  
    # in the case where no window_length is given when instantiated.  
    # This can also be set/over-ridden as shown below:  
    # my_lowest_low_20 = LowestLow(window_length = 20)  
    window_length = 10  
    def compute(self, today, assets, out, low):  
        # The input 'low' is a Numpy array with low prices as axis 0 (total rows = window length)  
        # and axis 1 are all the assets (ie stocks and securities).  
        # Here we use the built in Numpy method to return the low in each row.  
        # The nanmin method is used because it excludes nans unlike the standard min  
        out[:] = np.nanmin(low, axis=0)  

You can then instantiate a factor from that class something like this...

    my_lowest_low_5 = LowestLow(window_length = 5)

my_lowest_low_5 = LowestLow(window_length = 5)

Would it show the min price over the 5 days in past starting from yesterday?

In function we set window_length = 20, but calling the function we are overwriting this value with 5 days past, is it correct?

Yes to both questions. Setting the window_length = 5 will overwrite the default value of 5. The factor will then return the min value of the 5 previous days beginning with yesterday’s data.

Good luck.

Thank you, and if I am doing like this

out[:] = np.nanmin(low[5: 26], axis = 0)

Would it b=pick up 21 day past starting from 5 days ago?

Sorry for so maybe obvious question, just new to this.

The input array is organized with a row for each trading day and a column for each asset. The first row (index 0) is the earliest date. The last row (index -1) is yesterday.

So you will need to do

out[:] = np.nanmean(low[0:-5], axis=0)

This should be what you want.

Good luck.

Thank you, Dan, you are saved me. Do you know if it is possible to find the date of this min price output. Generally to load time series. in pipeline.