Skip to main content

OpenBB package with core functionality

Project description

THIS README IS A WORK IN PROGRESS

1. Introduction

This directory contains the OpenBB Platform's core functionality. It allows you to create an extension or a provider that will be automatically turned into REST API endpoint and allow sharing data between commands.

2. How to install?

Git clone

Git clone the repository:

git clone git@github.com:OpenBB-finance/OpenBBTerminal.git

Install

Go to openbb_platform folder and install the package.

cd openbb_platform
poetry install

3. How to add an extension?

Project

Build a Python package:

poetry new openbb-sdk-my_extension

Command

Add a router and a command in the openbb_platform/extensions/<my_extension_folder>/<openbb_my_extension>/<my_extension>_router.py

from openbb_core.app.router import Router

router = Router(prefix="/router_name")

@router.command
def some_command(
    some_param: some_param_type,
) -> OBBject[Item]:
    pass

If your command only makes use of a standard model defined inside openbb_provider/standard_models directory, there is no need to repeat its structure in the parameters. Just pass the model name as an argument.

This is an example how we do it for stocks.load which only depends on StockHistorical model defined in openbb-provider:

@router.command(model="StockHistorical")
def load(
    cc: CommandContext,                 # user settings inside
    provider_choices: ProviderChoices,  # available providers
    standard_params: StandardParams,    # symbol, start_date, etc.
    extra_params: ExtraParams,          # provider specific parameters
) -> OBBject[BaseModel]:
    """Load stock data for a specific ticker."""
    return OBBject(results=Query(**locals()).execute())

Entrypoint

Add an entrypoint for the extension inside your pyproject.toml file.

packages = [{include = "openbb_platform_my_extension"}]
...
[tool.poetry.extensions."openbb_extensions"]
extension_name_space = "my_extension.extension_router:router"

Install extension

Install your extension.

cd openbb_platform_my_extension
poetry install

4. Usage

Update your credentials and default providers by modifying the .openbb_platform/user_settings.json inside your home directory:

{
    "credentials": {
        "benzinga_api_key": null,
        "fmp_api_key": null,
        "polygon_api_key": null,
        "fred_api_key": null
    },
    "defaults": {
        "routes": {
            "/stocks/fa/balance": {
                "provider": "polygon"
            },
            "/stocks/load": {
                "provider": "fmp"
            },
            "/stocks/news": {
                "provider": "benzinga"
            }
        }
    }
}

Update your system settings by modifying the .openbb_platform/system_settings.json file inside your home directory:

{
    "test_mode": true
}

4.1 Static version

Run your command:

from openbb import obb

output = obb.stocks.load(
    symbol="TSLA",
    start_date="2023-01-01",
    provider="fmp",
    chart=True
)

4.1.1. OBBject

Each command will always return a OBBject. There you will find:

  • results: the data returned by the command None
  • provider: the provider name (only available provider names allowed) used to get the data or None
  • warnings: List[Warning_] with warnings caught during the command execution or None
  • error: an Error with any exception that occurred during the command execution or None
  • chart: a Chart with chart data and format or None

Helpers

To help you manipulate or visualize the data we make some helpers available.

  • to_dataframe: transforms results into a pandas DataFrame
>>> output.to_dataframe()
              open    high       low   close   adj_close    ...
date
2023-07-21  268.00  268.00  255.8000  260.02  260.019989    ...
2023-07-20  279.56  280.93  261.2000  262.90  262.899994    ...
2023-07-19  296.04  299.29  289.5201  291.26  291.260010    ...
  • to_dict: transforms results into a dict of lists
>>> output.to_dict()
{
    'open': [268.0, 279.56, 296.04],
    'high': [268.0, 280.93, 299.29],
    'low': [255.8, 261.2, 289.5201],
    'close': [260.02, 262.9, 291.26],
    'adj_close': [260.019989, 262.899994, 291.26001],
    ...
}
  • show: displays chart.content to a chart
>>> output.show()
# Jupyter Notebook: inline chart
# Python Interpreter: opens a PyWry window with the chart
  • to_plotly_json: proxy to chart.content
>>> output.to_plotly_json()
{
    'data':[
        {
            'close': [260.02, 262.9, 291.26],
            'decreasing': {'line': {'width': 1.1}},
            'high': [268.0, 280.93, 299.29],
            'increasing': {'line': {'width': 1.1}},
            ...
        }
    ...
    ]
}

4.1.2. Utilities

User settings

These are your user settings, you can change them anytime and they will be applied. Don't forget to sdk.account.save() if you want these changes to persist.

from openbb import obb

obb.user.profile
obb.user.credentials
obb.user.preferences
obb.user.defaults

System settings

Check your system settings.

from openbb import obb

obb.system

Preferences

Check your preferences by adjusting the user_settings.json file inside your home directory.

The default preferences are:

{
    "data_directory": "~/.openbb_platform", // Where to store data
    "export_directory": "~/.openbb_platform/exports", // Where to store exports
    "user_styles_directory": "~/.openbb_platform/styles/user", // Where to store user styles
    "charting_extension": "openbb_charting", // Charting extension to use
    "chart_style": "dark", // Chart style to use (dark or light)
    "plot_enable_pywry": true, // Whether to enable PyWry
    "plot_pywry_width": 1400, // PyWry width
    "plot_pywry_height": 762, // PyWry height
    "plot_open_export": false, // Whether to open plot image exports after they are created
    "table_style": "dark", // Table style to use (dark or light)
    "request_timeout": 15, // Request timeout
    "metadata": true, // Whether to include metadata in the output
    "output_type": "OBBject" // Our default output type (OBBject, dataframe, polars, numpy, dict, chart)
}

Coverage

Obtain the coverage of providers and commands.

>>> obb.coverage.commands
{
    '.crypto.load': ['fmp', 'polygon'],
    '.economy.const': ['fmp'],
    '.economy.cpi': ['fred'],
    ...
}
>>> obb.coverage.providers
{
    'fmp':
    [
        '.crypto.load',
        '.economy.const',
        '.economy.index',
        ...
    ],
    'fred': ['.economy.cpi'],
    ...
}

4.1.3. OpenBB Hub Account

You can login to your OpenBB Hub account and save your credentials there to access them from any device.

from openbb import obb

# Login with personal access token
obb.account.login(pat="your_pat", remember_me=True)  # pragma: allowlist secret

# Login with email, password or Platform token
obb.account.login(email="your_email", password="your_password", remember_me=True)  # pragma: allowlist secret

# Change a credential
obb.user.credentials.polygon_api_key = "new_key"  # pragma: allowlist secret

# Save account changes
obb.account.save()

# Refresh account with latest changes
obb.account.refresh()

# Logout
obb.account.logout()

Note: credentials are stored as Pydantic SecretStr objects. This means that they will be masked when printed or displayed in a Jupyter Notebook. To get the actual value, use obb.user.credentials.polygon_api_key.get_secret_value().

4.1.4. Command execution

How do we execute commands?

OpenBB Platform core is a REST API powered by FastAPI. We use this feature to run commands both in a web server setting and also in the openbb python package.

If you are using the openbb package, running the command below triggers a "request" to the CommandRunner class. The "request" will be similar to the one found in 4.2 Dynamic version. This will hit the endpoint matching the command and return the result.

from openbb import obb

obb.stocks.load(
    symbol="TSLA",
    start_date="2023-07-01",
    end_date="2023-07-25",
    provider="fmp",
    chart=True
    )

4.1.5. Environment variables

The OS environment is only read once before the program starts, so make sure you change the variable before importing the Platform. We use the prefix "OPENBB_" to avoid polluting the environment (no pun intended).

To apply an environment variable use one of the following:

  1. Temporary: use this snippet

    import os
    os.environ["OPENBB_DEBUG_MODE"] = "True"
    from openbb import sdk
    
  2. Persistent: create a .env file in /.openbb_platform folder inside your home directory with

    OPENBB_DEBUG_MODE="False"
    

The variables we use are:

  • OPENBB_API_AUTH: enables API endpoint authentication
  • OPENBB_API_USERNAME: sets API username
  • OPENBB_API_PASSWORD: sets API password
  • OPENBB_API_AUTH_EXTENSION: specifies which authentication extension to use
  • OPENBB_AUTO_BUILD: enables automatic package build on import
  • OPENBB_CHARTING_EXTENSION: specifies which charting extension to use
  • OPENBB_DEBUG_MODE: enables debug mode
  • OPENBB_DEV_MODE: enables development mode
  • OPENBB_HUB_BACKEND: sets the backend for the OpenBB Hub

4.2 Dynamic version

You can also use the dynamic version to consume the API endpoints from Python itself.

In fact, the static version makes use of this feature to run each command. Take a look at the example below:

>>> from openbb_core.app.command_runner import CommandRunner
>>> runner = CommandRunner()
>>> output = runner.run(
    "/stocks/load",
    provider_choices={
        "provider": "fmp",
    },
    standard_params={
        "symbol": "TSLA",
        "start_date": "2023-07-01",
        "end_date": "2023-07-25",
    },
    extra_params={},
    chart=True,
)
>>> output
OBBject

id: ...                 # UUID Tag
results: ...            # Serializable results.
provider: ...           # Provider name.
warnings: ...           # List of warnings.
chart: ...              # Chart object.
extra: ...              # Extra info.

5. REST API

OpenBB Platform comes with a ready to use Rest API built with FastAPI. Start the application using this command:

uvicorn openbb_core.api.rest_api:app --reload

5.1 HTTPS

If you want to run your FastAPI app over HTTPS locally you can use mkcert and pass the certificate and key to uvicorn.

  1. Install mkcert (see instructions here)

  2. cd into "openbb_platform/platform/core/openbb_core/api"

  3. Run the following commands:

    mkcert -install
    mkcert localhost 127.0.0.1 ::1
    

    You will see two files created in the current directory.

    • Certificate: "localhost+2.pem"
    • Key: "localhost+2-key.pem"
  4. Change the code to start the server inside "rest_api.py" to:

    if __name__ == "__main__":
        import uvicorn
    
        uvicorn.run(
            "openbb_core.api.rest_api:app",
            reload=True,
            ssl_keyfile="./localhost+2-key.pem",
            ssl_certfile="./localhost+2.pem",
        )
    
  5. Run the server from the terminal with:

    python rest_api.py
    

    Your app will be available at https://127.0.0.1:8000/

5.2 Docker

You can use the API through Docker.

We provide a `.dockerfile`` in OpenBB repo.

To build the image, you can run the following command from the repo root:

docker build -f build/docker/api.dockerfile -t openbb-platform:latest .

To run this newly-built image:

docker run --rm -p 8000:8000 -v ~/.openbb_platform:/root/.openbb_platform openbb-platform:latest

This will mount the local ~/.openbb_platform directory into the Docker container so you can use the API keys from there and it will expose the API on port 8000.

5.3 Authentication

By default the API launches with no authentication.

This means that if you deploy it on some network, any client will be served.

5.3.1 HTTP Basic Auth

This method is not recommended for production environments.

If you are in a rush and still want some layer of security you can use the FastAPI HTTP Basic Auth we included in the API. To enable this feature, set the following environment variables (more info on environment variables here 4.1.5. Environment variables) and replace the username and password with your preferred values:

OPENBB_API_AUTH="True"
OPENBB_API_USERNAME="some_user"
OPENBB_API_PASSWORD="some_pass"

The application will expect a header that contains username and password in the form of Basic <username:password>, where "username:password" is encoded in Base64. Pass this in every request you make to the API inside the headers "Authorization" field.

Here is an example using base64 and requests libraries:

import base64
import requests

msg = "some_user:some_pass"
msg_bytes = msg.encode('ascii')
base64_bytes = base64.b64encode(msg_bytes)
base64_msg = base64_bytes.decode('ascii')

requests.get(
    url="http://127.0.0.1:8000/api/v1/stocks/load?provider=fmp&symbol=AAPL",
    headers={"Authorization": f"Basic {base64_msg}"}
)

5.3.2 Custom authentication

For custom authentication methods you can plug an authentication extension into the API. To do so, set the environment variable OPENBB_API_AUTH_EXTENSION with the name of the extension you want to use.

The extension entry point defined in the respective "pyproject.toml should be similar to

[tool.poetry.plugins."openbb_core_extension"]
auth = "openbb_auth.auth_router:router"

In this case, the auth_router.py module should define:

  • router: fastapi.APIRouter with relevant user authentication endpoints (e.g. /token)
  • auth_hook: awaitable function that checks if given authorization credentials are valid and raises an HTTP_401_UNAUTHORIZED exception if not.
  • user_settings_hook: awaitable function that returns a UserSettings object. This will be called by every command endpoint to obtain the user settings for a given user and should depend on auth_hook to be executed first.

6. Front-end typing

Here are libraries to get frontend typing.

openapi-typescript + openapi-fetch

openapi-generator

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openbb_core-0.1.0a5.tar.gz (59.8 kB view hashes)

Uploaded Source

Built Distribution

openbb_core-0.1.0a5-py3-none-any.whl (76.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page