Using Finviz API To Get Stocks Data Programmatically

in #market3 years ago

finvizapi.png

FinViz is a great website for stock market traders. Even if you are not interested in programming and automating trading related tasks, but you are interested in stock markets you will find FinViz to be useful website. It provides information and tools that helps in evaluating trade ideas and analyzing stocks.

A list of valid stock tickers is needed to programmatically test various trading ideas and strategies. One of the ways I have used FinViz was to get a list of stock tickers using FinViz screener and a python code. I written about this process in the past in my Scraping Ticker Symbols From Finviz Screener with Selenium in Python post.

One of the problems of using the same list of tickers over an over is at some point the list will end containing invalid tickers because some stocks get delisted from exchanges. For this reason, the ticker list needs to be updated from time to time. Since this tickers list is used in various different projects/codes, some of them would end not working properly due to an invalid ticker in the list.

One solution would be to create the list every time other codes/tasks are performed. But that would be inefficient. That's why I wanted to find a better way to keep tickers listed updated.

After searching and experimenting with different approaches and modules others have used, I have come across with FinViz API created by Mario Stoev. It is an unofficial Python API for FinViz. Feel free to visit the GitHub page for the project at https://github.com/mariostoev/finviz for details.

While FinViz API uses similar process as I did before for my initial task of getting list of tickers, it has more interesting functions that can be helpful for various projects.

First let's see how easy it is to get a list of stocks and information about them:

from finviz.screener import Screener 

stock_list = Screener()
print(type(stock_list))
stock_list.to_csv('finviz_stocks_list.csv')

The code above gets 8000+ stock tickers and other information about tickers and saves them as a csv file. The information saved is the same that is shown on FinViz Screener as shown in the image below.

tickers.png

The Screener() has various parameters that can be used to sort, filter, and order the results. One of the interesting one is the filter parameter. The following code from their example, shows companies in NASDAQ which are in the S&P500, gets the performance table, and sorts it by price ascending.

filters = ['exch_nasd', 'idx_sp500'] 
stock_list = Screener(filters=filters, table='Performance', order='price')  

I didn't use any filters for my list, because I wanted to get all available tickers. Not passing any arguments accomplishes that. Saved list has tables like sector, industry, country, market cap, p/e, price, price change, volume that can be used later to filter based on what's needed.

Let's see what other interesting information we can get using FinViz API.

import finviz

stock = finviz.get_stock('TSLA')
print(stock)

This will return a long dictionary format data that contains a lot more information about a specific stock ticker, in this case TSLA. The image below is an accurate representation of what is returned. In fact, the code is trying to get all of this data as shown on FinViz website and send back as result.

stock.png

As you can see there is a lot of information listed in the image above. Getting this data programmatically may help us to use only the ones we need and perhaps create our own customized reports. Combined with the data returned with next few functions, we can have our own personal reports on specific stocks.

import finviz

insider = finviz.get_insider('TSLA')
print(insider)

news = finviz.get_news('TSLA')
print(news)

analyst_price_targets = finviz.get_analyst_price_targets('TSLA')
print(analyst_price_targets)

These functions are self-explanatory: get_insider() gets data about insider trades, get_news() gets lists of news articles titles and links for a specific stock ticker, and get_analyst_price_targets gets any information that analysts posted regarding their price targets for the stock ticker.

If we were to use any of these functions or data in our own customized reports or projects, it would also be great to get the charts for the stock tickers we are compiling information about. FinViz API makes that possible as well. Using the following few lines of code, we can download the charts for a list of stocks.

from finviz.screener import Screener 

stock_list = Screener(tickers=['TSLA', 'SQ', 'COIN', 'TWTR'])

stock_list.get_charts(period='d', chart_type='c', size='l', ta='1')

The code above creates a folder named charts in users folder and stores charts as .png files.

First we need to create a stock list using Screener() and passing tickers argument with a list of stock tickers. Then by calling .get_charts() methods we saved the charts.

.get_charts() method has four main parameters: period, chart_type, size, and ta. Period is for timeframe and values can be 'd' for daily chart, 'w' for weekly chart, and 'm' for monthly charts. Chart_type can be 'c' for candle type chats or 'l' for lines. Size can be 'm' for small and 'l' for large.

Lastly, ta stands for technical analysis. The value for ta can be '1' if we want technical analysis on charts like sma50, sma200, trendlines, etc. If we don't need any technical analysis on the saved charts, we can simply give ta value of '0'.

There is more that can be done with FinViz API. I just started experimenting with it. If you find it interesting and/or useful give it a try and let me know what you think.

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 98043.90
ETH 3346.07
USDT 1.00
SBD 3.02