I am getting some very weird results using Python and yahoo-finance package. When I run my function on individual stock symbols the function performs OK. But if I put them in a loop (hope later on to expand the list), I got some error codes from YQL. Have anyone encountered similar errors or did I miss anything? See the code below and outputs. Sorry relatively new to Python. Thanks for any help!
t1=m1('AAPL')
t2=m1('MSFT')
df1=['MSFT','APPL']
print(t1)
print(t2)
for stock in df1:
print(type(stock))
for stock in df1:
m1(stock)
Adj_Close Close High Low Open Symbol \
Date
2016-01-04 102.612183 105.349998 105.370003 102.000000 102.610001 AAPL
2016-01-05 100.040792 102.709999 105.849998 102.410004 105.750000 AAPL
2016-01-06 98.083025 100.699997 102.370003 99.870003 100.559998 AAPL
2016-01-07 93.943473 96.449997 100.129997 96.430000 98.680000 AAPL
Volume price_gap target
Date
2016-01-04 67649400 NaN 0.026703
2016-01-05 55791000 0.003797 -0.028747
2016-01-06 68457400 -0.020933 0.001392
2016-01-07 81094400 -0.020060 -0.022598
Adj_Close Close High Low Open Symbol \
Date
2016-01-04 53.015032 54.799999 54.799999 53.389999 54.320000 MSFT
2016-01-05 53.256889 55.049999 55.389999 54.540001 54.930000 MSFT
2016-01-06 52.289462 54.049999 54.400002 53.639999 54.320000 MSFT
2016-01-07 50.470697 52.169998 53.490002 52.070000 52.700001 MSFT
Volume price_gap target
Date
2016-01-04 53778000 NaN 0.008837
2016-01-05 34079700 0.002372 0.002185
2016-01-06 39518900 -0.013261 -0.004971
2016-01-07 56564900 -0.024977 -0.010057
<class 'str'>
<class 'str'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in _request(self, query)
119 try:
--> ====120 _, results = response['query']['results'].popitem()
121 except (KeyError, StopIteration, AttributeError):
AttributeError: 'NoneType' object has no attribute 'popitem'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in _request(self, query)
122 try:
--> 123 raise YQLQueryError(response['error']['description'])
124 except KeyError:
KeyError: 'error'
During handling of the above exception, another exception occurred:
YQLResponseMalformedError Traceback (most recent call last)
<ipython-input-144-fb031b50c461> in <module>()
11 for stock in df1:
12 print(type(stock))
---> 13 m1(stock)
14
15 for stock in df1:
<ipython-input-143-7724677970e7> in m1(msft, d1, d2)
1 def m1(msft,d1='2016-1-4',d2='2016-1-7'):
2 msft=Share(msft)
----> 3 msft_p = msft.get_historical(start_date=d1, end_date=d2)
4 msft_prices=pd.DataFrame(msft_p)
5 msft_prices[['Open','Close','Volume','High','Low','Adj_Close']]=msft_prices[['Open','Close','Volume','High','Low','Adj_Close']].apply(pd.to_numeric)
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in get_historical(self, start_date, end_date)
340 try:
341 query = self._prepare_query(table='historicaldata', startDate=s, endDate=e)
--> 342 result = self._request(query) 343 if isinstance(result, dict): 344 result = [result]
//anaconda/lib/python3.5/site-packages/yahoo_finance/__init__.py in _request(self, query)
123 raise YQLQueryError(response['error']['description'])
124 except KeyError:
--> 125 raise YQLResponseMalformedError()
126 else:
127 if self._is_error_in_results(results):
YQLResponseMalformedError: Response malformed.
It looks like this functionality has been discontinued: https://forums.yahoo.net/t5/Yahoo-Finance-help/ichart-not-working-in-my-app/td-p/253560
Here's a workaround (grab the data directly from the history pages):
import urllib2
from BeautifulSoup import BeautifulSoup as bs
def get_historical_data(name, number_of_days):
data = []
url = "https://finance.yahoo.com/quote/" + name + "/history/"
rows = bs(urllib2.urlopen(url).read()).findAll('table')[0].tbody.findAll('tr')
for each_row in rows:
divs = each_row.findAll('td')
if divs[1].span.text != 'Dividend': #Ignore this row in the table
#I'm only interested in 'Open' price; For other values, play with divs[1 - 5]
data.append({'Date': divs[0].span.text, 'Open': float(divs[1].span.text.replace(',',''))})
return data[:number_of_days]
#Test
for i in get_historical_data('amzn', 5):
print i
Output:
{'Date': u'Jun 02, 2017', 'Open': 999.0}
{'Date': u'Jun 01, 2017', 'Open': 998.59}
{'Date': u'May 31, 2017', 'Open': 1000.0}
{'Date': u'May 30, 2017', 'Open': 996.51}
{'Date': u'May 26, 2017', 'Open': 995.0}