Files
OpenBB/website/content/platform/development/developer-guidelines/obbject.md
Henrique Joaquim 2f01e954d1 Feature/charting obbject (#5938)
* fix circular import issues on the obbject extension framework

* settings available on the obbject

* extension init

* charting assets and code

* introduce to_chart functionality

* removes the to_chart method from obbject in detriment of the one from the obbject extension

* Update openbb_platform/obbject_extensions/charting/charting/__init__.py

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* fix on the show method

* adding the example to the to_chart docstring

* copying the exsting README from charting extension - needs to be revamped

* moving to openbb_charting instead

* update the pyproject.toml file

* pydocstyle

* versions

* better docstrings

* making accessors public

* removing unnecessary credentials variable cc @montezdesousa

* removing access to extra/metadata and use designated private attrs instead

* Fixes linting for the logging service (#5992)

* lints

* typing.Type instead

* deprecating ChartingService to use the new extension instead - adding function discovery capabilities using it

* tests for the obbject charting extension

* removing charting service from command runner

* filter functions not implemented in the module

* remove charting service tests

* removing charting service on the tests for int tests

* charting integration tests

* removing charting service file

* removing old charting extension code

* revamp builder

* fix obbject tests

* fix command_runner tests

* removing openbb_figure_table

* better website docs

* reviewed readme

* unsetting api key for posthog (pywry) windows

* added debug_mode to the system settings

* removing api key

* update nightly script

* fix tests

* black

* black

* Revert "black"

This reverts commit 21d76ad689.

* Revert "black"

This reverts commit 0904653fd7.

* black for henrique

* returning a list instead

* better docstrings

* removing unnecessary indicators docstring

* typo

* black

* intentional access to protected member

* changes to publish script

* filtering deleted files

* black

* adding render argument

* fixing the df_to_basemodel function: it should include the index in the columns either if flagged or if the index is named

* adding the hability to add you own data to to_chart method

* tests

* black

* disable protected access

* accept kwargs to eg. save image

* adjustment cc Igor

* add if statement for protection

* remove unused return typing

* fix technical charting views

* better error message cc @IgorWounds

* better structure and fix test

* Revert "better structure and fix test"

This reverts commit 612ea6117c.

* Revert "better error message cc @IgorWounds"

This reverts commit 3d7ee53878.

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2024-02-14 15:19:27 +00:00

5.1 KiB
Vendored

title, sidebar_position, description, keywords
title sidebar_position description keywords
OpenBB OBBject 7 This page provides information about the OBBject class in the OpenBB Platform. This class provides the interface to interact with commands
OBBject
Python
Development
OpenBB Platform
Results
Commands
Pandas
DataFrame
Polars
Numpy

import HeadTitle from '@site/src/components/General/HeadTitle.tsx';

The OBBject (OpenBB Object) is at the heart of developing around the OpenBB Platform. Every command will return this class as the command output. This class contains the following attributes:

  • results
    • This contains serializable data that was obtained by running the command.
  • provider
    • The name of the provider that was used to obtain the data.
  • Warnings
    • Any warnings that were generated by the command.
  • Extra
    • Additional metadata about the command run, including any arguments, the route, the timestamp, etc.

Additionally, the OBBject class contains a class variable, accessors, which is used to register extensions to the class. This is done by using the @obbject_accessor decorator. This decorator will register the class as an accessor to the OBBject class, and will be available on every OBBject returned by a command.


from openbb import obb
res = obb.equity.price.historical("AAPL")
res.accessors

An example with the openbb-charting extension would output:

{'charting'}

Find more about OBBject extensions here.

Example

In the python interface, the OBBject class is returned by every command. The following example shows how to access the results of a command:

from openbb import obb

obbject = obb.equity.price.historical("AAPL", provider="fmp")

display(obbject)
OBBject

id: 0655d06a-8797-7310-8000-4daaa1d47936
results: [{'date': datetime.datetime(2023, 11, 21, 0, 0), 'open': 191.41, 'high': 1...
provider: fmp
warnings: None
chart: None
extra: {'metadata': {'arguments': {'provider_choices': {'provider': 'fmp'}, 'standa...

The output type is <class 'openbb_core.app.model.obbject.OBBject'>.

If we look at the results attribute, we see that we are returned a list of the Data models used, i.e

obbject.results[0]
FMPEquityHistoricalData(date=2023-11-21 00:00:00, open=191.41, high=191.5, low=189.74, close=190.375, volume=24029603, vwap=190.54, label=November 21, 23, adj_close=190.375, unadjusted_volume=24029603.0, change=-1.03, change_percent=-0.5407241, change_over_time=-0.005407241)

In general, this will not be how you want to ingest or modify your data, so to help with this we have provided some methods.

OBBject Methods

We understand that there is no one size fits all solution for data ingestion and manipulation, so we have provided a few methods to help with this and handle the conversion from Obbject to your preferred data type. These methods are accessed by running obbject.<method>.

to_dataframe()

One of the most popular libraries for python data manipulation, this method will return a pandas dataframe with the results. We have designed this function to work with various data structures we have encountered. Given the time series nature of the data, we set the index to be date if that column appears in the models.

This also has the shorter alias to_df().

type(obb.equity.price.historical("AAPL", provider="fmp").to_dataframe())
<class 'pandas.core.frame.DataFrame'>

to_dict(orient)

A very common data structure is to return the data in a dictionary. This model provides an interface to do so, with options to return the dictionary in different orientations, just as in the pandas to_dict() method. The default method will return a dictionary that allows you to index to obtain data:

to_dict = obb.equity.price.historical("AAPL", provider="fmp").to_dict()["open"][:10]
[150.16, 148.13, 149.45, 148.31, 145.14, 144.29, 141.4, 148.21, 145.96, 147.77]

Orienting as records will give you a list of dictionaries, commonly used in JSON data structures:

obb.equity.price.historical("AAPL", provider="fmp").to_dict("records")[0]
{'open': 144.29,
'high': 144.81,
'low': 140.35,
'close': 141.17,
'volume': 83763800,
'vwap': 142.11,
'label': 'November 29, 22',
'adj_close': 140.39,
'unadjusted_volume': 83763800.0,
'change': -3.12,
'change_percent': -2.16,
'change_over_time': -0.0216}

Others

In addition to these two highly used methods, we have also provided the following methods:

to_numpy()

Returns numpy arrays of the results. Note that this loses the column names, so you will need to index the array to obtain the data.

to_polars()

Growing in popularity, polars is a blazingly fast dataframe library. This method will return a polars dataframe. Note that we do not include polars in the core dependency tree, so it needs to be installed separately.

show()

This method will display the chart if it was computed during the execution of the command, by setting the chart parameter to True.