tessa.price.yahoo

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

 1"""Everything Yahoo-Finance-related (other than search)."""
 2
 3import pandas as pd
 4import yfinance as yf
 5from .types import PriceHistory
 6
 7START_FROM = "2000-01-01"
 8"""Adjust this date if you need to get historical data further in the past. Note that
 9extending this date will lead to increased load on the Yahoo Finance servers.
10"""
11
12
13def get_price_history(
14    query: str, currency_preference: str = "USD"  # pylint: disable=unused-argument
15) -> PriceHistory:
16    """Get price history for a given query. Note that `currency_preference` will be
17    ignored since Yahoo Finance returns each ticker in the one currency that is set for
18    that ticker.
19    """
20    ticker = yf.Ticker(query)
21    df = ticker.history(start=START_FROM, raise_errors=True)
22
23    # Simplify dataframe:
24    df = df.copy()
25    df = df[["Close"]]
26    df.index = pd.to_datetime(df.index, utc=True)
27    df.index.name = "date"
28    df.rename(columns={"Close": "close"}, inplace=True)
29
30    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:
14def get_price_history(
15    query: str, currency_preference: str = "USD"  # pylint: disable=unused-argument
16) -> PriceHistory:
17    """Get price history for a given query. Note that `currency_preference` will be
18    ignored since Yahoo Finance returns each ticker in the one currency that is set for
19    that ticker.
20    """
21    ticker = yf.Ticker(query)
22    df = ticker.history(start=START_FROM, raise_errors=True)
23
24    # Simplify dataframe:
25    df = df.copy()
26    df = df[["Close"]]
27    df.index = pd.to_datetime(df.index, utc=True)
28    df.index.name = "date"
29    df.rename(columns={"Close": "close"}, inplace=True)
30
31    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.