yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

Question: Is there an efficient way to get the currency?

Open ymyke opened this issue 2 years ago • 7 comments

The history() function doesn't return the currency the prices are in.

In order to get the currency, one has to invoke the get_info() function, which is rather costly – on the order of a few seconds.

Is there a more efficient way to get the currency?

ymyke avatar Nov 23 '22 12:11 ymyke

Possible but slightly hacky. Yahoo returns currency with price data but no official way to add it to Pandas table. So question is where to put it?

Can add a .currency attribute to table but it'll disappear as soon as you modify table, e.g.:

df = yf.download(...)
print(df.currency)  # Good
print(df)  # Pandas won't show currency
df = df.copy()
print(df.currency)  # AttributeError exception

Or add a new currency property to Ticker - will persist but won't help download().

ValueRaider avatar Nov 23 '22 14:11 ValueRaider

Thanks! I tried this, however, it doesn't work:

df = yf.download("MSFT")
print(df.currency)  # produces AttributeError

Am I missing something?

ymyke avatar Nov 24 '22 07:11 ymyke

I saw that Yahoo returns some meta data. Example below. The currency is in there. Along with all kinds of other information that could be useful here and there.

Feature request: Would be great if – upon retrieval – you could simply store the raw "meta" dictionary in the Ticker object so it would be accessible if necessary/useful.

(Maybe this is more or less what you were suggesting above and what you were describing there is not how it works today but how it could work in the future?)

{'currency': 'USD',
 'symbol': 'COIN',
 'exchangeName': 'NMS',
 'instrumentType': 'EQUITY',
 'firstTradeDate': 1618407000,
 'regularMarketTime': 1669237204,
 'gmtoffset': -18000,
 'timezone': 'EST',
 'exchangeTimezoneName': 'America/New_York',
 'regularMarketPrice': 45.57,
 'chartPreviousClose': 43.39,
 'previousClose': 43.39,
 'scale': 3,
 'priceHint': 2,
 'currentTradingPeriod': {'pre': {'timezone': 'EST',
   'start': 1669194000,
   'end': 1669213800,
   'gmtoffset': -18000},
  'regular': {'timezone': 'EST',
   'start': 1669213800,
   'end': 1669237200,
   'gmtoffset': -18000},
  'post': {'timezone': 'EST',
   'start': 1669237200,
   'end': 1669251600,
   'gmtoffset': -18000}},
 'tradingPeriods': [[{'timezone': 'EST',
    'start': 1669213800,
    'end': 1669237200,
    'gmtoffset': -18000}]],
 'dataGranularity': '1m',
 'range': '1d',
 'validRanges': ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', 'ytd', 'max']}

ymyke avatar Nov 24 '22 09:11 ymyke

Oh it's not implemented, I'm just saying it could be but need to think best way.

If you have good idea then submit a PR to dev

ValueRaider avatar Nov 24 '22 12:11 ValueRaider

Seems like pandas does support adding extra properties to dataframes https://pandas.pydata.org/pandas-docs/stable/development/extending.html#define-original-properties

However it might be be better to use @ymyke suggestion and store it on the Ticker instead.

fredrik-corneliusson avatar Nov 24 '22 23:11 fredrik-corneliusson

Alright, here's the PR, @ValueRaider: https://github.com/ranaroussi/yfinance/pull/1198 – Turned out to be very easy.

ymyke avatar Nov 25 '22 07:11 ymyke

Note re download: It wouldn't be straightforward to add the functionality to the download method. Adding the info to the underlying dataframe object is not a solution since you can have several tickers with differing meta info (e.g., currencies) in one download call. So the information would have to be added to the dataframe itself, which would be a major interface change and maybe not advisable for the time being.

This means that both ways to retrieve several tickers simultaneously (yf.download and yf.Tickers) do not expose the meta info at this point.

ymyke avatar Nov 25 '22 07:11 ymyke