Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Ranking all stocks by performance

Hi all,

I'm a complete newbie to Quantopian and still trying to get the hang of the API and pipeline and was hoping someone could give me a hand with something.

What I'd like to do would be to simply rank a universe of stocks by their performance (i.e. change in their price) every month and simply go long on the bottom 10 and short on the top 10.

Could anyone possibly point me to where I could get some help with this?
Thanks in advance!

5 responses

Hi Karan,

Pipeline is definitely the best way to do this. I'd recommend going through the Getting Started Tutorial as well as the Pipeline Tutorial. In particular, the Pipeline Tutorial goes through a comparable example that should get you going in the right direction.

Once you feel comfortable with the API, I'd recommend checking out the Algorithmic Trading Tutorial by Sentdex which walks through the process of researching and analyzing a strategy on Quantopian.

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.

Hi Jamie,

Thanks for the response! I've spent a lot of time looking through those already, but what I'm struggling to do at the moment to adapt them to the actual backtester used by quantopian as I feel those are more geared towards the research environment?

I also went through sentdex's channel on youtube which was really helpful but again since I am still getting to grips with everything I am struggling to adapt ii, for example he uses the pipeline for equities of a particular sentiment factor but I am unsure how to rank them by their performance?

Hi Karan,

Lesson 12 of the Pipeline Tutorial covers the jump from research to the backtester. The way to think about the 'performance' of a stock is that it's just another factor that you can build in pipeline. In the Pipeline Tutorial, the main factor used in the final example is a percent_difference factor. The pipeline then spits out the top 25 and bottom 25 stocks based on the percent_difference factor. You could do something similar to get the bottom 10 and top 10 of a performance factor that you define.

I'm picturing something like this:

class PriceNDaysAgo(CustomFactor):

    inputs = [USEquityPricing.close]

    def compute(self, today, asset_ids, out, values):  
        out[:] = values[0]

...

# in initialize:

base_universe = Q1500US()

month_ago_price = PriceNDaysAgo(window_length=21)

performance = (USEquityPricing.close.latest - month_ago_price) / month_ago_price)

longs = performance.top(10, mask=Q1500US())  
shorts = performance.bottom(10, mask=Q1500US())  

I left out some imports and some other context in the algo, but try working that into lesson 12 of the Pipeline Tutorial.

Karan-

This does so with the Daily Chart. It would have to be adapted to actually work on the Monthly chart as you are requesting.

But I figure this might help you get started. It is adapted from another thread:

https://www.quantopian.com/posts/newbie-question-regarding-some-functions

Good Luck

Hi all, thanks again for the help and suggestions and I managed to put together my first algorithm by following the template of the pipline tutorial here as suggested.

Looking over the results though it seems to be making transactions every few days, or even every day in some cases and I don't understand why as I scheduled the function to run at the end of every month, can you see what I am doing wrong?