ranaroussi / yfinance

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

isin to symbol converter using finance.yahoo.com #231

Closed FelixWeichselgartner closed 1 year ago

FelixWeichselgartner commented 4 years ago

Hey ranaroussi,

I have a request for a new feature. A conversion of isin to symbol. @JakobLachermeier and me found a way to convert the an isin to a symbol using the following:

curl https://query2.finance.yahoo.com/v1/finance/search?q=IE00BK1PV551&quotesCount=6&newsCount=0&enableFuzzyQuery=false&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&newsQueryId=news_ss_symbols&enableCb=false&enableNavLinks=false&vespaNewsTimeoutMs=600

this returns a json like this:

{"explains":[],"count":1,"quotes":[{"exchange":"EBS","shortname":"X(IE) MSCI W","quoteType":"ETF","symbol":"XDWL.SW","index":"quotes","score":20003.0,"typeDisp":"ETF","longname":"Xtrackers MSCI World UCITS ETF 1D","isYahooFinance":true}],"news":[],"nav":[],"lists":[],"totalTime":87,"timeTakenForQuotes":6,"timeTakenForNews":80,"timeTakenForAlgowatchlist":1,"timeTakenForPredefinedScreener":1,"timeTakenForCrunchbase":0,"timeTakenForNav":1}

From this you can extract the symbol of the isin. With this conversion you could implement an argument for an isin of your yfinance.Ticker class and not only for symbols.

Greetings Felix Weichselgartner

FelixWeichselgartner commented 4 years ago

possible implementation:

import yfinance as yf
import requests

def isin2stockprice(isin):
    request_string = f"""https://query2.finance.yahoo.com/v1/finance/search?q={isin}&quotesCount=6&newsCount=0&enableFuzzyQuery=false&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&newsQueryId=news_ss_symbols&enableCb=false&enableNavLinks=false&vespaNewsTimeoutMs=600"""
    r = requests.get(request_string)
    data = r.json()
    try:
        return yf.Ticker(data['quotes'][0]['symbol'])
    except IndexError:
        return None
anonz322 commented 4 years ago

Why not submitting a pull request ? You can find the TickerBase class in base.py (with a get_isin method doing the reverse operation via BusinessInsider, but tagged as experimental), you'll see that there is no verification of the ticker (you just end up with an empty dict if you look at .info on an invalid ticker), perhaps passing a "tick_is_ISIN" bool (defaulted to false) to the init which, if true turned to true, treat the ticker param as an isin ? Something like:

class TickerBase():
    def __init__(self, ticker, isin_code=False):
        if tick_is_ISIN: ticker = isin2ticker(ticker)
        self.ticker = ticker.upper()