Skip to main content

Basics

Importing the SDK

Now that you have the SDK installed, the first step is to import the OpenBB SDK in your preferred code editor. Nearly everything required to interact with any function from the OpenBB Terminal gets imported in one line. Begin a Python script or Notebook file with:

from openbb_terminal.sdk import openbb

Example snippets used in the remainder of this guide will assume the code block above is used.

In the same way as operating the OpenBB Terminal, functions are divided into menus which are scrollable after importing into a jupyter notebook. Entering a period, ., after openbb, will display the Sub-menus available.

Navigation

An alternate way to view the contents of a menu is to use Python's built-in help.

help(openbb.stocks.dd)

Docstrings

In addition to Python's built-in help, docstrings are also displayed in the Contextual Help window, within a Jupyter environment or by running help(openbb.economy.events) in your code editor.

help(openbb.economy.events)

Help on Operation in module openbb_terminal.core.library.operation:

<openbb_terminal.core.library.operation.Operation object>
Get economic calendar for countries between specified dates

Parameters
----------
countries : [List[str],str]
List of countries to include in calendar. Empty returns all
start_date : str
Start date for calendar
end_date : str
End date for calendar

Returns
-------
pd.DataFrame
Economic calendar

How to use the SDK

Your First Function

To get started using the OpenBB SDK you can try the below code for your first function.

openbb.economy.events()
Time (GMT)CountryEventactualconsensuspreviousDate
001:30FranceFrench Unemployment Rate7.3%7.3%7.4%2022-11-15
102:00United KingdomAverage Earnings ex Bonus5.7%5.5%5.5%2022-11-15
202:00United KingdomAverage Earnings Index +Bonus6.0%5.9%6.1%2022-11-15
302:00United KingdomClaimant Count Change3.3K17.3K3.9K2022-11-15
402:00United KingdomEmployment Change 3M/3M-52K-25K-109K2022-11-15
- The results from data functions are not stored to memory unless explicitly instructed to. Most functions returning data are presented as a Pandas DataFrame.

Modify the syntax slightly to deliver the output into a DataFrame:

economic_calendar = openbb.economy.events()

Defined as a variable, interacting with the results becomes a matter of manipulating tables. For example, the code block below will filter the results of the events function to display only events scheduled at a specific time.

economic_calendar = openbb.economy.events()
economic_calendar.set_index(keys = ['Time (GMT)'], append = True, inplace = True)
events = economic_calendar.filter(like = '9:00', axis = 0)

events
CountryEventactualconsensuspreviousDate
09:00United StatesFed Governor Cook Speaks---2022-11-15
09:00GermanyGerman Buba Balz Speaks---2022-11-15
09:00GermanyGerman Buba Vice President Buch Speaks---2022-11-15

Passing Results to Another Function

Let's take a look at another example where the input to a function is a list. It may be desirable to derive that list from a different function. This can be useful for screening tickers, or analyzing particular industries or sectors. The Comparison Analysis sub-module, within Stocks, is one set of functions that can benefit from this kind of workflow. Instead of something like:

openbb.stocks.ca.screener(similar = ['AAPL', 'NFLX', 'META', 'AMZN', 'MSFT', 'GOOGL', 'DIS', 'TSLA'], data_type = 'valuation')

Try, openbb.etf.holdings, to populate a list dynamically:

symbols = openbb.etf.holdings('DIA')
dia_symbols = list(symbols.index.drop(['N/A']))
dia_valuation = openbb.stocks.ca.screener(similar = dia_symbols, data_type = 'valuation')
dia_valuation = dia_valuation.sort_values(by = ['Price'], ascending = False).convert_dtypes()

dia_valuation.head(5)
TickerMarket CapP/EFwd P/EPEGP/SP/BP/CP/FCFEPS this YEPS next YEPS past 5YEPS next 5YSales past 5YPriceChangeVolume
25UNH5.0033e+1125.1720.611.771.596.4312.8819.610.1280.13150.2010.14220.092503.01-0.02095007787
10GS1.282e+1110.1810.17N/A2.081.240.452.981.4030.09760.296-0.09120.113382.880.00143184768
11HD3.1097e+1118.8617.861.221334.4324789.720.3010.03610.1920.1570.098311.930.01639239159
1AMGN1.5543e+1122.8615.43.385.941.7713.5432.04-0.1650.04860.0010.06770.025283.6-0.0062761083
18MCD2.0272e+1134.326.015.148.71N/A71.67118.650.5910.05220.130.0667-0.012267.84-0.01635421817

Displaying Charts

The OpenBB SDK has built-in charting libraries for Matplotlib, for any chart available from the Terminal. User style sheets can be added to the folder (more on this in Importing and Exporting Data), ~/OpenBBUserData/styles/user. Styles are shared properties between the OpenBB Terminal and the SDK.

Displaying charts in Jupyter Notebooks requires an additional line of code. You can either render a static image with %matplotlib inline or add in pan/zoom functionality with %matplotlib widget.

Functions, such as candle, exist to display charts. Others, like those within the Technical Analysis module, have the option to return either, a chart or raw data. The next examples will outline a few different scenarios. First, let's get some data:

spy_daily = openbb.stocks.load(
symbol = 'SPY',
start_date = '1993-11-01',
monthly = True)

Data from the previous example, spy_daily, can be used in the openbb.stocks.candle function, for example:

openbb.stocks.candle(
data = spy_daily,
asset_type = 'SPY - Monthly Chart from November, 1993',
symbol = ''
)

openbb.stocks.candle

The function will also respond to individual tickers without saving the data first as done with load:

openbb.stocks.candle('SPY')

openbb.stocks.candle

Where functions in the Terminal display either a chart or raw data, the command will have an additional _chart component. For example, Donchian Channels:

openbb.ta.donchian(openbb.stocks.load('SPY', interval = 15))
dateDCL_20_20DCM_20_20DCU_20_20
2022-11-15 14:45:00394.49398.33402.17
2022-11-15 15:00:00394.49398.195401.9
2022-11-15 15:15:00394.49398.195401.9
2022-11-15 15:30:00394.49398.105401.72
2022-11-15 15:45:00394.49398.027401.565
openbb.ta.donchian_chart(
data = openbb.stocks.load('SPY', interval = 15),
symbol = 'SPY 15 Minute Data'
)

openbb.ta.donchian

Futures curves are another example where this syntax is applied:

openbb.futures.curve_chart('GE')

openbb.futures.curve

The intros section for each module explore further functionality and provide sample code snippets. For example, an introduction to Stocks can be found here.