-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REV/MAINT: Switching back to Yahoo (temp) to deal with Google #1972
Changes from all commits
c066466
906fa73
f18b9c9
1e7e78d
920be8f
c1bf9c2
3056ecc
6010cab
bfe44ca
b9a76dd
3e56055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,9 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
from collections import OrderedDict | ||
|
||
import logbook | ||
import pandas as pd | ||
from pandas_datareader.data import DataReader | ||
import pytz | ||
from six import iteritems | ||
from six.moves.urllib_error import HTTPError | ||
|
||
from .benchmarks import get_benchmark_returns | ||
|
@@ -35,7 +31,7 @@ | |
|
||
# Mapping from index symbol to appropriate bond data | ||
INDEX_MAPPING = { | ||
'SPY': | ||
'^GSPC': | ||
(treasuries, 'treasury_curves.csv', 'www.federalreserve.gov'), | ||
'^GSPTSE': | ||
(treasuries_can, 'treasury_curves_can.csv', 'bankofcanada.ca'), | ||
|
@@ -91,7 +87,9 @@ def has_data_for_dates(series_or_df, first_date, last_date): | |
return (first <= first_date) and (last >= last_date) | ||
|
||
|
||
def load_market_data(trading_day=None, trading_days=None, bm_symbol='SPY', | ||
def load_market_data(trading_day=None, | ||
trading_days=None, | ||
bm_symbol='^GSPC', | ||
environ=None): | ||
""" | ||
Load benchmark returns and treasury yield curves for the given calendar and | ||
|
@@ -115,7 +113,7 @@ def load_market_data(trading_day=None, trading_days=None, bm_symbol='SPY', | |
A calendar of trading days. Also used for determining what cached | ||
dates we should expect to have cached. Defaults to the NYSE calendar. | ||
bm_symbol : str, optional | ||
Symbol for the benchmark index to load. Defaults to 'SPY', the Google | ||
Symbol for the benchmark index to load. Defaults to 'GSPC', the Yahoo | ||
ticker for the S&P 500. | ||
|
||
Returns | ||
|
@@ -272,7 +270,7 @@ def ensure_treasury_data(symbol, first_date, last_date, now, environ=None): | |
path. | ||
""" | ||
loader_module, filename, source = INDEX_MAPPING.get( | ||
symbol, INDEX_MAPPING['SPY'], | ||
symbol, INDEX_MAPPING['^GSPC'], | ||
) | ||
first_date = max(first_date, loader_module.earliest_possible_date()) | ||
|
||
|
@@ -356,93 +354,6 @@ def _load_cached_data(filename, first_date, last_date, now, resource_name, | |
return None | ||
|
||
|
||
def _load_raw_yahoo_data(indexes=None, stocks=None, start=None, end=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should've been removed in #1812 |
||
"""Load closing prices from yahoo finance. | ||
|
||
:Optional: | ||
indexes : dict (Default: {'SPX': '^SPY'}) | ||
Financial indexes to load. | ||
stocks : list (Default: ['AAPL', 'GE', 'IBM', 'MSFT', | ||
'XOM', 'AA', 'JNJ', 'PEP', 'KO']) | ||
Stock closing prices to load. | ||
start : datetime (Default: datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc)) | ||
Retrieve prices from start date on. | ||
end : datetime (Default: datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc)) | ||
Retrieve prices until end date. | ||
|
||
:Note: | ||
This is based on code presented in a talk by Wes McKinney: | ||
http://wesmckinney.com/files/20111017/notebook_output.pdf | ||
""" | ||
assert indexes is not None or stocks is not None, """ | ||
must specify stocks or indexes""" | ||
|
||
if start is None: | ||
start = pd.datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc) | ||
|
||
if start is not None and end is not None: | ||
assert start < end, "start date is later than end date." | ||
|
||
data = OrderedDict() | ||
|
||
if stocks is not None: | ||
for stock in stocks: | ||
logger.info('Loading stock: {}'.format(stock)) | ||
stock_pathsafe = stock.replace(os.path.sep, '--') | ||
cache_filename = "{stock}-{start}-{end}.csv".format( | ||
stock=stock_pathsafe, | ||
start=start, | ||
end=end).replace(':', '-') | ||
cache_filepath = get_cache_filepath(cache_filename) | ||
if os.path.exists(cache_filepath): | ||
stkd = pd.DataFrame.from_csv(cache_filepath) | ||
else: | ||
stkd = DataReader(stock, 'yahoo', start, end).sort_index() | ||
stkd.to_csv(cache_filepath) | ||
data[stock] = stkd | ||
|
||
if indexes is not None: | ||
for name, ticker in iteritems(indexes): | ||
logger.info('Loading index: {} ({})'.format(name, ticker)) | ||
stkd = DataReader(ticker, 'yahoo', start, end).sort_index() | ||
data[name] = stkd | ||
|
||
return data | ||
|
||
|
||
def load_from_yahoo(indexes=None, | ||
stocks=None, | ||
start=None, | ||
end=None, | ||
adjusted=True): | ||
""" | ||
Loads price data from Yahoo into a dataframe for each of the indicated | ||
assets. By default, 'price' is taken from Yahoo's 'Adjusted Close', | ||
which removes the impact of splits and dividends. If the argument | ||
'adjusted' is False, then the non-adjusted 'close' field is used instead. | ||
|
||
:param indexes: Financial indexes to load. | ||
:type indexes: dict | ||
:param stocks: Stock closing prices to load. | ||
:type stocks: list | ||
:param start: Retrieve prices from start date on. | ||
:type start: datetime | ||
:param end: Retrieve prices until end date. | ||
:type end: datetime | ||
:param adjusted: Adjust the price for splits and dividends. | ||
:type adjusted: bool | ||
|
||
""" | ||
data = _load_raw_yahoo_data(indexes, stocks, start, end) | ||
if adjusted: | ||
close_key = 'Adj Close' | ||
else: | ||
close_key = 'Close' | ||
df = pd.DataFrame({key: d[close_key] for key, d in iteritems(data)}) | ||
df.index = df.index.tz_localize(pytz.utc) | ||
return df | ||
|
||
|
||
def load_prices_from_csv(filepath, identifier_col, tz='UTC'): | ||
data = pd.read_csv(filepath, index_col=identifier_col) | ||
data.index = pd.DatetimeIndex(data.index, tz=tz) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
from toolz import merge | ||
|
||
from zipline import run_algorithm | ||
|
||
from zipline.data.bundles.core import register | ||
|
||
# These are used by test_examples.py to discover the examples to run. | ||
from zipline.utils.calendars import register_calendar, get_calendar | ||
|
@@ -69,6 +69,8 @@ def run_example(example_name, environ): | |
mod = EXAMPLE_MODULES[example_name] | ||
|
||
register_calendar("YAHOO", get_calendar("NYSE"), force=True) | ||
# for when we don't actually have a 'test' bundle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When do we have a 'test' bundle otherwise? Did this work before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we ever actually had a |
||
register('test', lambda *args: None) | ||
|
||
return run_algorithm( | ||
initialize=getattr(mod, 'initialize', None), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,3 +177,5 @@ | |
'YM': DEFAULT_ETA, # Dow Jones e-mini | ||
'YS': DEFAULT_ETA, # Silver e-mini | ||
} | ||
|
||
BENCHMARK_SYMBOL = '^GSPC' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the newer version works ok with yahoo again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes on my machine with a fresh zipline install it works.