ranaroussi / yfinance

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

incorrect 52 week high and low (compare with Yahoo) #1986

Closed leaderdevil closed 4 months ago

leaderdevil commented 4 months ago

Describe bug

incorrect 52 week high and low values (compare with Yahoo). It is the same as regular market day high and low values.

Simple code that reproduces your problem

import yfinance as yf
msft = yf.Ticker("PTT.BK")
hist = msft.history(period="1mo")
msft.history_metadata

Debug log

N/A

Bad data proof

The result returned from yfinance is

{ ..., "dataGranularity": "1d", "exchangeName": "SET", "exchangeTimezoneName": "Asia/Bangkok", "fiftyTwoWeekHigh": 32.75, "fiftyTwoWeekLow": 32.0, "symbol": "PTT.BK", "timezone": "ICT", ... ] }

But the 52 Week High and Low in Yahoo Finance (https://finance.yahoo.com/quote/PTT.BK/key-statistics/) are 36.5 and 31.25 respectively.

202407161640

yfinance version

0.2.40

Python version

3.9.7

Operating system

Windows 10 Pro

Michiel-Vanzeir commented 4 months ago

Reproducing the problem

I was able to reproduce the incorrect 52-week high and low data using the tickers "PTT.BK" and "KBC.BR". The high and low values retrieved appear to be the same as the regular daily market high and low values, which are correct.

Temporary solution

Despite extensive troubleshooting, I haven't yet identified the bug causing this issue. However, I found a temporary workaround using the Ticker().info attribute from the yfinance library. Below is the code example for this workaround. I will continue to debug the problem. Feel free to let me know if you have any other questions.

Coded workaround

import yfinance as yf

msft = yf.Ticker("PTT.BK")
msft_info = msft.info

fifty_two_week_high = msft_info.get('fiftyTwoWeekHigh')
fifty_two_week_low = msft_info.get('fiftyTwoWeekLow')

print(f"52-Week High: {fifty_two_week_high}")
print(f"52-Week Low: {fifty_two_week_low}")
ValueRaider commented 4 months ago

I have a hunch ... what does period=1y metadata say?

Michiel-Vanzeir commented 4 months ago

Here's the metadata for 1y

{
  "chart": {
    "result": [
      {
        "meta": {
          "currency": "THB",
          "symbol": "PTT.BK",
          "exchangeName": "SET",
          "fullExchangeName": "Thailand",
          "instrumentType": "EQUITY",
          "firstTradeDate": 1007607600,
          "regularMarketTime": 1721291580,
          "hasPrePostMarketData": false,
          "gmtoffset": 25200,
          "timezone": "ICT",
          "exchangeTimezoneName": "Asia/Bangkok",
          "regularMarketPrice": 32.75,
          "fiftyTwoWeekHigh": 32.75,
          "fiftyTwoWeekLow": 32.25,
          "regularMarketDayHigh": 32.75,
          "regularMarketDayLow": 32.25,
          "regularMarketVolume": 10370364,
          "chartPreviousClose": 32.5,
          "previousClose": 32.5,
          "scale": 3,
          "priceHint": 2,
          "currentTradingPeriod": {
            "pre": {
              "timezone": "ICT",
              "start": 1721271600,
              "end": 1721271600,
              "gmtoffset": 25200
            },
            "regular": {
              "timezone": "ICT",
              "start": 1721271600,
              "end": 1721295000,
              "gmtoffset": 25200
            },
            "post": {
              "timezone": "ICT",
              "start": 1721295000,
              "end": 1721295000,
              "gmtoffset": 25200
            }
          },

It appears to be about the same as period=1mo.

leaderdevil commented 4 months ago

@Michiel-Vanzeir Thank you for the workaround solution. The solution is work! As of the workaround solution works, I would close the issue.