Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie Trying to Lag Returns by one Month in Pipeline

Hello,

I'm trying to create a factor that allows me to take the average between one month price change and 12 month price change, with the 12 month price change excluding most recent month - i.e. lagged by one month. I've got one month price change using: pchg_1mo = Factors.Returns(window_length = 30). I was also getting 12 month price chang using: pchg_12mo = Factors.Returns(window_length = 252) to get the returns, but I'm getting stuck with regard to lagging them.

I tried using pchg_1mo = Factors.Returns(window_length = 30).shift(-30), but get an error that the returns object has no attribute of shift. Then I thought I could use prices(assets, start, end, frequency='daily', price_field='price', symbol_reference_date=None, start_offset=0) and just set the start_offset=-30, but then I realized I'm not sure I know how to set dynamic start and end dates for that. Is that possible? If so, how would I do that?

Has anyone a good method of lagging returns by 1 month with dynamic date? My pipeline is using base_universe of QTradableStocksUS, and so just trying to get the two returns columns mentioned above and take the average between them.

THANK YOU IN ADVANCE!
-> Nate

2 responses

One could write a custom factor. However, since you are already defining a custom factor for 'Previous', maybe just use that? The 12 month returns excluding the previous month would simply be:

    price_12mo_ago = Previous(inputs = [USEquityPricing.close], window_length = 252)  
    price_1mo_ago = Previous(inputs = [USEquityPricing.close], window_length = 21)  
    pchg_12mo_excl_prev_mo = (price_1mo_ago / price_12mo_ago) - 1.0

Note also that one should use 20 or 21 days to get monthly returns and not 30. It assumes trading, and not calendar, days.

Good luck.

Thanks a ton @Dan!