Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Previous SMA in Pipeline

Hello all,

Just need some help with something I can't find the reference for...

In Pipeline, I am using the Q1500 and SimpleMovingAverage. I do not know how to call the previous SMA.

For instance:

def make_pipeline():
base_universe = Q1500US()

mean_5 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=5, mask=base_universe)  
--> how would I call mean_5's previous value here? <--

I appreciate anyone's assistance.

Thank you.

Danny

1 response

@Daniel

The built in factors generally return the latest values (in this case the latest average or mean). In order to get a previous value you will need to write a small custom factor. Maybe something like this.

class PreviousMean(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.close]

    # Set window_length to 1 day more than length of mean length  
    window_length = 6  
    def compute(self, today, assets, out, close):  
        # Take the mean of previous days but exclude the latest date  
        out[:] = np.nanmean(close[0:-1], axis=0)

I've attached a notebook with this in action.