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_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)
property volume

Volume series (raises if absent).