Skip to content

ohlcv_accessors module

Custom pandas accessors for OHLC(V) data.

Methods can be accessed as follows:

The accessors inherit vectorbt.generic.accessors.

Note

Accessors do not utilize caching.

Column names

By default, vectorbt searches for columns with names 'open', 'high', 'low', 'close', and 'volume' (case doesn't matter). You can change the naming either using ohlcv.column_names in settings, or by providing column_names directly to the accessor.

>>> import pandas as pd
>>> import vectorbt as vbt

>>> df = pd.DataFrame({
...     'my_open1': [2, 3, 4, 3.5, 2.5],
...     'my_high2': [3, 4, 4.5, 4, 3],
...     'my_low3': [1.5, 2.5, 3.5, 2.5, 1.5],
...     'my_close4': [2.5, 3.5, 4, 3, 2],
...     'my_volume5': [10, 11, 10, 9, 10]
... })

>>> # vectorbt can't find columns
>>> df.vbt.ohlcv.get_column('open')
None

>>> my_column_names = dict(
...     open='my_open1',
...     high='my_high2',
...     low='my_low3',
...     close='my_close4',
...     volume='my_volume5',
... )
>>> ohlcv_acc = df.vbt.ohlcv(freq='d', column_names=my_column_names)
>>> ohlcv_acc.get_column('open')
0    2.0
1    3.0
2    4.0
3    3.5
4    2.5
Name: my_open1, dtype: float64

Stats

>>> ohlcv_acc.stats()
Start                           0
End                             4
Period            5 days 00:00:00
First Price                   2.0
Lowest Price                  1.5
Highest Price                 4.5
Last Price                    2.0
First Volume                   10
Lowest Volume                   9
Highest Volume                 11
Last Volume                    10
Name: agg_func_mean, dtype: object

Plots

OHLCVDFAccessor class has a single subplot based on OHLCVDFAccessor.plot() (without volume):

>>> ohlcv_acc.plots(settings=dict(plot_type='candlestick'))


OHLCVDFAccessor class

OHLCVDFAccessor(
    obj,
    column_names=None,
    **kwargs
)

Accessor on top of OHLCV data. For DataFrames only.

Accessible through pd.DataFrame.vbt.ohlcv.

Superclasses

Inherited members


close property

Close series.


column_names property

Column names.


get_column method

OHLCVDFAccessor.get_column(
    col_name
)

Get column from OHLCVDFAccessor.column_names.


high property

High series.


low property

Low series.


metrics class variable

Metrics supported by OHLCVDFAccessor.

Config({
    "start": {
        "title": "Start",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e0103a0>",
        "agg_func": null,
        "tags": "wrapper"
    },
    "end": {
        "title": "End",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010430>",
        "agg_func": null,
        "tags": "wrapper"
    },
    "period": {
        "title": "Period",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e0104c0>",
        "apply_to_timedelta": true,
        "agg_func": null,
        "tags": "wrapper"
    },
    "first_price": {
        "title": "First Price",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010550>",
        "resolve_ohlc": true,
        "tags": [
            "ohlcv",
            "ohlc"
        ]
    },
    "lowest_price": {
        "title": "Lowest Price",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e0105e0>",
        "resolve_ohlc": true,
        "tags": [
            "ohlcv",
            "ohlc"
        ]
    },
    "highest_price": {
        "title": "Highest Price",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010670>",
        "resolve_ohlc": true,
        "tags": [
            "ohlcv",
            "ohlc"
        ]
    },
    "last_price": {
        "title": "Last Price",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010700>",
        "resolve_ohlc": true,
        "tags": [
            "ohlcv",
            "ohlc"
        ]
    },
    "first_volume": {
        "title": "First Volume",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010790>",
        "resolve_volume": true,
        "tags": [
            "ohlcv",
            "volume"
        ]
    },
    "lowest_volume": {
        "title": "Lowest Volume",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010820>",
        "resolve_volume": true,
        "tags": [
            "ohlcv",
            "volume"
        ]
    },
    "highest_volume": {
        "title": "Highest Volume",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e0108b0>",
        "resolve_volume": true,
        "tags": [
            "ohlcv",
            "volume"
        ]
    },
    "last_volume": {
        "title": "Last Volume",
        "calc_func": "<function OHLCVDFAccessor.<lambda> at 0x7f951e010940>",
        "resolve_volume": true,
        "tags": [
            "ohlcv",
            "volume"
        ]
    }
})

Returns OHLCVDFAccessor._metrics, which gets (deep) copied upon creation of each instance. Thus, changing this config won't affect the class.

To change metrics, you can either change the config in-place, override this property, or overwrite the instance variable OHLCVDFAccessor._metrics.


ohlc property

Open, high, low, and close series.


open property

Open series.


plot method

OHLCVDFAccessor.plot(
    plot_type=None,
    show_volume=None,
    ohlc_kwargs=None,
    volume_kwargs=None,
    ohlc_add_trace_kwargs=None,
    volume_add_trace_kwargs=None,
    fig=None,
    **layout_kwargs
)

Plot OHLCV data.

Args

plot_type

Either 'OHLC', 'Candlestick' or Plotly trace.

Pass None to use the default.

show_volume : bool
If True, shows volume as bar chart.
ohlc_kwargs : dict
Keyword arguments passed to plot_type.
volume_kwargs : dict
Keyword arguments passed to plotly.graph_objects.Bar.
ohlc_add_trace_kwargs : dict
Keyword arguments passed to add_trace for OHLC.
volume_add_trace_kwargs : dict
Keyword arguments passed to add_trace for volume.
fig : Figure or FigureWidget
Figure to add traces to.
**layout_kwargs
Keyword arguments for layout.

Usage

>>> import vectorbt as vbt

>>> vbt.YFData.download("BTC-USD").get().vbt.ohlcv.plot()


plots_defaults property

Defaults for PlotsBuilderMixin.plots().

Merges GenericAccessor.plots_defaults and ohlcv.plots from settings.


stats_defaults property

Defaults for StatsBuilderMixin.stats().

Merges GenericAccessor.stats_defaults and ohlcv.stats from settings.


subplots class variable

Subplots supported by OHLCVDFAccessor.

Config({
    "plot": {
        "title": "OHLC",
        "xaxis_kwargs": {
            "showgrid": true,
            "rangeslider_visible": false
        },
        "yaxis_kwargs": {
            "showgrid": true
        },
        "check_is_not_grouped": true,
        "plot_func": "plot",
        "show_volume": false,
        "tags": "ohlcv"
    }
})

Returns OHLCVDFAccessor._subplots, which gets (deep) copied upon creation of each instance. Thus, changing this config won't affect the class.

To change subplots, you can either change the config in-place, override this property, or overwrite the instance variable OHLCVDFAccessor._subplots.


volume property

Volume series.