Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Any way to select a random symbol?

Hi, I'm interested in building a strategy that works based on evaluating randomly-selected stock symbols, but the API doesn't seem to allow that. In particular, the data object is only prepopulated with the sids it finds at compile-time; even if I try something like this (just as a test):

def initialize(context):  
    context.stocks = []  
    while len(context.stocks) < 200:  
        randId = int(random.random()*30000)  
        context.stocks.append(sid(randId))  

I get a compile-time error, "The sid(id) method takes one parameter."

I was really hoping to just have something that can buy stocks selected at random (I'm calling it a "monkey with a dartboard") and hold on to them until a certain condition happens, but it doesn't look like the Quantopian API allows for this. Do I really just need to generate a list of 200 at the outset and essentially hardcode them into my algorithm?

2 responses

Hi J,

Before executing your code, we parse it for any build errors and security violations. During this stage, we need the stock to be initialized in your universe.

There are 3 ways to reference securities in your algorithm: manually, set_universe, and Fetcher. It sounds like the last two options are what you're looking for. Set_universe will select a random basket of stocks based on their traded dollar volume. Or alternatively, you can import a random list of stocks in a CSV file via fetcher to screen your strategy. Hope that helps!

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.

Ah, cool, I must have overlooked the explanation of what set_universe does. Thanks, that does exactly what I want!