Skip to main content
Version: Python

ByEmaSimple

ByEmaSimple calculates Exponential Moving Averages (EMA) in the Deephaven Query Language.

Syntax

The constructor for ByEmaSimple is shown below.

ByEmaSimple(
nullBehavior,
nanBehavior,
mode,
timescale,
timeUnit,
type
)

Parameters

ParameterTypeDescription
nullBehaviorBadDataBehavior
nanBehaviorBadDataBehavior
modeenum
  • TICK uses each new entry of data to determine the EMA window.
  • TIME uses time to determine the EMA window.
timescaledouble

The number of units for the EMA window.

timeUnitenum
  • DAYS - Time unit representing twenty four hours.
  • HOURS - Time unit representing sixty minutes.
  • MICROSECONDS - Time unit representing one thousandth of a millisecond.
  • MILLISECONDS - Time unit representing one thousandth of a second.
  • MINUTES - Time unit representing sixty seconds.
  • NANOSECONDS - Time unit representing one thousandth of a microsecond.
  • SECONDS - Time unit representing one second.
typeenum
  • LEVEL - sets the first EMA value to the first value recorded.
  • DIFFERENCE - sets the first EMA value to zero assuming the change is initially zero.

Returns

Parameterized constructor to use for calculating an EMA.

Examples

In the following example, we use the time-table and update methods to create a table with random data to simulate real-time data. We then use ByEmaSimple to configure what we want for our EMA.

ema1sec is set to use BD_SKIP to skip null values and skip NaN values. The type is set to LEVEL and we are using TIME rather than TICKS.

ema_data then uses ema1sec to calculate the total EMA on a timescale of 10 seconds.

from deephaven.experimental.ema import ByEmaSimple
from deephaven import time_table
import random

def random_char():
return random.choice(['A', 'B', 'C', 'D', 'E', 'F'])

ema1sec = ByEmaSimple('BD_SKIP','BD_SKIP','TIME',10,'SECONDS', type='LEVEL')

result = time_table('00:00:01').update(formulas=["Sym = (String)random_char()", "Numbers = (int)random.randint(0, 100)"])

ema_data = result.view(formulas=["EMA_data = ema1sec.update(Timestamp, Numbers)"]).tail(1)
ema_data_grouped = result.view(formulas=["Sym", "EMA_data = ema1sec.update(Timestamp, Numbers, Sym)"]).last_by(by=["Sym"]).tail(5)