Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Inverse ranking of alpha factor

Hey guys, I am currently getting familiar with alphalens and started to test some factors. Now I have the problem that I do not know how to inverse a ranking. In the sense that the high numbers will be in the quantile to be shorted and the low numbers in the quantile to be longed.

The following line of code works fine for factors like roa, roe etc. But for RSI for example I saw that I could get some alpha if I reverse the ranking.

testing_factor = testing_factor.rank(mask=universe, method='average')

Is here someone who can let me know how this can be done and how I could get more control and info about this.

Thanks a lot in advance.

Kind regards

Marcel kresse

2 responses

Hi Marcel -

You could try simply multiplying the factor by -1. Then, when it ranked, the ranks will be flipped.

Note that the code for the built-in RSI is here. I wrote a custom factor based on the built-in RSI code. So, if you use it, you can do whatever you want to manipulate the ranking.

    class RSI(CustomFactor):  
        window_length = 15  
        inputs = (USEquityPricing.close,)

        def compute(self, today, assets, out, closes):  
            diffs = np.diff(closes, axis=0)  
            ups = np.nanmean(np.clip(diffs, 0, np.inf), axis=0)  
            downs = abs(np.nanmean(np.clip(diffs, -np.inf, 0), axis=0))  
            rs = ups/downs  
            rsi = 100.0-100.0/(1.0+rs)  
            out[:] = rsi  

Awesome, thanks a lot for your help :)