Instead of translating each criterion into a filters dict by hand, paste the URL
finviz puts in your address bar as you click filters in its UI. screener.from_url
reads the view (v) and reconstructs the filters (f), signal (s) and ticker
(t), returning the matching screener object:
Sort order, pagination and custom columns are screener_view() arguments, so
they are ignored in the URL. Any filter/signal/view code that finvizfinance does
not recognize raises a ValueError naming the code, rather than silently
dropping it and returning the wrong stocks.
Filters set on an instance stay on it between calls, so one screener object can
be reused. screener_view() returns an empty DataFrame when a view has no
matches — never None — and Ticker.screener_view() returns an empty list; a
long run blocked mid-way keeps what it already fetched on err.partial and can
be resumed with select_page=err.next_page.
Screener (Ticker)
Getting list of tickers according to the filters.
Calendar
Getting the economic calendar (release datetime, impact, actual/expected/prior).
Partitioning tickers by their earnings dates for a period.
from finvizfinance.earnings import Earnings
# period: This Week (default), Next Week, Previous Week, This Month
fearnings = Earnings(period='This Week')
# mode: financial (default), overview, valuation, ownership, performance, technical
days = fearnings.partition_days(mode='financial')
# optionally export the partitioned tables
fearnings.output_csv('earning_days')
# Excel export needs the optional extra: pip install finvizfinance[excel]
fearnings.output_excel('earning_days.xlsx')
Future
Getting futures performance.
from finvizfinance.future import Future
ffuture = Future()
# timeframe: D (default), W, M, Q, HY, Y
df = ffuture.performance(timeframe='D')
df.head()
Misc (Proxy)
Optional proxy can be used for getting information from FinViz website. Accessible from finvizfinance
it's an extension of requests library proxies
from finvizfinance.util import set_proxy
proxies={'http': 'http://127.0.0.1:8080'}
set_proxy(proxies)
set_proxy, set_timeout and set_session apply to the calling thread or
async task only — a web app can configure each request handler differently
without the settings bleeding into other callers. Threads that were given no
session get their own default one, since requests.Session is not thread-safe.
That also means a thread-pool worker starts with an empty context: a set_proxy
or set_session call made in the parent thread is not visible inside
ThreadPoolExecutor workers, and they fall back to a default session with the
default timeout. Configure inside the worker, or carry the context over:
import contextvars
from concurrent.futures import ThreadPoolExecutor
from finvizfinance.util import set_proxy
set_proxy({"https": "http://127.0.0.1:8080"})
ctx = contextvars.copy_context()
with ThreadPoolExecutor() as pool:
list(pool.map(lambda t: ctx.copy().run(scrape, t), tickers))
A session shared across workers also needs a big enough connection pool
(HTTPAdapter(pool_maxsize=...)), because requests defaults to 10.
Credit
Developed by Tianning Li. Feel free to give comments or suggestions.
Alternatives
Use instead of finvizfinance: tools that do the same job, closest first.