Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect close data from yahoo_finance in python

Tags:

python

finance

I am using yahoo_finance in python to pull stock data and for some reason, the get_prev_close() method is not returning the same data with every call.

Here is a simple example:

from yahoo_finance import Share
from time import sleep

while True:
    stock = Share('XLV')
    prevClose = float(stock.get_prev_close())
    print prevClose

    sleep(1)

For some reason this will print two different numbers seemingly at random. So for today I am getting 69.3 and 69.71 printed out. But since this is yesterdays close data, there should only be a single value.

Is this is known bug and is there a way around this?

like image 374
The Nightman Avatar asked May 13 '16 20:05

The Nightman


2 Answers

This issue has nothing with yahoo_finance python library. Inconsistent results from Yahoo Finance have been observed by many users. You can find complaints here forums.developer.yahoo and here different result. I've run couple of queries today and got different results as well. Probably you'd better find another source for the finance data.

like image 143
algor Avatar answered Nov 10 '22 20:11

algor


It appears that the realtime YQL data that the yahoo_finance package use are stale. 69.71 is the close on May 11. The correct value should be 69.3.

The easy solution is to either access the historical data table of the same package as in

>>> from yahoo_finance import Share
>>> stock = Share('XLV')
>>> data, = stock.get_historical('2016-05-12','2016-05-12')
>>> data['Close']
69.300003

Alternatively, grab the close data directly from the Yahoo/Finance website. (A lot faster and more flexible) When trying to replicate your situation, I don't get the flickering of values you report, but I do get a stale close (that of May 20th instead of May 23). Maybe the flickering you see is the result of the YQL sometimes going to a server with stale data, sometimes not. Here's code that gets the previous close from the historical data of yahoo_finance (a), from the realtime data of yahoo_finance (b), directly from the yahoo-finance real-time quotes (c) and directly from the yahoo-finance historical data (d).

import csv
import time
import datetime

import requests
import pytz

from yahoo_finance import Share

hist_url = 'http://real-chart.finance.yahoo.com/table.csv?s=XLV&a=4&b=23&c=2016&g=d&ignore.csv'
realtime_url = 'http://download.finance.yahoo.com/d/quotes.csv?s=XLV&f=p&e=.csv'

eastern = pytz.timezone('US/Eastern')

while True:
    stock = Share('XLV')
    a = stock.get_historical('2016-05-23','2016-05-23')[0]['Close']
    b = stock.get_prev_close()
    r = requests.get(hist_url)
    reader = csv.DictReader(r.iter_lines())
    c = float(next(reader)['Close'])
    d = float(requests.get(realtime_url).text)
    print datetime.datetime.now(eastern).time(), a, b, c, d
    time.sleep(120)

The close from stock.get_prev_close() in the 3rd column is consistently stale, while the other numbers are correct as per the following output:

09:24:51.582532 69.410004 69.69 69.410004 69.41
09:26:52.749902 69.410004 69.69 69.410004 69.41
09:28:54.589506 69.410004 69.69 69.410004 69.41
09:30:56.681914 69.410004 69.69 69.410004 69.41
09:32:58.255181 69.410004 69.69 69.410004 69.41
like image 40
Noyer282 Avatar answered Nov 10 '22 21:11

Noyer282