Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Design patterns: Dealing with more than 500+ stocks in total? Can you use fetch_csv outside of 'initialize'?

Hi.

I've invested some time reading the Quantopian documentation so we could port our algorithm to the Quantopian platform.

My big question is: can I have a CSV that has signal information for more than 500 stocks, and only trade on the relevant stocks once I do day-by-day filtering based on fundamentals?

For a contrived example, let's say I have a special signal file that contains the # of tweets about each stock on the NYSE, and it's going back 5 years.
Question #1) Is there a max_size limit to my csv?

Then, question #2: Apparently the universe_func can return a set of at max 500 securities. Can my fetcher be called every day, and then let the universe_func callback whittle down my list to the securities in my CSV (intersect) those that match certain fundamental criteria? I'm happy with only getting BAR data for 500 stocks a day, as long as that list of stocks can change.

Thanks. I'm excited about Quantopian. There's just a little bit of a learning curve.

7 responses

Your CSV can have any number of stocks, but you can only receive data for (and trade upon) 500 securities at a time. The file limit is 100MB, but the larger the file, the longer it takes to read it. The algo has 6 minutes to fully ingest the data, and if its not done, a timeout error is thrown.

You're on the right track with universe_func :) You can use it to create a dynamic changing universe and whittle down your list. If you need an example, take a look at this thread.

Enjoy working on your algo!

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.

We're also making it easier to process large trading universes. Karen announced the kickoff of that here, and you can watch her webinar to see examples. We've been working on it this summer, and a lot of progress has been made.

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.

Thanks for the prompt replies & attention.

Seriously!

"You can use [universe_func] to create a dynamic changing universe"

I still don't understand. Can I call universe_func multiple times?

Here is how I understand the lifecycle of a quantopian algo:

(Program starts)
[initialize function is called]
   [[inside initialize, fetch_csv is called]]  
   [[the universe_func callback for fetch_csv then gets a list of fundamental data for the current day and whittles down my list]]  
[the day's trading begins]
[it's the next day.  how do I re-adjust my universe based on the dynamic, changing fundamentals?]

I think the answer I'm looking for, the design pattern, is inside my initial (and only) universe_func(context, fetcher_data) callback invocation, I should store fetcher_data on context. Then in my before_trading_start() function, I can look at context.fetcher_data and re-adjust my Universe/set

Hi Zach,
I just spent some time trying to find something that will work for you. I did come up with something, it's not perfect, but it works.

When an algo starts up, the order of the function calls is initialize -> before_trading_start -> universe_func, so you are not able to access the fetcher symbols from within before_trading_start. However, you can access the before_trading_start data within the universe func.

The trick is that you are not allowed to have > 500 sid objects in your algo, but you can create a list of their N ticker symbols using sid.symbol. You can do your fundamentals screen first and save the set of symbol strings that passed your screen as a context variable, then you can then filter the symbols in your fetcher file by removing the ones that did not pass the fundamentals screen.

Here is a basic example of what I'm talking about. It's a bit hacky, but it works, I hope this helps you get going again.

Best,
David

Thanks David.

I solved it using a similar solution.

It took me a while to realize that there is no limit on the number of securities that your fetch_csv could get data for, AND that the data is automatically slurped into the "data" parameter to handle_data.

:-)

Right now I'm trying to figure out why my Backtests are getting stuck at 0%..

It's probably taking a while to crunch the large amount of data. Try kicking off a full backtest and coming back to it (you can close the tab, the full backtests run in the cloud and the data gets saved).