Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to access to history of factors in pipeline?

this is part of the code from the tutorials.
now I want to do more by comparing the recent 20 days' mean_10.
how to get this data?
seems pipeline only gets mean_10 of yesterday.
Thanks.

def make_pipeline():  
    # Base universe set to the Q1500US.  
    base_universe = Q1500US()

    # 10-day close price average.  
    mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=base_universe)

    # 30-day close price average.  
    mean_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30, mask=base_universe)

    percent_difference = (mean_10 - mean_30) / mean_30  
4 responses

Unless I'm not understanding your question, you can get the mean (ie the average) of the last 20 trading days close prices with the following factor.

 # 20-day close price average.  
 mean_20 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=20, mask=base_universe)

The 'window_length' is the number of days to average.

I mean i want to get mean_10 of several days ago, like the day before yesterday.
not make window length 20.

let's say today is Jan 31,
mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=base_universe)
this gets mean_10 of Jan 30, right?
now i want mean_10 of Jan 29,..., Jan 4.

Ahhhh. You'll need a small custom factor for that. Maybe something like this...

import numpy as np  
class SMA_10_back_2(CustomFactor):  
    """  
    Compute 10 day average close price but start 2 days ago  
    """  
    inputs = [USEquityPricing.close]  
    # need to fetch 12 days worth of data (10 day sma + 2 day look back)  
    window_length = 12 

    def compute(self, today, assets, out, close):  
        # yesterday is -1, two days ago is -2, etc  
        lookback = -2  
        out[:] = np.nanmean(close[0:lookback], axis=0)