Pipeline: Changing daily data to weekly (using end of week prices) to calculate Factors (such as RSI)

What I am trying to do is take Q500US universe, resample it to weekly data (end of business week closes) then using that data to calculate RSI. I want to calculate 4 period RSI using weekly data (last 4 weeks of closes).

Panda's "resample" doesn't work in the pipeline. Any help here would be much appreciated.

15 responses

Maybe using numpy

# daily closes:
x = 100 * (1 + np.random.normal(0,0.01,252).cumsum())

# weekly closes:
w = x[::5]


i would use pipeline to get the data frame of daily prices. and use pandas to do the resampling to weekly. and then use talib RSI to get the RSI. just my 2 cents

I was thinking use the pipeline to filter stocks with WEEKLY RSI's below a certain level but it doesnt seem to be working. I figured out how to grab the latest weekly closes but I cant pass that into the input argument in the RSI calculation.

Do you have sample code of what you are stating above?

Perhaps try, Chris the .downsample(frequency) filter for example:

marketCap = Fundamentals.market_cap.latest.percentile_between(1, 99).downsample('week_start')


Hope this helps.

@Karl

Interesting. I was not aware of this before. Good to know - thanks!

Hey Karl, thanks for the reply

So I used the downsample method to get the latest weekly closes. I got this to work. The problem is I need to pass a numpy array of weekly closes into the RSI function (or most other Factors) to compute the weekly RSI. So, for example, if I wanted the 4 period WEEKLY RSI value I would need to pass in the last 4 weeks of closes (or more).

This is where I am stuck. The method you referred to above returns the latest, not a historical series or numpy array (which is what I need).

Any other ideas would be welcome.

Thanks

Hi Chris,

Would you be ok to share with us your snippets of codes where you got stuck? I'm sure the forum members would be happy to help suggesting how it may work to your purpose.

Cheers

Sure it is attached.

This codes throws an error at the moment

A reminder of what I am trying to do:

Goal here is to compute the WEEKLY 4 period RSI for the Q500US universe
I need to pass into the RSI factor an array of weekly prices, but I cant seem to do that.

8
Notebook previews are currently unavailable.

Hello Chris,

Did you finally get a solution for this? I am also trying to compute weekly RSI, but no luck so far.

Thanks!
Guillaume

3
Notebook previews are currently unavailable.

Thanks a lot for the link Umar! Great post. Probably the best way to combine factors on different timeframes. Very smart...

Umar, thanks a lot, this is indeed very useful! How can I set the RSI length to for instance 4, as per the original question of this post?

I have tried to change the line rsi = RSI() in the above code to rsi = RSI(inputs=[USEquityPricing.close], window_length=4), but when I add this to the code and print the values, I get odd values that do not match what a chart shows me.

Thank you for your help!

My mistake. It seems "downsample" just takes the daily value of the RSI (see notebook above ) and keeps this value for 1 week or any other time period choosen. It does not seem to answer to Chris initial question as he needed weekly data as input instead of daily. I found a post solving this in an algo but no answer inside a notebook. https://www.quantopian.com/posts/different-timeframes-in-factors-for-pipelines

I found another post on this topic https://www.quantopian.com/posts/how-to-resample-the-daily-usequitypricing-dot-close-to-weekly but it is also unsolved. As we cannot use pandas method "resample" here because input is a numpy array the only way to solve this is probably what Mathieu recommended

# weekly closes:
w = x[::5]


but the issue here is that this will not be end of week prices.

Never give up :-) At least it works

1