ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
13.24k stars 2.34k forks source link

Some symbols doesn't work as the others does. Am i #1677

Closed emrahyalcin23 closed 1 year ago

emrahyalcin23 commented 1 year ago

Simple code that reproduces your problem

This does work (Symbol = EREGL, timerange)

from pandas_datareader import data as pdr import yfinance as yf yf.pdr_override() pdr.get_data_yahoo('EREGL.IS', start='2023-08-12', end='2023-09-05')

This does work as well (Symbol = BOBET, time : last day)

from pandas_datareader import data as pdr import yfinance as yf yf.pdr_override() pdr.get_data_yahoo('BOBET.IS', start='2023-09-07')

But this does Not work (Symbol = BOBET, timerange )

from pandas_datareader import data as pdr import yfinance as yf yf.pdr_override() pdr.get_data_yahoo('BOBET.IS', start='2023-08-12', end='2023-09-05')

The Error =>

Exception("%ticker%: Period 'max' is invalid, must be one of ['1d', '5d']")

and there are a couple more symbols that doesn't work. I tried many symbols and most of them works perfectly. am I missing something here?

Debug log

AttributeError: 'IndentLoggerAdapter' object has no attribute 'handlers'

Bad data proof

No response

yfinance version

0.2.22

Python version

No response

Operating system

No response

rickturner2001 commented 1 year ago

Upon trying this on my machine, this is what I have gathered:


import yfinance as yf
from pandas_datareader import data as pdr

yf.pdr_override()

def successful_fetch():
    pdr.get_data_yahoo('EREGL.IS', start='2023-08-12', end='2023-09-05')

def unsuccessful_fetch():
    pdr.get_data_yahoo('BOBET.IS', start='2023-08-12', end='2023-09-05')

successful_fetch()
unsuccessful_fetch()

This code as we expect, fails when fetching the data form BOBET.IS, and the error message is in a sense misleading. In order to better understand this, we can try sniffing the HTTP traffic generated by our Python script. This should technically result in 2 API calls

the first request is made to https://query2.finance.yahoo.com/v8/finance/chart/EREGL.IS?period1=1691787600&period2=1693861200&interval=1d&includePrePost=False&events=div%2Csplits%2CcapitalGains

and the second one: https://query2.finance.yahoo.com/v8/finance/chart/BOBET.IS?period1=1691787600&period2=1693861200&interval=1d&includePrePost=False&events=div%2Csplits%2CcapitalGains

both values for period1 and period2 are respectively 1691787600 and 1693861200, so yfinance is not at fault as far as composing the URLs for the API requests.

This means that something's going on in the other end (yahoo finance's official API)

Within the JSON for both responses there is a field called validRanges which is different for the two tickers

EREGL.IS

"validRanges": [
            "1d",
            "5d",
            "1mo",
            "3mo",
            "6mo",
            "1y",
            "2y",
            "5y",
            "10y",
            "ytd",
            "max"
          ]

BOBET.IS

"validRanges": [
            "1d",
            "5d"
          ]

My confident guess is that, by not having access to max as a valid range, then we are not allowed to specify a range.

Secondary Issues


import yfinance as yf
from pandas_datareader import data as pdr

yf.pdr_override()

def get_five_day_bobet():
    return pdr.get_data_yahoo('BOBET.IS', period="5d", interval="1d")

bobet = get_five_day_bobet()
print(bobet)

This time we are fetching bobet data with a legal period of 5d but the output is only for 1 day

[*********************100%%**********************]  1 of 1 completed
            Open  High        Low      Close  Adj Close    Volume
Date                                                             
2023-09-08  37.0  40.0  36.919998  38.220001  38.220001  12454019

GET request endpoint: https://query2.finance.yahoo.com/v8/finance/chart/BOBET.IS?range=5d&interval=1d&includePrePost=False&events=div%2Csplits%2CcapitalGains

ValueRaider commented 1 year ago

This certainly isn't fault of yfinance, just need to visit webpage to see BOBET.IS is unusual.

I don't know how yfinance can handle better, because this what Yahoo says. Maybe even more emphasis on Does Yahoo actually have the data? in bug report?

emrahyalcin23 commented 1 year ago

So I will issued it to Yahoo Finance instead of yfinance?

rickturner2001 commented 1 year ago

This certainly isn't fault of yfinance, just need to visit webpage to see BOBET.IS is unusual.

I don't know how yfinance can handle better, because this what Yahoo says. Maybe even more emphasis on Does Yahoo actually have the data? in bug report?

It is broken on the official website, however, they also do display 30 days woth of data, which we currently are not able to access through the API.

Maybe leave the issue open, someone might be able to replicate the behavior of the official website (again, the yfinance API only provides the last trading day for this particular ticker).

rickturner2001 commented 1 year ago

BOBET.IS

@emrahyalcin23 It's probably not worth your time. Just find a website on the internet that cares for Turkish markets. I hope this helps

import requests

def compose_url( from_: int, to: int, period=60):
    return f"https://www.isyatirim.com.tr/_Layouts/15/IsYatirim.Website/Common/ChartData.aspx/IndexHistoricalAll?period={period}&from={from_}&to={to}&endeks=BOBET.E.BIST"

from_ = 20230902000000
to = 20230909235959

url = compose_url(from_, to)
res = requests.get(url)

try:
    json = res.json()
    print(json)
except Exception as e:
    print(e)

This should return you the data that you're looking for. Maybe try to study how the API operates.

ValueRaider commented 1 year ago

they also do display 30 days woth of data

30 days of intraday data. You can access this, and also the single 1D interval, through API.

emrahyalcin23 commented 1 year ago

@rickturner2001 Thank you for your advice and help. I will do that.