Skip to main content
Version: Java (Groovy)

ByEmaSimple

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

Syntax

The constructor for ByEmaSimple is shown below.

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

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.

import io.deephaven.numerics.movingaverages.ByEma
import io.deephaven.numerics.movingaverages.AbstractMa
import io.deephaven.numerics.movingaverages.ByEmaSimple
import java.util.concurrent.TimeUnit


ema1sec = new ByEmaSimple(
ByEma.BadDataBehavior.BD_SKIP,
ByEma.BadDataBehavior.BD_SKIP,
AbstractMa.Type.LEVEL,
AbstractMa.Mode.TIME,
10,
TimeUnit.SECONDS
)

chars = "ABCDE".toCharArray()

result = timeTable('00:00:01').update("Sym = chars[ new Random().nextInt(5)]", "Numbers = new Random().nextInt(100)")

ema_data = result.view("EMA_data = ema1sec.update(Timestamp, Numbers)").tail(1)
ema_data_grouped = result.view("Sym", "EMA_data = ema1sec.update(Timestamp, Numbers, Sym)").lastBy("Sym").tail(5)