Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Question about data.history

Hello!

I am trying to fetch a large amount of historical data using the function data.history.

I have the symbols I would like to fetch data for:
context.security = symbols('CASH,EUR,USD','CASH,GBP,USD')

Now I would like to fetch the past 10 years of 'Close' values for these symbols:
data.history(context.security,'close','10 Y','1d')

Note that I have 10 Y in there instead of 3600 since anything above 365 does not appear to work. However the syntax above also does not work and I have not been able to find a straightforward answer to this issue. Does anybody know what the syntax for the above line is? Thank you very much.

8 responses

bump?

A few things...

Not sure what the symbols 'CASH,EUR,USD' are. Those aren't stocks or ETFs. You need to enter in actual security symbols something like this

context.security = symbols('IBM', 'SPY')

One can't use '10Y' as a parameter, however, using '1d' frequency you should be able to look back 10 years. Something like this

approx_10_years = 252 * 10  
close_prices_for_10_years = data.history(context.security, 'close', approx_10_years,' 1d')

An issue you will have in the backtester is historical data only goes back to 2002. Therefore the earliest date one can run a 10 year lookback is 2012. Also, rather than using the 'data.history' method, it will be faster if you use the pipeline. It's purpose is to speed up data fetches.

See attached backtest for a 10 year lookup in action. Check the logs to verify the lookup date.

Good luck.

But that's the thing:

approx_10_years = 252 * 10
close_prices_for_10_years = data.history(context.security, 'close', approx_10_years,' 1d')

does not seem to work. I receive the error message "Historical data requests for durations longer than 365 days must be made in years"

Hmm. Are you using the Quantopian online IDE? The above algo runs for me and fetches 10 year data.

Sorry for the delayed reply. I am just using Spyder at the moment.

*with the Ibridgepy package

i should clarify that i am attempting to automate a strategy in interactive brokers

Ah, Ibridgepy has their own implementation of the 'history' method and may differ from the zipline/Quantopian method. The syntax and restrictions may be different. Maybe reach out to Ibridgepy support. I've had good luck with them getting back (though it has often taken a few days).

Good luck getting up and running with IB.