Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What is the difference between context.stocks and data inside the handle_data function?

When I'm using

for stocks in context.stocks:  
    ....  

I'm getting a return of about 50%. But when I'm using

for stocks in data:  
    .......  

the total return is very much different for the same time series (about 11% at a point compared to previous 34%). I was hoping if anyone could tell me what is the difference between this two objects.

I'm trying to use data, because I now want to import a csv and my understanding is that the fetch_csv maps the csv data with '***data***' object in handle_data function.

Thanks.

5 responses

This is the backtest, where I'm using

for stocks in context.stocks:  
    ......  

Stocks might not be in data if they haven't traded yet today. Furthermore, data will contain the benchmark security.

Simon can you elaborate on more on what the difference is? I'm also curious about this.

Recall that context tracks your algorithm itself. Things like your portfolio, performance, leverage...etc. If you have no position in the company, it wont be in your context. Data tracks your universe of companies. Your universe is the list of companies that you are tracking information on and could plausibly trade, but may not.

Thanks Harrison, so does data change every day then? I am using a pipeline screen and updating the universe in before_trading_start to that pipeline. So I'm thinking that data might exclude companies that I have a position in if the current day's value causes it to be excluded in the screen. Is that right?