Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Iterating through QTradableStocksUS()

Am I able to iterate through QTradableStocksUS() in the research environment. I'm trying to test if the old tale of selling when RSI crosses 90 and buy when at 30 is true or not. I'm able to graph the results for a selective list of stocks which I choose but I would like the sample size to be much larger? Any thoughts on how to iterate through the given universe?

2 responses

It will be a challenge getting the notebook code 'as is' to work with a variable universe. The reason is the securities in the universe are not fixed. One day there may be 3200 securities the next there may be 3150. Additionally, the notebook is calculating returns based on pricing returned by pipeline. Pipeline prices are adjusted as of each day in the pipeline. To calculate returns one should use the get_pricing method which are adjusted as of a single date.

So, perhaps a different approach... To quickly check if a particular factor or signal has any merit we strongly urge using Alphalens. There is a good tutorial here https://www.quantopian.com/tutorials/alphalens . All one needs to do is encapsulate their signal into a pipeline factor and Alphalens does the rest. In this case one could create a custom factor something like this

class RSI_Open_Close(CustomFactor):  
    """  
    Custom factor to short when RSI crosses above 90  
    long when it crosses below 30. The output is 1, -1, 0  
    for long, short, hold.  
    """  
    inputs = [RSI()]  
    # Below is the window length of this factor and not the RSI  
    window_length = 2

    def compute(self, today, asset_ids, out, rsi):  
        low_rsi = 30  
        high_rsi = 90

        rsi_today = rsi[-1]  
        rsi_yesterday = rsi[0]

        # set crossing up assets to -1, crossing down to 1, others will be 0  
        crossing_up = np.where((rsi_today > high_rsi) & (rsi_yesterday <= high_rsi), -1, 0)  
        crossing_down = np.where((rsi_today < low_rsi) & (rsi_yesterday >= low_rsi), 1, 0)

        # combine the two series. this works because they are exclusive of each other.  
        combined_up_down = crossing_up + crossing_down

        out[:] = combined_up_down

Running Alphalens it does appear there is alpha but it's the reverse of what was expected. Positive returns when the RSI crosses above 90 and negative when it crosses below 30. However, the alpha is small and the alpha decay is substantial with really only the 1 day returns being good.

I know this wasn't the original question but the tools on Quantopian are designed to answer questions like this with minimal effort.

The attached notebook has the Alphalens analysis with some comments in the last cell. It's also a good example (with a small binning trick) of using Alphalens with factors which produce a discrete buy/sell signal. Play around with the notebook. Change the crossing points. Also change the stock universe. The number of periods and the length of periods in the analysis can also easily be changed. There are a number of different strategies associated with the RSI. Try inverting the logic to go long when crossing above 30 and short when crossing below 90.

Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Wow, thank you so much for going this in depth. I am still digesting it but will understand it soon. Thank you again Dan!