investfly.models.indicator
Contracts for defining and consuming custom indicator values.
Indicator Param Type
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
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).
A named indicator parameter paired with its declared specification.
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.
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.
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.
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.
Initialize the indicator.
Investfly provides the data service after instantiation and before computation.
Return IndicatorSpec with name, description, required params, and valuetype.
See IndicatorSpec abstract class for more details.
Returns:
IndicatorSpec: The indicator specification object.
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. Ifcount=2, you should request 21 bars; in general, for SMA, the number of bars needed isperiod + count - 1. If COUNT is not specified, request ALL_BARS to compute the full series.
Returns:
List of
DatedValuerepresenting indicator values for each time unit.
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
DatedValuefrom the indicator series.
Raises:
- IndexError: If the indicator series is empty.
Market data access for indicators with security context captured at construction.
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:
- numBars: Number of bars to return. Use IndicatorDataService.ALL_BARS to retrieve all available bars.
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.
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
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
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