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 yfinance.exceptions import YFRateLimitError 6from .types import PriceHistory, RateLimitHitError 7 8# Ensure yfinance raises exceptions instead of silently failing 9yf.config.debug.hide_exceptions = False 10 11START_FROM = "2000-01-01" 12"""Adjust this date if you need to get historical data further in the past. Note that 13extending this date will lead to increased load on the Yahoo Finance servers. 14""" 15 16 17def get_price_history( 18 query: str, currency_preference: str = "USD" # pylint: disable=unused-argument 19) -> PriceHistory: 20 """Get price history for a given query. Note that `currency_preference` will be 21 ignored since Yahoo Finance returns each ticker in the one currency that is set for 22 that ticker. 23 """ 24 try: 25 ticker = yf.Ticker(query) 26 df = ticker.history(start=START_FROM) 27 except YFRateLimitError as exc: 28 raise RateLimitHitError(source="yahoo") from exc 29 30 # Simplify dataframe: 31 df = df.copy() 32 df = df[["Close"]] 33 df.index = pd.to_datetime(df.index, utc=True) 34 df.index.name = "date" 35 df.rename(columns={"Close": "close"}, inplace=True) 36 37 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:
18def get_price_history( 19 query: str, currency_preference: str = "USD" # pylint: disable=unused-argument 20) -> PriceHistory: 21 """Get price history for a given query. Note that `currency_preference` will be 22 ignored since Yahoo Finance returns each ticker in the one currency that is set for 23 that ticker. 24 """ 25 try: 26 ticker = yf.Ticker(query) 27 df = ticker.history(start=START_FROM) 28 except YFRateLimitError as exc: 29 raise RateLimitHitError(source="yahoo") from exc 30 31 # Simplify dataframe: 32 df = df.copy() 33 df = df[["Close"]] 34 df.index = pd.to_datetime(df.index, utc=True) 35 df.index.name = "date" 36 df.rename(columns={"Close": "close"}, inplace=True) 37 38 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.