API
- GST python API
- Python environment setup
- Understanding the API functions
- Usage examples
- External matplotlib axes support
The GST 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").
Import the GST API into your python script or notebook with
from openbb_terminal import api as openbb
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
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}
}
)
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()