-
Notifications
You must be signed in to change notification settings - Fork 679
Replace Yahoo iCharts API #331
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
Closed
+190
−91
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c86cbf8
Replaces ichart API for single-stock price exports from Yahoo, multi-…
rgkimball a9bcc9e
Restores change necessary for Google to function
rgkimball 605eb43
Fixes yahoo-actions per API endpoint update
rgkimball 77f349b
Update regex pattern for crumbs, per heyuhere's review
rgkimball 01cfe68
'v' is no longer a valid interval value
rgkimball 5de5d1e
Fixes Yahoo intervals and cases where the Yahoo cookie could not be e…
rgkimball 9e1c1e0
Implements multi-stock queries to Yahoo API
rgkimball 61fc09e
Adds a pause multiplier for subsequent requests from Yahoo, error han…
rgkimball 683ab75
Check object type before checking contents
rgkimball 9c1d506
Replacement regex logic for additional Yahoo cookie token structures,…
rgkimball 3561dbd
Improved error handling and refactoring test to best practices, per j…
rgkimball b3cc479
Applies additional null type, per OlegShteynbuk #342
rgkimball 159d25e
Update test to sort DF columns.
rgkimball 6324028
REL: Release 0.4.1 #315
rgkimball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,53 @@ | ||
import csv | ||
from pandas import to_datetime, DataFrame | ||
from pandas import (concat, DataFrame) | ||
from pandas_datareader.yahoo.daily import YahooDailyReader | ||
|
||
from pandas_datareader.base import _DailyBaseReader | ||
|
||
|
||
class YahooActionReader(_DailyBaseReader): | ||
|
||
class YahooActionReader(YahooDailyReader): | ||
""" | ||
Returns DataFrame of historical corporate actions (dividends and stock | ||
splits) from symbols, over date range, start to end. All dates in the | ||
resulting DataFrame correspond with dividend and stock split ex-dates. | ||
""" | ||
def read(self): | ||
dividends = YahooDivReader(symbols=self.symbols, | ||
start=self.start, | ||
end=self.end, | ||
retry_count=self.retry_count, | ||
pause=self.pause, | ||
session=self.session).read() | ||
# Add a label column so we can combine our two DFs | ||
if isinstance(dividends, DataFrame): | ||
dividends["action"] = "DIVIDEND" | ||
dividends = dividends.rename(columns={'Dividends': 'value'}) | ||
|
||
splits = YahooSplitReader(symbols=self.symbols, | ||
start=self.start, | ||
end=self.end, | ||
retry_count=self.retry_count, | ||
pause=self.pause, | ||
session=self.session).read() | ||
# Add a label column so we can combine our two DFs | ||
if isinstance(splits, DataFrame): | ||
splits["action"] = "SPLIT" | ||
splits = splits.rename(columns={'Stock Splits': 'value'}) | ||
# Converts fractional form splits (i.e. "2/1") into conversion | ||
# ratios, then take the reciprocal | ||
splits['value'] = splits.apply(lambda x: 1/eval(x['value']), axis=1) # noqa | ||
|
||
output = concat([dividends, splits]).sort_index(ascending=False) | ||
|
||
return output | ||
|
||
|
||
class YahooDivReader(YahooDailyReader): | ||
|
||
@property | ||
def service(self): | ||
return 'div' | ||
|
||
|
||
class YahooSplitReader(YahooDailyReader): | ||
|
||
@property | ||
def url(self): | ||
return 'http://ichart.finance.yahoo.com/x' | ||
|
||
def _get_params(self, symbols=None): | ||
params = { | ||
's': self.symbols, | ||
'a': self.start.month - 1, | ||
'b': self.start.day, | ||
'c': self.start.year, | ||
'd': self.end.month - 1, | ||
'e': self.end.day, | ||
'f': self.end.year, | ||
'g': 'v' | ||
} | ||
return params | ||
|
||
def _read_lines(self, out): | ||
actions_index = [] | ||
actions_entries = [] | ||
|
||
for line in csv.reader(out.readlines()): | ||
# Ignore lines that aren't dividends or splits (Yahoo | ||
# add a bunch of irrelevant fields.) | ||
if len(line) != 3 or line[0] not in ('DIVIDEND', 'SPLIT'): | ||
continue | ||
|
||
action, date, value = line | ||
if action == 'DIVIDEND': | ||
actions_index.append(to_datetime(date)) | ||
actions_entries.append({ | ||
'action': action, | ||
'value': float(value) | ||
}) | ||
elif action == 'SPLIT' and ':' in value: | ||
# Convert the split ratio to a fraction. For example a | ||
# 4:1 split expressed as a fraction is 1/4 = 0.25. | ||
denominator, numerator = value.split(':', 1) | ||
split_fraction = float(numerator) / float(denominator) | ||
|
||
actions_index.append(to_datetime(date)) | ||
actions_entries.append({ | ||
'action': action, | ||
'value': split_fraction | ||
}) | ||
|
||
return DataFrame(actions_entries, index=actions_index) | ||
def service(self): | ||
return 'split' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ideally return a more informative error (e.g. service name / url)
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.
Updated the error to include subclass and requested URL, and cleaned up my PR.