quantlab.utils.returns.one_bar_returns#

quantlab.utils.returns.one_bar_returns(prices)[source]#

Return prices[t] / prices[t - 1] - 1 along the time axis, NaN on the first bar.

A return is NaN where either price is missing; a previous price of 0 gives what the division gives (inf or NaN), with no warning. The result keeps the input’s float precision (an integer input gives float64).

Parameters:
pricesnp.ndarray or xr.DataArray

An array with time on its first axis ([T] or [T, S]), or a data array with a timestamp dimension anywhere.

Returns:
np.ndarray or xr.DataArray

The returns, the shape of prices; a data array keeps its name, dimensions, their order, coordinates and attributes, as arithmetic on it does.

Examples

>>> import numpy as np
>>> one_bar_returns(np.array([[10.0, 20.0], [11.0, np.nan], [12.1, 22.0]]))
array([[nan, nan],
       [0.1, nan],
       [0.1, nan]])