Files
OpenBB/website/content/api/_index.md
Jeroen Bouma f0a3dfbc56 Improve the Hugo documentation to be more accessible and user-friendly (#1891)
* Greatly improve the documentation

* Rename the website from OpenBB Terminal to OpenBB Documentation

* Adjust the menu

* Add 'Table of Contents' to the toc file

* Small improvs

* Fix broken links

* Improve more

* Small small small fix
2022-06-02 18:14:40 +02:00

3.5 KiB
Vendored

title, geekdocCollapseSection
title geekdocCollapseSection
OpenBB Python API true

The OpenBB Python API was created to enable usage of the terminal functionality in python scripts and IPython Notebooks (Jupyter). The API wraps the functionality of the terminal in a way that the Python commands follow the same convention as the terminal.

For example stocks/load aapl becomes openbb.stocks.load("aapl").

image

{{< toc >}}


Python environment setup

Import the GST API into your python script or notebook with

from openbb_terminal import api as openbb

Understanding the API functions

To understand the parameters of an API function the simplest way is to refer to the docstring. Docstrings can be viewed using the code introspection tools of your text/notebook editor or the built-in python help command. Docstrings contain both the description of the functionality and the overview of the parameters.

As an example this is the docstring of openbb.economy.bigmac:

Display Big Mac Index for given countries

Parameters
----------
country_codes : List[str]
    List of country codes to get for
raw : bool, optional
    Flag to display raw data, by default False
export : str, optional
    Format data, by default ""
external_axes : Optional[List[plt.Axes]], optional
    External axes (3 axes are expected in the list), by default None

Usage examples

Load stock data into a dataframe:

aapl_data = openbb.stocks.load(
    ticker="aapl",
    start="2021-06-10",
)
aapl_data = openbb.stocks.process_candle(aapl_data)

Plot candle chart

openbb.stocks.candle("AAPL", aapl_data, True)

Call the "What if" command of the backtesting menu

openbb.stocks.bt.whatif("aapl", 10, datetime.strptime("2012-03-03", "%Y-%m-%d"))

Get sentiment for a list of tickers

openbb.stocks.ca.models.finbrain.get_sentiments(tickers=["aapl"])

Print key fundamental metrics for past 2 years

openbb.stocks.fa.fmp.metrics(ticker="AAPL", number=2)

Get fundamental metrics for past 3 years as a pandas dataframe

openbb.stocks.fa.fmp.models.fmp.get_key_metrics(ticker="AAPL", number=3)

Perform an OLS regression on multiple custom datasets

openbb.econometrics.ols(
    regression_variables=[
    'close-tsla', 'volume-aapl'
    ],
    data={"aapl":aapl_data,"tsla":tsla_data},
    datasets={
        "close-tsla":{"Close": None, "tsla": None},
        "volume-aapl":{"Volume": None, "aapl": None}
    }
)

External matplotlib axes support

Most of the functions of the terminal that generate matplotlib charts support passing external axes into them. This is very handy when you want a chart that is generated by GST to appear in another chart that you are creating in a notebook.

Example:

# Load stock data
ticker = "AAPL"
stock_data = openbb.stocks.load(ticker="aapl")

# Create a matplotlib figure
fig, axes = plt.subplots(3, 1, figsize=(10, 7))

# Use GST to add charts to the axes of the created figure
openbb.stocks.ta.macd(s_ticker=ticker, series=aapl["Close"], external_axes=axes[0:2])
openbb.stocks.options.voi_yf(
    ticker=ticker,
    expiry="2022-03-11",
    min_sp=110,
    max_sp=210,
    min_vol=0.3,
    external_axes=[axes[2]],
)

# Show the figure with charts
fig.show()