Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to fetch_csv in handle_data?

I would like to fetch information from the web (csv, json, etc...) from a url in handle_data or anywhere other than def initialize... It looks like fetch_cvs only runs in initialize and the problem for me is that initialize only runs once.

How do you get the most up to date information about stocks once the script is running? I have a url that I can generate that gives me today's winners,losers another good information. I would like to update my universe with this "external" data.

Jonathan

8 responses

I believe if you put it into initialize, in live trading, it will get done every night around midnight. That's the best you can do.

In backtesting, the CSV is loaded once (at the start of the backtest) and the data is fed in point-in-time as it becomes available in your "date" column.

In live trading, your CSV is read nightly by the algorithm in preparation for the trading day. You can update the file until midnight EST. If you wish to add new data (for example, the most current signals), you can do this by appending the new data to the bottom of the file. Make sure you don't change the historical contents of the file, and only append new data, since we don't keep a copy of your CSV. 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.

Hi Alisa,

Would you be so kind as to further elaborate what you mean by "the data is fed in point-in-time"? I suspect my confusion may be addressed if I understand that.

Here is what my understanding is and where I'm getting confused when it comes to fetch_csv:

First of all, I am guessing that what happens when I hit ctrl+B is that whatever data that is fed in through initialize(context) is stored in the data[] dictionary and subsequently iterated through line by line. The function handle_data(context, data) is then called at every iteration / line.

Similarly if fetch_csv[] is read once, then that must mean that it is stored in memory as well and there has to be some form of iteration through it.

So now what happens if I have a 60 minute by minute apple stock prices obtained from symbol() but as well, a csv file containing other apple metrics only at the 15min and 30 min mark, resulting in two iterations of different length. My guess is that the all the data is stored in memory, as in data becomes of

                                                    dimensions: = #time-stamps  
                                                                             x # symbols  
                                                                             x # extra metrics from csv  
                                                                             x # symbol parameters (e.g. close, open etc)  

Is that correct?

If so, then what happens if I have multiple lines in my csv with the same timestamp and ticker?

Would you be able to provide me a general explanation of how the quantopian software reconciles this?

Thanks a lot for any insight!

Ray

You cannot have data more frequent than daily, and I believe multiple rows that have the same day is an error. Likewise you cannot update data, only append a new day's quote to the bottom.

Every call for handle_data that day will have just that day's value.

Hi Simon,

I don't get why I can't have minute by minute data? When one attempts to run a back-test it allows you to pick minutely or hourly doesn't it?

Yes, but not for fetched data.

Hmm, however in this backtest by Accern, https://www.quantopian.com/posts/accern-news-and-blog-backtest-results-using-quantopian-link-to-pdf-report-attached, they have massive csv files that have minute by minute data.

I haven't looked at the accern stuff.