Implementation:1kbgz Tributary Streaming Rolling Calculations
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Financial_Calculations |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete streaming rolling-window calculation functions for cumulative and sliding-window statistics on data streams, provided by the Tributary library.
Description
The rolling calculations module provides functions that create downstream nodes tracking cumulative statistics:
- RollingCount - Cumulative count of inputs
- RollingMax / RollingMin - Cumulative max/min
- RollingSum - Cumulative sum (handles iterables)
- RollingAverage - Cumulative running average
- SMA - Simple Moving Average over a fixed window
- EMA - Exponential Moving Average with configurable alpha/adjust
- Last / First - Returns last/first value encountered
- Diff - Returns difference from previous value
These are monkey-patched onto the streaming Node class as methods (e.g., node.sma(), node.ema()).
Usage
Import these when you need rolling or cumulative statistics on a streaming data source. Particularly useful for financial time-series analysis.
Code Reference
Source Location
- Repository: Tributary
- File: tributary/streaming/calculations/rolling.py
Signature
def RollingCount(node):
"""Cumulative count of inputs."""
def RollingMax(node):
"""Cumulative maximum."""
def RollingMin(node):
"""Cumulative minimum."""
def RollingSum(node):
"""Cumulative sum (handles both scalars and iterables)."""
def RollingAverage(node):
"""Cumulative running average."""
def SMA(node, window_width=10, full_only=False):
"""Simple Moving Average over a window of ticks.
Args:
window_width: size of the rolling window
full_only: only emit when the window is full
"""
def EMA(node, window_width=10, full_only=False, alpha=None, adjust=False):
"""Exponential Moving Average.
Args:
window_width: size of the rolling window
alpha: smoothing factor (if None, computed as 2/(window_width+1))
adjust: use Pandas ewm adjust parameter
"""
def Last(node):
"""Return the last value encountered."""
def First(node):
"""Return the first value encountered."""
def Diff(node):
"""Return the difference between current and previous value."""
# Bindings
Node.rollingCount = RollingCount
Node.rollingSum = RollingSum
Node.sma = SMA
Node.ema = EMA
Node.diff = Diff
Import
from tributary.streaming import (
RollingCount, RollingMax, RollingMin, RollingSum,
RollingAverage, SMA, EMA, Last, First, Diff
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| node | Node | Yes | Upstream streaming node |
| window_width | int | No (SMA, EMA) | Window size (default 10) |
| full_only | bool | No (SMA, EMA) | Only emit when window is full |
| alpha | float | No (EMA) | Smoothing factor |
| adjust | bool | No (EMA) │ Pandas ewm adjust parameter |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Node | A new downstream Node emitting the rolling calculation |
Usage Examples
Simple Moving Average
import tributary.streaming as ts
# Compute 3-tick SMA of a data stream
source = ts.Curve(value=[10, 20, 30, 40, 50])
sma = SMA(source, window_width=3, full_only=True)
out = ts.Print(sma)
ts.run(out)
# Output: 20.0, 30.0, 40.0 (averages of [10,20,30], [20,30,40], [30,40,50])
Exponential Moving Average
import tributary.streaming as ts
# Compute EMA with alpha=0.5
source = ts.Curve(value=[1, 2, 3, 4, 5])
ema = EMA(source, window_width=3, alpha=0.5)
out = ts.Print(ema)
ts.run(out)
Rolling Sum and Diff
import tributary.streaming as ts
source = ts.Curve(value=[1, 2, 3, 4, 5])
cumsum = RollingSum(source)
diff = Diff(source)
print("Cumulative sum:")
ts.run(ts.Print(cumsum)) # 1, 3, 6, 10, 15
print("Diff:")
ts.run(ts.Print(diff)) # None, 1, 1, 1, 1