Code Monkey home page Code Monkey logo

yahoo_quote_download's Introduction

yahoo_quote_download

Introduction

This project is for downloading Yahoo financial EOD (End-of-Day) data of stock and market indexes.

Background

Yahoo has provided the EOD financial data service for a long time and it has been well-used. However, starting on May 2017, Yahoo financial has terminated that service without warning. This is confirmed by a Yahoo employee in forum posts.

However, it is later found that despite that the service has been terminated, the Yahoo financial EOD data is still available, though with some authentication steps added and some changes in format. The technical details are described in a separate document.

This project provides a way to continue obtaining the same data.

Installation

This is a Python project. So Python should be installed first. This project works with either Python2 or Python3.

To install this project On Windows / Linux, you may simply do


pip install yahoo_quote_download

Data Download

The main entry point is a commandline application. To download EOD data for a ticker, please try


yqdownload -t ticker -f datafile [-b begindate] [-e enddate] [-m maxretries] [-v verbose] [-h]

where

  • ticker - (Required) the ticker to download, e.g., MSFT (Microsoft) or ^DJI (Dow Jones Industrial index). Please check Yahoo financial webpage for the tickers they use.
  • datafile - (Required) the file where the downloaded data is saved to.
  • begindate - (Optional) the beginning date of the download in the format of YYYY-MM-DD. If not provided, the data will be incrementally downloaded based on the data in the data file. If all fails, the default is 1970-01-01.
  • enddate - (Optional) the ending date of the download in the format of YYYY-MM-DD. If not provided, the default is the current day.
  • maxretries - (Optional) max number of retries. Occationally, the download will reach an error. This is the maximum number of retries in such cases. The default is 5.
  • verbose - (Optional) verbose level. The default is 1. You may use 0 to make it really quiet.
  • As usual, you may use -h or --help options to see all the supported options.

License

This code in this project is available through "Simplified BSD License".

yahoo_quote_download's People

Contributors

c0redumb avatar dannyben avatar edenpan avatar jimsparkle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yahoo_quote_download's Issues

No License

Hi,

Please include a license to this project. I would like to contribute but can't now because there is no license.

Thanks

Xero

The same feature set as before?

Not issues per se, except possibly in terms of complementing the documentation:

Is there also company/ticker status information? By not adding start, end and interval, like before?

Does the crumb expire, or can it be saved for use over time?

I will of course experiment with this.

'begindate' outside of time.mktime range

Hello,

The current implementation of the load_yahoo_quote function cannot use dates prior to 1970/01/01 due to the limited range of time.mktime. Would it be possible/relevant to modify the load_yahoo_quote function so that it accepts earlier dates?

On paper I think calendar.timegm would do the trick - it outputs prior dates as negative numbers, which is also the format accepted for parameter 'period1' in the Yahoo URL for dates before 1970/01/01. However I've tried to modify the function accordingly (note the date format change):

tb = calendar.timegm(time.strptime(begindate+":4","%Y-%m-%d:%H"))
te = calendar.timegm(time.strptime(enddate+":18","%Y-%m-%d:%H"))

and get an 'invalid literal for int() with base 10' ValueError in exchange, which seems weird not only because both lines output correctly when run in isolation outside of the function, but because the error is marked at line 84, which is the '# Prepare the parameters and the URL' comment!

AssertionError: 1 columns passed, passed data had 7 columns

Hi, thank you for your project that helps me a lot. But it seems has problem when i want the result output as 'dataframe'.
Here is the error log:

Traceback (most recent call last):  
  File "test_yqd.py", line 25, in <module>  
    test()
  File "test_yqd.py", line 18, in test
    load_quote('QCOM')
  File "test_yqd.py", line 12, in load_quote
    print(yqd.load_yahoo_quote(ticker, '20170515', '20170517',format_output = 'dataframe'))
  File "~/code/python/venv/lib/python2.7/site-packages/yahoo_quote_download-0.1-py2.7.egg/yahoo_quote_download/yqd.py", line 117, in load_yahoo_quote
    adf = pd.DataFrame.from_records(nested_alines, columns=cols)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 1269, in from_records
    coerce_float=coerce_float)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 7461, in _to_arrays
    dtype=dtype)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 7540, in _list_to_arrays
    coerce_float=coerce_float)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 7598, in _convert_object_array
    con=len(content)))
AssertionError: 1 columns passed, passed data had 2 columns

It seems the code return dataframe may need to change to :

    if format_output == 'dataframe':
		#nested_alines = [line.split(',') for line in alines[1:]]
		nested_alines = [line.split(',') for line in alines.split('\n')[1:]]
		cols = alines.split('\n')[0].split(',')
		# print(cols)
		adf = pd.DataFrame.from_records(nested_alines[:-1], columns=cols)
		return adf

unauthorized

I'm using this tool since a couple of years and it's excellent; anyway it's a couple of days that I am experiencing this problem (unauthorized error). I tried it from different places and with different ip addresses but still not solving the problem.
Might it be due to changes on Yahoo's policies? thanks

Consistent Error 401: Unauthorized

Although yahoo_quote_download has been downloading for a while, the latest test_yqd.py is now generating:

HTTPError: HTTP Error 401: Unauthorized

Any suggestions?

Occasional HTTPError 401 Unauthorized

Occasionally, the urlopen raise HTTPError with 401 Unauthorized response. I don't know what causes it (maybe too much request in a short time?)

At this time, I tried to catch those exceptions in my calling script and rerun the download after some timed sleep. But this is only a patch solution.

Any suggestions on what causes it?

Python 2.7

I don't have enough reputation points to leave a comment on stackoverflow, hence this note.
First, thanks for addressing the problem.
Second, I'm using Python 2.7 and replaced
import urllib.request, urllib.parse, urllib.error
with
from six.moves import urllib

when I run your test I get and error
yqd.py", line 39, in _get_cookie_crumb
alines = f.read().decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 59021: ordinal not in range(128)

Suggestions appreciated.

Consider adding a --version flag

Thanks for the nice utility.

It would be nice to have the customary --version flag, to just show version number.

In addition, I opened a small PR for minor README fixes.

javascript api

I'm trying to implement your workaround to obtain data for a web. I could get the crumb and overcome the cross domain limitations through a third party proxy. However I couldn't retrieve the data. Could you provide insights into how to do it or expand your solution for a javascript api?

This is what I've done so far:

// example.
var url="https://finance.yahoo.com/quote/%5EGSPC/history?p=%5EGSPC";

var cors_api_url = 'https://cors-anywhere.herokuapp.com/';

var requestParameters = {
method: "GET", //either POST or GET
url: url
};

doCORSRequest(requestParameters, function printResult(result) {
console.log(result);

var cs = result.indexOf('CrumbStore');
var cr = result.indexOf('crumb', cs + 10);
var cl = result.indexOf(':', cr + 5);
var q1 = result.indexOf('"', cl + 1);
var q2 = result.indexOf('"', q1 + 1);
var crumb = result.slice(q1 + 1,q2);

console.log("cs",cs);
console.log("cr",cr);
console.log("cl",cl);
console.log("q1",q1);
console.log("q2",q2);
console.log("crumb",crumb);

var url3 ="https://finance.yahoo.com/quote/XLF/history?period1=914299200&period2=1496376000&interval=1d&filter=history&frequency=1d"

var url2 = "https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=1493730135&period2=1496408535&interval=1d&events=history&crumb="+crumb;

console.log(encodeURIComponent(url2));

var urlGoogleFinance = "https://www.google.com/finance/historical?q=XLY&startdate=2001-12-31&output=csv";
var urlGoogleFinance2 = "https://www.google.com/finance/historical?q=GE&startdate=1990-06-30&output=csv";

var baseUri = "https://query.yahooapis.com/v1/public/yql?q=";
var uriQuery = encodeURIComponent("SELECT * from json where url='"+url3+"'"/+" and columns='Date,Open,High,Low,Close,Volume' "/);
var totalUri = baseUri + uriQuery+"&format=json";

console.log("totalUri", totalUri);

Plotly.d3.json(totalUri, function(readData) {
//console.log("readData",readData);
console.log("readData",readData);
console.log("readData",readData.query.results.row[0]);
console.log("readData",readData.query.results);
});

});

// main function
function doCORSRequest(options, printResult) {
var x = new XMLHttpRequest();
console.log(" cors_api_url + options.url", cors_api_url + options.url);
x.open(options.method, cors_api_url + options.url);
x.onload = x.onerror = function() {
printResult(
options.method + ' ' + options.url + '\n' +
x.status + ' ' + x.statusText + '\n\n' +
(x.responseText || '')
);
};
if (/^POST/i.test(options.method)) {
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
x.send(options.data);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.