tessa.price.yahoo

Everything Yahoo-Finance-related (other than search).

 1"""Everything Yahoo-Finance-related (other than search)."""
 2
 3import io
 4import contextlib
 5import pandas as pd
 6import yfinance as yf
 7from .types import PriceHistory, SymbolNotFoundError
 8
 9START_FROM = "2000-01-01"
10"""Adjust this date if you need to get historical data further in the past. Note that
11extending this date will lead to increased load on the Yahoo Finance servers.
12"""
13
14
15def get_price_history(
16    query: str, currency_preference: str = "USD"  # pylint: disable=unused-argument
17) -> PriceHistory:
18    """Get price history for a given query. Note that `currency_preference` will be
19    ignored since Yahoo Finance returns each ticker in the one currency that is set for
20    that ticker.
21
22    Note that yfinance has some strange error behavior, e.g., when a ticker doesn't
23    exist: `history()` will return an empty dataframe (but with all the column headers
24    as usual) and print "No data found, symbol may be delisted" to stdout.
25    """
26    stdout = io.StringIO()
27    ticker = yf.Ticker(query)
28    with contextlib.redirect_stdout(stdout):
29        df = ticker.history(start=START_FROM, debug=True)
30    if "No data found" in stdout.getvalue():
31        raise SymbolNotFoundError(source="yahoo", query=query)
32
33    # Simplify dataframe:
34    df = df.copy()
35    df = df[["Close"]]
36    df.index = pd.to_datetime(df.index, utc=True)
37    df.index.name = "date"
38    df.rename(columns={"Close": "close"}, inplace=True)
39
40    return PriceHistory(df, ticker.get_history_metadata()["currency"])
START_FROM = '2000-01-01'

Adjust this date if you need to get historical data further in the past. Note that extending this date will lead to increased load on the Yahoo Finance servers.

def get_price_history( query: str, currency_preference: str = 'USD') -> tessa.price.types.PriceHistory:
16def get_price_history(
17    query: str, currency_preference: str = "USD"  # pylint: disable=unused-argument
18) -> PriceHistory:
19    """Get price history for a given query. Note that `currency_preference` will be
20    ignored since Yahoo Finance returns each ticker in the one currency that is set for
21    that ticker.
22
23    Note that yfinance has some strange error behavior, e.g., when a ticker doesn't
24    exist: `history()` will return an empty dataframe (but with all the column headers
25    as usual) and print "No data found, symbol may be delisted" to stdout.
26    """
27    stdout = io.StringIO()
28    ticker = yf.Ticker(query)
29    with contextlib.redirect_stdout(stdout):
30        df = ticker.history(start=START_FROM, debug=True)
31    if "No data found" in stdout.getvalue():
32        raise SymbolNotFoundError(source="yahoo", query=query)
33
34    # Simplify dataframe:
35    df = df.copy()
36    df = df[["Close"]]
37    df.index = pd.to_datetime(df.index, utc=True)
38    df.index.name = "date"
39    df.rename(columns={"Close": "close"}, inplace=True)
40
41    return PriceHistory(df, ticker.get_history_metadata()["currency"])

Get price history for a given query. Note that currency_preference will be ignored since Yahoo Finance returns each ticker in the one currency that is set for that ticker.

Note that yfinance has some strange error behavior, e.g., when a ticker doesn't exist: history() will return an empty dataframe (but with all the column headers as usual) and print "No data found, symbol may be delisted" to stdout.