Skip to content

Client

yafin.Client

Bases: ClientBase

Client for Yahoo Finance API.

HTTP resources closing

Uses http resources, so do not forget to close them after use to avoid resource leakage or use context manager.

Attributes:

Name Type Description
timeout

timeout (in secs) for each http request.

max_retries

number of retries in case of failed request.

_session Session[Any] | None

session instance, that is used for all http requests. (Is lazily initialized.)

Methods:

Name Description
get_chart

Get chart data for the ticker.

get_quote

Get quote for tickers.

get_quote_type

Get quote type for tickers.

get_quote_summary

Get quote summary for the ticker.

get_timeseries

Get timeseries for the ticker.

get_options

Get options for the ticker.

get_search

Get search results for tickers.

get_recommendations

Get analyst recommendations for tickers.

get_insights

Get insights for tickers.

get_ratings

Get ratings for the ticker.

get_market_summaries

Get market summaries.

get_trending

Get trending tickers.

get_currencies

Get currency exchange rates.

get_calendar_event

Get calendar events.

__init__(timeout=5.0, max_retries=5)

Create new Client instance.

Parameters:

Name Type Description Default
timeout float

timeout (in secs) for each http request.

5.0
max_retries int

number of retries in case of failed request.

5

close()

Close the session if open and reset crumb.

get_chart(ticker, interval, period_range=None, period1=None, period2=None, include_pre_post=None, events=EVENTS) cached

Get chart data for the ticker.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
interval str

Data interval.

required
period_range str | None

Range of the period.

None
period1 int | float | None

Start timestamp in seconds. (optional, default: None)

None
period2 int | float | None

End timestamp in seconds. (optional, default: None)

None
include_pre_post bool | None

Whether to include pre and post market.

None
events str | None

Comma-separated events to include.

EVENTS

Returns: Chart response json including result and error.

Raises:

Type Description
ValueError

If any of period_range, interval or parsed_events are not in list of valid values.

Note

Even though the the endpoint param is called range, period_range was chosen to avoid collision with python built-in method name.

get_quote(tickers, include_pre_post=None) cached

Get quote for tickers.

Parameters:

Name Type Description Default
tickers str

Comma-separated ticker symbols.

required
include_pre_post bool | None

Whether to include pre and post market.

None

Returns: Quote response json including result and error.

Note

Even though the the endpoint param is called symbols, tickers was chosen to use the same name as is used in higher level (Async)Symbol class.

get_quote_type(tickers) cached

Get quote type for tickers.

Parameters:

Name Type Description Default
tickers str

Comma-separated ticker symbols.

required

Returns: Quote type response json including result and error.

Note

Even though the the endpoint param is called symbol, ticker was chosen to use the same name as is used in higher level (Async)Symbol class.

get_quote_summary(ticker, modules) cached

Get quote summary for the ticker.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
modules str

Comma-separated modules to include.

required

Returns: Quote summary response json including result and error.

Raises: ValueError: If modules are not in list of valid values.

get_timeseries(ticker, types, period1=None, period2=None)

Get timeseries for the ticker.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required
types str

Comma-separated types (incl. frequency) to include.

required
period1 int | float | None

Start timestamp in seconds. (optional, default: 1st Jan 2020 timestamp)

None
period2 int | float | None

End timestamp in seconds. (optional, default: now timestamp)

None

Returns: Timeseries response json including result and error.

Raises: ValueError: If types are not in list of valid values.

get_options(ticker) cached

Get options for the ticker.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required

Returns: Options response json including result and error.

Get search results for tickers.

Parameters:

Name Type Description Default
tickers str

Comma-separated ticker symbols.

required

Returns: Search result json.

get_recommendations(tickers) cached

Get analyst recommendations for tickers.

Parameters:

Name Type Description Default
tickers str

Comma-separated ticker symbols.

required

Returns: Recommendations response json including result and error.

get_insights(tickers) cached

Get insights for tickers.

Parameters:

Name Type Description Default
tickers str

Comma-separated ticker symbols.

required

Returns: Insights response json including result and error.

Note

Even though the the endpoint param is called symbols, tickers was chosen to use the same name as is used in higher level (Async)Symbol class.

get_ratings(ticker) cached

Get ratings for the ticker.

Parameters:

Name Type Description Default
ticker str

Ticker symbol.

required

Returns: Ratings result json.

get_market_summaries() cached

Get market summaries.

Returns: Market summaries response json including result and error.

Get trending tickers.

Returns: Trending tickers response json including result and error.

get_currencies() cached

Get currency exchange rates.

Returns: Currency exchange rates response json including result and error.

get_calendar_events(modules=None, start_date=None, end_date=None)

Get calendar events.

Parameters:

Name Type Description Default
modules str | None

Comma-separated modules to include.

None
start_date int | float | None

Start timestamp in miliseconds. (optional, default: (period2 - 149days) timestamp)

None
end_date int | float | None

End timestamp in miliseconds. (optional, default: now timestamp)

None

Returns: Calendar events response json including result and error.

Note: Query range cannot be greater than 150 days.