Files
OpenBB/website/content/api/_index.md
Theodore Aptekarev 1b914d45e8 Econometrics notebooks API (#1462)
* Add initial implementations of the API wrappers

* Fix typos in docstrings

* Fix typos an markdown linting errors in docs

* Ditch using insecure eval in favor of secure getattr

* Add GST notebooks API documentation

* Add notebook screenshot to the GST API docs
2022-03-11 22:26:02 +03:00

3.6 KiB
Vendored

geekdocCollapseSection
geekdocCollapseSection
true

{{< toc-tree >}}

image

GST python API

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 gst.stocks.load("aapl").


Python environment setup

Import the GST API into your python script or notebook with

from gamestonk_terminal import api as gst

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 gst.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 = gst.stocks.load(
    ticker="aapl",
    start="2021-06-10",
)
aapl_data = gst.stocks.process_candle(aapl_data)

Plot candle chart

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

Call the "What if" command of the backtesting menu

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

Get sentiment for a list of tickers

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

Print key fundamental metrics for past 2 years

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

Get fundamental metrics for past 3 years as a pandas dataframe

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

Perform an OLS regression on multiple custom datasets

gst.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 = gst.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
gst.stocks.ta.macd(s_ticker=ticker, series=aapl["Close"], external_axes=axes[0:2])
gst.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()