1

I am using Python 2.7 and trying to execute a fairly straight forward code. I'm not sure why I am getting an error. The code fails when downloading the date from yahoo.

from matplotlib.finance import quotes_historical_yahoo
from datetime import datetime
from dateutil.relativedelta import relativedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import norm

if __name__ == '__main__':
n_shares=200
confidence_level=0.99
nunDays=15
z=norm.ppf(confidence_level)
ticker= 'IBM'
begdate = (2014, 11, 1)
enddate = (2014, 11, 30)
x =quotes_historical_yahoo(ticker,begdate,enddate,asobject=True,adjusted=True)
ret = (x.aclose[1:]-x.aclose[:-1])/x.aclose[:-1]
netPosition=n_shares*x.close[-1]
VaR=netPosition*z*np.std(ret)*np.sqrt(nunDays)
print "Net Value of Holding=",netPosition
print "Value at Risk (VaR)=", round(VaR,4), "over the next",nunDays, "Days"

I get the following error

Traceback (most recent call last):
  line 17, in <module> x = quotes_historical_yahoo(ticker,begdate,enddate,asobject=True,adjusted=True)
line 236, in quotes_historical_yahoo fh = fetch_historical_yahoo(ticker, date1, date2, cachename)
line 197, in fetch_historical_yahoo
with contextlib.closing(urlopen(url)) as urlfh:
line 154, in urlopen
return opener.open(url, data, timeout)
line 437, in open
response = meth(req, response)
line 550, in http_response
'http', request, response, code, msg, hdrs)
line 469, in error
result = self._call_chain(*args)
line 409, in _call_chain
result = func(*args)
line 656, in http_error_302
return self.parent.open(new, timeout=req.timeout)
line 437, in open
response = meth(req, response)
line 550, in http_response
'http', request, response, code, msg, hdrs)
line 475, in error
return self._call_chain(*args)
line 409, in _call_chain
result = func(*args)
line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

Process finished with exit code 1

Any help is most appreciated. Thanks

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I am using matplotlib.finance the tag for matplotlib.finance does not exist. The error is in the matplotlib.finance function quotes_historical_yahoo – Raza Akhtar May 27 '17 at 17:38
  • I haven't used `matplotlib.finance` before, but I got curious and started reading the documentation. But at least in `matplotlib 2.02` there is no function called `quotes_historical_yahoo`, only `quotes_historical_yahoo_ochl`. What version are you using? – Thomas Kühn May 27 '17 at 17:50
  • I am using matplotlib 2.02. The documentation is out of date, the function quotes_historical_yahoo_ochl is no longer valid and will give a syntax error. https://matplotlib.org/api/finance_api.html states that This module is deprecated in 2.0 and has been moved to a module called mpl_finance – Raza Akhtar May 27 '17 at 18:25
  • The matplotlib documenation is **not** out of date. There is a version of the documentation for each matplotlib version available and all examples are in sync with the respective versions. Since matplotlib 2.0.2 is not yet 2.2 the functions `quotes_historical_yahoo_ochl` and `quotes_historical_yahoo_ohlc` are both valid. There is an example [here](https://matplotlib.org/examples/pylab_examples/finance_demo.html), which is working just fine with matplotlib 2.0.2. Of course you need to make sure the ticker is valid and produces some output for the dates in question. – ImportanceOfBeingErnest May 27 '17 at 22:12
  • Thanks double checked and I don't have the correct version for matplotlib. Tried to install and keep getting different types of errors – Raza Akhtar May 28 '17 at 02:53
  • I just came across [this question](https://stackoverflow.com/questions/44040042/yahoo-finance-ichart-service-availability) where an answer links to forum in which people complain about the yahoo finance API not working correctly anymore. This might as well be the reason for the problems you're having. – ImportanceOfBeingErnest May 30 '17 at 23:08

0 Answers0