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

I have the following code:

def tickers():  
    return (  
            "YELP",  
            "P",  
            "BBRY",  
            "MBLY",  
            "GPRO",  
            "INVN",  
            "FEYE",  
            "TWTR",  
            "CRM",  
            "AMZN",  
            "BIDU",  
            "JD",  
            "QIHU",  
            "WB",  
            "YOKU",  
            "BITA",  
            "JRJC",  
            "INO",  
            "ISIS",  
            "TKMR",  
            "JNUG",  
            "JDST"  
           )

def initialize(context):  
    context.stocks = symbols(*tickers())

def handle_data(context, data):  
    for stock in context.stocks:  
        # Do something with stock  

Which actually contains two errors according to the Q framework. The first is there is no sid(...) referenced, so it will not Build. Second is (after I put in a random reference to a sid to avoid the first error) the call to symbols(...) fails with "KeyError: 'YELP'". Can anyone help with this?

7 responses

Hi,

symbols() requires a static list of strings so you can't pass in a variable. This works for me:

def initialize(context):  
    set_symbol_lookup_date('2014-06-26')  
    context.symbols = symbols(  
            "YELP",  
            "P",  
            "BBRY",  
            #"MBLY",  
            "GPRO",  
            "INVN",  
            "FEYE",  
            "TWTR",  
            "CRM",  
            "AMZN",  
            "BIDU",  
            "JD",  
            "QIHU",  
            "WB",  
            "YOKU",  
            "BITA",  
            "JRJC",  
            "INO",  
            "ISIS",  
            "TKMR",  
            "JNUG",  
            "JDST"  
           )  

I'm not sure which GPRO you were referring to so you have to adjust the lookup time.

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.

Hello Thomas, thanks for getting back with me.

The use of * before the tickers() call should "expand" the tuple into an argument list, so this should not be an issue. (I have used this method for years in Python programming.)

Also, what about the no "sid" reference issue?

Sorry for not being clear. What you wrote is valid python code but it doesn't work because of a limitation of our system. symbols() does actually not get executed, so that list never gets created. Instead, we do static code analysis to look at the arguments that are passed to it. That's why you have to pass in a list of strings directly.

That is unfortunate, as I was looking to use this programmatically later after loading a list of symbols from a csv file.

This use case is supported by using fetch_csv to create a custom universe: https://www.quantopian.com/posts/define-custom-universe-via-fetcher-using-the-new-universe-func-callback

And I understand any time your incoming csv drops a ticker from the list (you don't want to be trading that stock anymore), you could first exclude it from buying and after your trading rules have sold the shares at some point, then use context.symbols.remove(sid) to have it out of the mix entirely.

Here's an example showing that the list of sids persists in data, even though context.symbols has been modified. I suspect that the contents of data cannot be modified on-the-fly; the algorithm needs to be stopped and re-initialized.

Under live trading, my impression is that the algo effectively gets shut down and re-initialized overnight, so would it be possible in this fashion to drop sids from data, based on the incoming .CSV file contents? Or does the list of sids need to persist in data for record keeping (even though positions have been closed out)?

I guess a use case here would be if the algo needs the absolute maximum number of sids allowed in its universe, but the list of securities needs to change over time.

def initialize(context):  
    context.symbols = symbols('SPY','QQQ')  
    context.bar_count = 0  
def handle_data(context, data):  
    # output context.symbols and data before and after removing QQQ  
    if context.bar_count == 0 or context.bar_count == 11:  
        print 'bar count: ' + str(context.bar_count)  
        print 'context.symbols'  
        for stock in context.symbols:  
            print stock.symbol  
        print 'data'  
        for stock in data:  
            print stock.symbol  
    context.bar_count += 1  
    # remove QQQ  
    if context.bar_count == 10:  
        context.symbols.remove(context.symbols[1])