Previous post demonstrated pulling company balanced sheets and financial records using Yahoo Query Language (YQL) . Historical prices which is used to calculate price trends can also be obtained from YQL using following table “yahoo.finance.historicaldata”. The YQL statement is as followed:
select * from yahoo.finance.historicaldata where symbol in (“stock_sym1″,”stock_sym2”) and startDate = “2009-09-11” and endDate = “2010-03-10”
Note that for this method, multiple stocks can be retrieved at a time as highlighted in blue. This is a faster way compared to the method described in previous post using the Yahoo Finance API where only one stock’s data can be retrieved at a single run. However, the disadvantage of this method is that the time interval cannot be very large. Hence, this is for cases where there is a need to add more recent data of large quantity of stocks on a daily basis, for example, to a database.
The url generated from this query is as followed. The blue portion is the stock symbols, the orange is the start date and the green is the end date.
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%225FH.SI%22%2C%22A31.SI%22)%20and%20startDate%20%3D%20%222015-02-19%22%20and%20endDate%20%3D%20%222015-02-24%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
To retrieve the above using python, the same method can be employed as what is done previously by constructing the url and downloading the data using PATTERN module to download and processed the json information. Json data can be easily transformed to a pandas Data frame for further processing which can also be easily inputted to sql using the Pandas to_sql function. Note that the url would need to consist of the stock symbols, the start and end date.
How can I use the same YQL query for 100 symbols?
select * from yahoo.finance.historicaldata where symbol in (“stock_sym1″,”stock_sym2”) and startDate = “2009-09-11” and endDate = “2010-03-10”
Hi, one way is to put the symbols in a file and use file operation to read the lines and pass the data to a list.
select * from yahoo.finance.historicaldata where symbol in (“stock_sym1″,”stock_sym2”) and startDate = “2009-09-11” and endDate = “2010-03-10”
for this query How can I get recoerd for 100 symbols?
Hi Sreelatha, you mean where to get list of stock symbols? One way is described as in below post: https://simplypython.wordpress.com/2014/08/16/retrieve-all-stock-symbols-using-python/
Historicaldata in yahoo with yql stop working?
Hi Meir, i tried it one or two weeks ago. It is working then.
It stops working since early May 2017. It was working last month. I think Yahoo did something on the historicaldata table. Even on YQL console it has errors: https://developer.yahoo.com/yql/console/?q=desc%20yahoo.finance.quotes&env=store://datatables.org/alltableswithkeys#h=select+*+from+yahoo.finance.historicaldata+where+symbol+%3D+%22YHOO%22+and+startDate+%3D+%222009-09-11%22+and+endDate+%3D+%222010-03-10%22
Hi Ross, Thanks for highlighting. Yes, seems like not working nowadays.
This query works:
select * from pm.finance where symbol = “YHOO”
but you cant include startDate or endDate
Hi Jim, thank you for sharing the new query.