investfly.models.indicator

Contracts for defining and consuming custom indicator values.

class ParamType(builtins.str, enum.Enum):

Indicator Param Type

class IndicatorValueType(builtins.str, enum.Enum):

Indicator ValueType can possibly used by Investfly to validate expression and optimize experience for users For e.g, all Indicators of same valueType can be plotted in the same y-axis

class IndicatorId(builtins.str, enum.Enum):

Technical indicators supported by Investfly.

This enum lists all standard technical indicators that can be used in trading strategies for technical analysis. The enum values are string identifiers that must be used when calling computeIndicatorSeries() in DataService.

Note: The enum values are strings (not the enum names) to support both standard and custom indicators. When using standard indicators, use the .value property or the string directly (e.g., "SMA" or StandardIndicatorId.SMA.value).

class IndicatorParam:

A named indicator parameter paired with its declared specification.

class StandardParams(builtins.str, enum.Enum):

Standard/common parameters used across multiple indicators.

These parameters are common to many indicators and can be referenced using StandardParams instead of indicator-specific parameter enums.

@dataclass
class IndicatorParamSpec:

Describe one accepted custom-indicator parameter.

Attributes:
  • paramType: Required data type.
  • required: Whether callers must supply the parameter.
  • defaultValue: Suggested value used by authoring interfaces.
  • options: Optional finite list of accepted values.
paramType: ParamType

Parameter Type (INTEGER, FLOAT, BOOLEAN, STRING, BARINTERVAL)

required: bool = True

Whether this parameter is required or optional

defaultValue: typing.Any | None = None

The default value for the parameter to auto-populate mainly in UI

options: Optional[List[Any]] = None

Valid value options (if any). If specified, then in the UI, this parameter renders as a dropdown select list. If left as None, parameter renders and freeform input text field.

class IndicatorSpec:

Describe a custom indicator's identity, output type, and parameters.

Attributes:
  • indicatorId: Runtime identifier assigned from the indicator class.
  • name: User-visible indicator name.
  • description: Concise explanation shown in authoring interfaces.
  • valueType: Shape of each computed value.
  • params: Accepted parameter specifications keyed by parameter name.
def addParam( self, paramName: str, paramSpec: IndicatorParamSpec) -> None:

Declare a named parameter accepted by the custom indicator.

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) -> 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.
class IndicatorDataService(abc.ABC):

Market data access for indicators with security context captured at construction.

ALL_BARS: int = -1

Constant for retrieving all available bars.

@abstractmethod
def getBars(self, numBars: int = -1) -> List[investfly.models.marketdata.Bar]:

Retrieve historical bars for the configured security.

The bar interval and lookback are captured when the service is instantiated. If lookback > 0, the most recent 'lookback' bars are automatically excluded from the result, effectively shifting the time window backwards.

Arguments:
Returns:

List of Bar objects containing OHLC data in chronological order (oldest first). If lookback > 0, the result excludes the most recent 'lookback' bars.

Raises:
  • NoDataException: If the requested data is not available.
@abstractmethod
def getNews(self) -> List[investfly.models.marketdata.StockNews]:

Retrieve latest news articles for the configured security.

@abstractmethod
def getSecurity(self) -> investfly.models.marketdata.Security:

Security context captured when the service was created.

@abstractmethod
def getBarInterval(self) -> Optional[investfly.models.marketdata.BarInterval]:

Bar interval captured when the service was created.

@abstractmethod
def getRegularSessionHours(self) -> investfly.models.marketdata.MarketHours:

Regular/liquid session hours for the configured security type (FUTURE = RTH).

class IndicatorSeries:

Represents a series of indicator values over time. Provides methods to access the latest value and detect crossovers.

Get the most recent indicator value.

Returns:

DatedValue: The latest indicator value with its timestamp

def cross_over( self, other: IndicatorSeries) -> bool:

Check if this indicator series crosses above another indicator series.

Returns True only on the bar/tick where self crosses above other. This means:

  • Previous bar: self < other
  • Current bar: self > other
Arguments:
  • other: Another IndicatorSeries to compare against
Returns:

bool: True if crossover occurred on the current bar/tick, False otherwise

def cross_under( self, other: IndicatorSeries) -> bool:

Check if this indicator series crosses below another indicator series.

Returns True only on the bar/tick where self crosses below other. This means:

  • Previous bar: self > other
  • Current bar: self < other
Arguments:
  • other: Another IndicatorSeries to compare against
Returns:

bool: True if crossunder occurred on the current bar/tick, False otherwise

def toList(self) -> List[investfly.models.common.DatedValue]:

Get all indicator values as a list.

Returns:

List[DatedValue]: List of all indicator values, ordered from oldest to newest