yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

Add Search Utility

Open avhagedorn opened this issue 1 year ago • 7 comments

Generalizes isin search to support new types of search queries.

Use case: I would like to be able to keep all yahoo finance code contained in the yfinance lib. Right now to perform a ticker search, I have to perform the following in my application:

    response = requests.get(
        "https://query1.finance.yahoo.com/v1/finance/search",
        params={"q": q, "newsCount": 0, "quotesCount": 5, "enableFuzzyQuery": False},
        headers={
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
        },
    )

Related to https://github.com/ranaroussi/yfinance/issues/837

avhagedorn avatar May 27 '24 14:05 avhagedorn

Give an example use code, so people can try out.

ValueRaider avatar May 27 '24 16:05 ValueRaider

Sorry, by example use code do you mean providing a snippet here or adding to the README?

In either case, here's a quick snippet.

import json
from yfinance.utils import search

user_input = input("Enter a ticker query: ")
search_result = search(user_input, newsCount=0)            # Just search for tickers
search_result_quotes = search_result.get("quotes", [])

print(json.dumps(search_result_quotes, indent=2))           # Pretty print

Input

Enter a ticker query: apple

Output

[
  {
    "exchange": "NMS",
    "shortname": "Apple Inc.",
    "quoteType": "EQUITY",
    "symbol": "AAPL",
    ...
  },
  ...
]

avhagedorn avatar May 28 '24 04:05 avhagedorn

Just so I understand the use case correctly - how is this different to Ticker.news? Should Ticker.news be changed to use your code.

ValueRaider avatar Jun 01 '24 19:06 ValueRaider

This use case targets searching for tickers - the underlying API does also fetch related news articles, but the primary motivation behind this is some kind of ticker search functionality. I should also note that I haven't found anything in the library currently for this use case, but I could be missing something. Hope that clears this up!

avhagedorn avatar Jun 02 '24 17:06 avhagedorn

https://github.com/ranaroussi/yfinance/blob/930b305327e2e3769b5d62115b3ab25bc58f28de/yfinance/base.py#L502

ValueRaider avatar Jun 02 '24 20:06 ValueRaider

@ValueRaider, the get_news function currently only returns the news list and excludes the quotes list in the /search response. Having a search utility function would be a valuable addition and filtering the data to show just the quotes would help eliminate redundancy with get_news.

petey9891 avatar Sep 08 '24 19:09 petey9891

would help eliminate redundancy with get_news

Agree. I wasn't rejecting the idea.

ValueRaider avatar Sep 08 '24 20:09 ValueRaider