OHLCV

Defined in fynance.core

class OHLCV(close, open=None, high=None, low=None, volume=None)[source]

Bases: object

Thin, numpy-backed container of aligned OHLCV series.

Holds up to five aligned 1-D float64 arrays — open, high, low, close, volume — sharing a common length. close is required; the others are optional and raise an informative error when accessed while absent. Composes numpy arrays rather than subclassing them.

Parameters:
closearray-like

Close series (required, defines the length).

open, high, low, volumearray-like, optional

Other OHLCV fields; each, when given, must match close in length.

Attributes:
open, high, low, close, volumenumpy.ndarray

Read-only float64 1-D arrays. Accessing an absent field raises ValueError.

Examples

>>> bars = OHLCV(close=[10., 11., 12.], high=[10.5, 11.5, 12.5],
...              low=[9.5, 10.5, 11.5])
>>> len(bars)
3
>>> bars.high
array([10.5, 11.5, 12.5])
>>> bars.columns
('high', 'low', 'close')
>>> bars.volume
Traceback (most recent call last):
    ...
ValueError: OHLCV has no 'volume' field
property close

Close series (always present).

property columns

Present fields, in canonical OHLCV order.

classmethod from_dict(data)[source]

Build from a mapping of field name -> array-like.

Only the canonical keys (open/high/low/close/volume) are read; close must be present.

Examples

>>> OHLCV.from_dict({"close": [1., 2.], "volume": [10., 20.]}).columns
('close', 'volume')
classmethod from_numpy(array, columns=_FIELDS)[source]

Build from a 2-D array whose columns are named by columns.

Parameters:
arraynumpy.ndarray

Shape (N, k) with k == len(columns).

columnstuple of str

Field name of each column, in order. Defaults to the full (open, high, low, close, volume).

Examples

>>> import numpy as np
>>> a = np.array([[9., 11., 8., 10.], [10., 12., 9., 11.]])
>>> OHLCV.from_numpy(a, columns=("open", "high", "low", "close")).columns
('open', 'high', 'low', 'close')
classmethod from_pandas(df, columns=_FIELDS)[source]

Build from a pandas-like DataFrame (columns matched case-insensitively).

Unlike from_polars (which scans data.columns for the fixed canonical names), columns here lets the caller declare the DataFrame’s actual column name for each OHLCV field, in (open, high, low, close, volume) order; matching against df.columns is case-insensitive on both sides.

Parameters:
dfAny

Duck-typed DataFrame-like object: only df.columns (an iterable of str) and column access df[name] (exposing .to_numpy() or .values) are required – no pandas import happens in this method.

columnstuple of str

The DataFrame’s column name for each canonical field, in (open, high, low, close, volume) order. Defaults to the canonical names themselves. A shorter tuple leaves the trailing fields unrequested (treated as absent, see below).

Returns:
OHLCV
Raises:
ValueError

If the column mapped to close – the only field OHLCV requires – cannot be found in df. Every other field follows OHLCV’s own optionality (only close is required at construction, see the class docstring): when its column is absent, that field is simply omitted rather than raising. This is why volume – the field most often missing from real OHLC feeds – can be dropped from columns or absent from df with no error; the same leniency applies to open/high/low.

Examples

>>> import pandas as pd
>>> df = pd.DataFrame({"Close": [1., 2.], "High": [2., 3.]})
>>> OHLCV.from_pandas(df).columns
('high', 'close')
classmethod from_polars(data)[source]

Build from a polars DataFrame (columns matched case-insensitively).

Any column whose lower-cased name is one of open/high/low/ close/volume is mapped to the matching field.

has(field)[source]

Whether field is present.

property high

High series (raises if absent).

property low

Low series (raises if absent).

property open

Open series (raises if absent).

to_numpy()[source]

Column-stack the present fields into a (N, k) array.

Columns follow columns (canonical OHLCV order).

Examples

>>> OHLCV(close=[1., 2., 3.], high=[2., 3., 4.]).to_numpy().shape
(3, 2)
to_pandas()[source]

Return the present fields as a pandas.DataFrame (lazy import).

Columns follow columns (canonical OHLCV order).

Raises:
ImportError

If pandas is not installed, with a clear, actionable message.

Examples

>>> OHLCV(close=[1., 2.], high=[2., 3.]).to_pandas()
   high  close
0   2.0    1.0
1   3.0    2.0
property volume

Volume series (raises if absent).