Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to determine best stock from list?

I have a list of stocks that I would be interested in investing in, however, I would like to loops through to find which are the top performers.
My pseudo code would be something like this:

stocks = [sid(####),sid(####),sid(####),sid(####),sid(####),sid(####)]
for x in stocks
find stock value for 4 days ago, 3 days ago, 2 days ago
if value 4 days ago < value 3 days ago < value 2 days ago
then place stock in new vector

I know matlab and VBA pretty well - coding logic isn't the challenge for me but understanding the syntax is. I would appreciate any help at all: how to find stock prices and then how if the logic is true place stock number in new vector?
Thanks!

4 responses

See https://www.quantopian.com/help#ide-history and the section on Day Units. You can get a Pandas dataframe of the trailing daily closing prices, if those will work for you. I've attached an example to illustrate.

Hey thanks
In line 20 what does the 5 do? and in line 25 the p[#] refers to the day look back?
I was looking at one of the sample code offered by Quantopian there is a function that prevents investing unsettled cash how is that incorporated?

Have a look at the help page regarding what is returned by:

price = history(5,'1d','price')  

The first 4 rows are the daily closing prices for the past 4 trading days, and the last row is the most recent minutely closing price.

p[#] refers to the day look back?

Yes, with the oldest data is indexed with a zero.

Regarding unsettled funds, the brute-force way to avoid placing orders when orders are still open:

if get_open_orders():  
        return  

There are more refined ways to do it, depending on your need.

I suggest playing around with the debugger and adding some print statements to see what's going on.

Grant

Calvin,

If you're looking to find the best stocks based on your custom criteria, try using the Pipeline API and here are the docs. This allows you to screen through 8000+ securities, filter down based on your criteria, return up to 500 securities to trade, and then rank them based on your factors.

Check it out! And if you're looking for other examples, try searching the community for similar algos people have shared.

Alisa

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.