investfly.models.indicator.Indicator

Base contracts for authoring custom Investfly indicators.

class Indicator(abc.ABC):

Base class for a custom indicator.

Implement getIndicatorSpec() to describe the indicator and computeSeries() to calculate its values. Investfly injects dataService before computation so the implementation can read the configured security's bars, quote, financial data, or news.

A custom indicator can be used anywhere a standard indicator can be used, including strategies, screeners, and charts.

Attributes:
  • dataService: Market-data access scoped to the security being evaluated.
Indicator()

Initialize the indicator.

Investfly provides the data service after instantiation and before computation.

@abstractmethod
def getIndicatorSpec(self) -> investfly.models.indicator.IndicatorSpec:

Return IndicatorSpec with name, description, required params, and valuetype.

See IndicatorSpec abstract class for more details.

Returns:

IndicatorSpec: The indicator specification object.

@abstractmethod
def computeSeries( self, params: Dict[str, Any]) -> List[investfly.models.common.DatedValue]:

Compute indicator series based on provided parameters.

This function must return List of indicator values instead of only the most recent single value because indicator series is required to plot in the price chart and also to use in backtest.

The indicator should use self.dataService to retrieve the data it needs (bars, quotes, financials, news). The timestamps in the DatedValue must correspond to timestamps in the retrieved data.

Arguments:
  • params: User supplied indicator parameter values. The keys match the keys from IndicatorSpec.params.
Note:

The params[StandardParams.COUNT] parameter specifies how many indicator values the function should return in the list.

  • If COUNT is NOT specified: Compute and return the FULL series based on all available data.
  • If COUNT is specified: Return only the last COUNT values.

For optimal performance, use COUNT to only request the minimum necessary amount of historical data. For example: to compute a 20-period SMA and return a single most recent value (count=1), you should request 20 bars. If count=2, you should request 21 bars; in general, for SMA, the number of bars needed is period + count - 1. If COUNT is not specified, request ALL_BARS to compute the full series.

Returns:

List of DatedValue representing indicator values for each time unit.

def computeCurrent( self, params: Dict[str, Any]) -> investfly.models.common.DatedValue:

Compute the current (most recent) indicator value.

This default implementation calls computeSeries() and returns the last value. If computing just the current value is more performant than computing the full series, Indicator implementations should override this method.

Arguments:
  • params: User supplied indicator parameter values. Same as computeSeries().
Returns:

The most recent DatedValue from the indicator series.

Raises:
  • IndexError: If the indicator series is empty.