Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
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.

Thanks in advance

21 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.

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

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

This is still the weekly RSI based off the daily RSI which not very usefull at all, especially as rsi is an oscilator.
Anyone has found a better solution?

Hi Pros,

I want to share with you something about rsi performance here in quantopian.

I was talking with team support this week about my concern of rsi   values in my pipelines vs other platforms like amibroker and teamviewer doesn't match. These platforms shows very similar numbers instead here in quantopian.

Ex If you make a simple pipeline screener with rsi 2 periods you will get only two values for daily timeframe, these values are 100 or 0.

Once i noticed about this, i wrote to the team support and the answer was  that quantopian"uses the mean of the differences while some sources use the exponentially weighted mean."

Im a fan of Mr. Connors strategies where rsi 2 period is a most value indicator in many of his strategies.In fact i reach to this thread looking for information about rsi in a weekly timeframe which is part of the strategy called "weekly mean revertion" from his last book called "The alfa formula". By the way this book was wrote with Mr @ChrisCain, the one that wrote in this thread before.

So, It seems that we need to build our custom factor rsi using exponential average in ups and downs. If so, any of you pros can share this custom factor ? will be good for many of beginners like me.

This the thread with Mr. Dan Whitnable from team support.

https://www.quantopian.com/posts/rsi-factor-documentation#5ee7dd52c1e75a0042976d6b

Thanks in advance.

@ Ramon Gonzalez

Try this.

class RSI(CustomFactor):  
    # Relative Strength Index using Wilder's smoothing  
    # Quantopian built-In RSI factor is Cutler's RSI which uses SMA smoothing and can't be used here  
    inputs = [USEquityPricing.close]  
    params = {'rsi_len' : 2,}  
    window_length = 60 # allow at least 4 times RSI len for proper smoothing  
    # The line below allows this factor as an input to other factors  
    window_safe = True  
    def compute(self, today, assets, out, close, rsi_len):  
        diffs = np.diff(close, axis=0)  
        count = len(diffs)  
        rsi_decay_rate = (1.0 - (1.0 / rsi_len))  
        rsi_weights = np.full(count, rsi_decay_rate, float) ** np.arange(count + 1, 1, -1)  
        ups = np.average(np.clip(diffs, 0, np.inf), axis=0, weights=rsi_weights)  
        downs = abs(np.average(np.clip(diffs, -np.inf, 0), axis=0, weights=rsi_weights))  
        out[:] = 100 - (100 / (1 + (ups / downs)))  

Hi Steve, thanks for your support. I wil be checking out this now!

Hi Steve, just noticed that you belong to Connors research team. Its really honored get your help and attention.

Regarding your kindle support about RSI custom formula, just want to say that results are more close of what can i see on Amibroker and Tradingview. Just to confirm, enclosed a simple pipeline using your rsi custom factor for SPY.

Amibroker RSI (2) 19-06-2020 = 22.77
Tradingview RSI (2) 19-06-2020 = 25.64
Rsi (params = {'rsi_len' : 2,} ) custom factor 19-06-2020 = 31.47 (Using window lenght = 60)

Comming from you i think this is the official formula that i need to use for connors strategies.

Thanks again

Hi again pros,

Just building a simple pipeline using the RSI from Steve posted above which is more accurate for 2 periods.

As this thread says,i m trying to get weekly RSI values but, like several of you ,no luck.

I tried with downsample('week_start') and resample('w') inside inputs params and close price but nothing.

Finally, could any pro solves this issue? thanks!!!

Attached my modest notebook.

Ramon.