You may remember thememe stock hysteria that sent GameStop’s stock up 1,744% in January 2021. One of the good things that came from that episode was the GameStonk terminal, now rebranded as OpenBB. OpenBB is the most popular open-source finance projects on GitHub for good reason: it provides a single interface to access hundreds of data feeds from one place in a standard way. OpenBB has a command-line interface that is great for manual investment research. But when it’s time to get data into Python, you want the OpenBB Platform. This recipe will guide you through the process of using the OpenBB Platform to fetch stockmarket data.
Getting ready…
By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up. The OpenBB Platform is free to use and offers a web-based UI to manage your configuration files, store API keys, and get code walkthroughs and examples. Sign up for a free Hub account at https://my.openbb.co/login. The popular course,Getting Started with Python for Quant Finance, uses OpenBB exclusively for all the code. Check outhttps://www.pyquantnews.com/getting-started-with-python-for-quant-finance for information on howto join.
How to do it…
Using the OpenBB Platform involvesone import:
- Import theOpenBB Platform:
from openbb import obbobb.user.preferences.output_type = "dataframe"
- Use the
historical
method todownload price data for theSPY ETF:data = obb.equity.price.historical("SPY", provider="yfinance")
- Inspect theresulting DataFrame:
print(data)
Running the preceding code generates a pandas DataFrame and prints the data tothe screen:
Figure 1.1: Historic price data for SPY
How it works…
The OpenBB Platform follows an easy-to-understand namespace convention. All the methods for acquiring stock price data aremethodsonopenbb.equity
.
Thehistorical
method accepts a ticker symbol and returns the open, high, low, close, adjusted close, volume, dividend, and split adjustments in a pandas DataFrame. The additional parameters you can specify areas follows:
start_date
: Start date to get datafrom withinterval
: Interval (in minutes) to get data—that is, 1, 5, 15, 30, 60,or 1,440end_date
: End date to get datafrom withprovider
: Source ofdata extracted
There’s more…
An important benefit of using the OpenBB Platform is choosing your data source. By default, the OpenBB Platform will attempt to download data from free sources such as Yahoo! Finance. In most OpenBB Platform calls, you can indicate a different source. To use a source that requires an API key (either free or paid), you can configure it in theOpenBB Hub.
Tip
Check out the OpenBB Platform documentation forthe latestfunctionality:https://docs.openbb.co.
Let’s look at some more of the functions of theOpenBB Platform.
Comparison of fundamental data
Not only can the OpenBB Platform download fundamental data in an organized and usable way, but it can also concatenate it in a single Pandas DataFrame forfurther analysis.
We can use the following code to see the balance sheet metrics fromAAPL
andMSFT
:
obb.equity.fundamental.metrics( "AAPL,MSFT", provider="yfinance")
The output of the preceding snippet is a pandas DataFrame with fundamental data for each ticker thatwas passed:
Figure 1.2: Balance sheet data for MSFT and AAPL
Building stock screeners
One of the most powerful features ofthe OpenBB Platform is the custom stock screener. It uses theFinviz stock screener under the hood and surfaces metrics across a range of stocks based on either pre-built or custom criteria. See the documentation for more on how to use the OpenBB screenerfunctions (https://docs.openbb.co/platform/reference/equity/screener):
- Create an overview screener based on a list of stocks using thedefault view:
obb.equity.compare.groups( group="industry", metric="valuation", provider="finviz")
The output of the preceding snippet is the followingpandas DataFrame:
Figure 1.3: Results of a comparison screener between F, GE, and TSLA
- Create a screener that returns the top gainers from the technology sector based ona preset:
obb.equity.compare.groups( group="technology", metric="performance", provider="finviz")
The output of the preceding snippet is the followingpandas DataFrame:
Figure 1.4: Results of a screener showing the day’s top-gaining stocks
- Create a screener that presents an overview groupedby sector:
obb.equity.compare.groups( group="sector", metric="overview", provider="finviz")
The output of the preceding snippet is the followingpandas DataFrame:
Figure 1.5: Results of a screener grouped by sector
See also
For more on OpenBB and theFinviz stock screener, check out thefollowing resources: