tensortrade.feed.api.float.window.ewm module

ewm.py contains functions and classes for exponential weighted moving stream operations.

class tensortrade.feed.api.float.window.ewm.EWM(com: float = None, span: float = None, halflife: float = None, alpha: float = None, min_periods: int = 0, adjust: bool = True, ignore_na: bool = False)[source]

Bases: tensortrade.feed.core.base.Stream

Provide exponential weighted (EW) functions.

Exactly one parameter: com, span, halflife, or alpha must be provided.

Parameters:
  • com (float, optional) – Specify decay in terms of center of mass, \(\alpha = 1 / (1 + com)\), for \(com \geq 0\).
  • span (float, optional) – Specify decay in terms of span, \(\alpha = 2 / (span + 1)\), for \(span \geq 1\).
  • halflife (float, str, timedelta, optional) – Specify decay in terms of half-life, \(\alpha = 1 - \exp\left(-\ln(2) / halflife\right)\), for \(halflife > 0\). If times is specified, the time unit (str or timedelta) over which an observation decays to half its value. Only applicable to mean() and halflife value will not apply to the other functions.
  • alpha (float, optional) – Specify smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\).
  • min_periods (int, default 0) – Minimum number of observations in window required to have a value (otherwise result is NA).
  • adjust (bool, default True) –

    Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average). - When adjust=True (default), the EW function is calculated using weights

    \(w_i = (1 - \alpha)^i\). For example, the EW moving average of the series [\(x_0, x_1, ..., x_t\)] would be:
    \[y_t = \frac{x_t + (1 - \alpha)x_{t-1} + (1 - \alpha)^2 x_{t-2} + ... + (1 - \alpha)^t x_0}{1 + (1 - \alpha) + (1 - \alpha)^2 + ... + (1 - \alpha)^t}\]
    • When adjust=False, the exponentially weighted function is calculated recursively:
    \[\begin{split}\begin{split} y_0 &= x_0\\ y_t &= (1 - \alpha) y_{t-1} + \alpha x_t, \end{split}\end{split}\]
  • ignore_na (bool, default False) – Ignore missing values when calculating weights. - When ignore_na=False (default), weights are based on absolute positions. - When ignore_na=True, weights are based on relative positions.

See also

References

[1]https://github.com/pandas-dev/pandas/blob/d9fff2792bf16178d4e450fe7384244e50635733/pandas/core/window/ewm.py#L65
forward() → Tuple[List[float], List[float]][source]

Generates the next value from the underlying data streams.

Returns:T – The next value in the stream.
has_next() → bool[source]

Checks if there is another value.

Returns:bool – If there is another value or not.
mean() → tensortrade.feed.core.base.Stream[float][float][source]

Computes the exponential weighted moving average.

Returns:Stream[float] – The exponential weighted moving average stream based on the underlying stream of values.
reset() → None[source]

Resets all inputs to and listeners of the stream and sets stream value to None.

std(bias=False) → tensortrade.feed.core.base.Stream[float][float][source]

Computes the exponential weighted moving standard deviation.

Returns:Stream[float] – The exponential weighted moving standard deviation stream based on the underlying stream of values.
var(bias=False) → tensortrade.feed.core.base.Stream[float][float][source]

Computes the exponential weighted moving variance.

Returns:Stream[float] – The exponential weighted moving variance stream based on the underlying stream of values.
class tensortrade.feed.api.float.window.ewm.ExponentialWeightedMovingAverage(alpha: float, adjust: bool, ignore_na: bool, min_periods: int)[source]

Bases: tensortrade.feed.core.base.Stream

A stream operator that computes an exponential weighted moving average on a given float stream.

Parameters:
  • alpha (float) – The smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\).
  • adjust (bool) – Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
  • ignore_na (bool) – Ignore missing values when calculating weights.
  • min_periods (int) – Minimum number of observations in window required to have a value (otherwise result is NA).

References

[1]https://github.com/pandas-dev/pandas/blob/d9fff2792bf16178d4e450fe7384244e50635733/pandas/_libs/window/aggregations.pyx#L1801
forward() → float[source]

Generates the next value from the underlying data streams.

Returns:T – The next value in the stream.
has_next() → bool[source]

Checks if there is another value.

Returns:bool – If there is another value or not.
reset() → None[source]

Resets all inputs to and listeners of the stream and sets stream value to None.

class tensortrade.feed.api.float.window.ewm.ExponentialWeightedMovingCovariance(alpha: float, adjust: bool, ignore_na: bool, min_periods: int, bias: bool)[source]

Bases: tensortrade.feed.core.base.Stream

A stream operator that computes an exponential weighted moving average on a given float stream.

Parameters:
  • alpha (float) – The smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\).
  • adjust (bool) – Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
  • ignore_na (bool) – Ignore missing values when calculating weights.
  • min_periods (int) – Minimum number of observations in window required to have a value (otherwise result is NA).
  • bias (bool) – Use a standard estimation bias correction
forward() → float[source]

Generates the next value from the underlying data streams.

Returns:T – The next value in the stream.
has_next() → bool[source]

Checks if there is another value.

Returns:bool – If there is another value or not.
reset() → None[source]

Resets all inputs to and listeners of the stream and sets stream value to None.

tensortrade.feed.api.float.window.ewm.ewm(s: tensortrade.feed.core.base.Stream[float][float], com: float = None, span: float = None, halflife: float = None, alpha: float = None, min_periods: int = 0, adjust: bool = True, ignore_na: bool = False) → tensortrade.feed.core.base.Stream[typing.Tuple[typing.List[float], typing.List[float]]][Tuple[List[float], List[float]]][source]

Computes the weights and values in order to perform an exponential weighted moving operation.

Parameters:
  • s (Stream[float]) – A float stream.
  • com (float, optional) – Specify decay in terms of center of mass, \(\alpha = 1 / (1 + com)\), for \(com \geq 0\).
  • span (float, optional) – Specify decay in terms of span, \(\alpha = 2 / (span + 1)\), for \(span \geq 1\).
  • halflife (float, optional) – Specify decay in terms of half-life, \(\alpha = 1 - \exp\left(-\ln(2) / halflife\right)\), for \(halflife > 0\).
  • alpha (float, optional) – Specify smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\).
  • min_periods (int, default 0) – Minimum number of observations in window required to have a value (otherwise result is NA).
  • adjust (bool, default True) – Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
  • ignore_na (bool, default False) – Ignore missing values when calculating weights.
Returns:

Stream[Tuple[List[float], List[float]]] – A stream of weights and values to be used for computation of exponential weighted moving operations.