Custom Indicators


Investfly offers a comprehensive set of standard fundamental and technical indicators such as SMA, RSI, MACD, and more. These indicators can be utilized in screeners, alerts, trading strategies, or simply plotted on a price chart. If the standard indicators do not meet your needs, Investfly allows you to create custom technical indicators using Python code. Once defined, these custom indicators can be used across all features (screeners, trading strategies, price charts) just like the standard indicators. This document provides a detailed guide on how to define and add custom indicators in Investfly. We also offer numerous samples to assist you. This guide assumes you are proficient in Python and object-oriented programming.

Indicator Data Types

  • Indicator

    The Indicator class is the most abstract representation of a technical indicator. It is designed to be flexible enough to model any indicator that computes values based on various inputs. For example, you can create an indicator based on social media sentiment, historical price data, or other data sources. We provide the SecurityDataProvider class to retrieve historical price data, but if your indicator relies on other data (e.g., social media mentions), you must fetch this data yourself using Python's requests module. To define a custom technical indicator, extend the Indicator base class and implement the following abstract methods:

                                    @abstractmethoddef getIndicatorSpec(self) -> IndicatorSpec:
                                    # Return IndicatorDefinition with name, description, required params, and valuetype
                                    # See IndicatorSpec abstract class for more details
                                    pass
    
    
                                    @abstractmethod
                                    def computeSeries(self, params: Dict[str, Any], bars: List[Bar]) -> List[DatedValue]:
                                    # This is the main function which must compute and return value. This function must return within 1 sec
                                    # or it will be killed
                                    pass
                                    
  • IndicatorSpec

    Every custom indicator must provide its definition, including its name, description, required parameters, and value type. The following table lists the required attributes of the IndicatorSpec object. You must return an IndicatorSpec from your custom indicator implementation.

    Attribute Python Data Type Description
    name str Indicator Name, e.g., “MyIndicator”
    description str Indicator Description, e.g., “Predict tomorrow’s price”
    params dict[str, ParamSpec] Parameters needed for this indicator, e.g., SMA indicator has “period” parameter. MACD indicator has “fast_period”, “slow_period”, “signal_period” parameters. Dictionary of paramName → ParamType. ParamType is a string Enum with the following values: INTEGER, FLOAT, BOOLEAN, STRING, BARINTERVAL
    valueType Enum ValueType DOLLAR: Indicates the value for this indicator can be plotted on the same y-axis in the price chart. NUMBER: Unit-less numeric value, e.g., RSI. BOOLEAN: Boolean value. PERCENT: Values that are in the range 0-1.

Python Restrictions

Your custom indicator code runs in a restricted and sandboxed Python environment for security reasons. This ensures that the code cannot perform any malicious tasks on our servers. Only a few white-listed safe modules can be imported. System operations such as file IO are not allowed, and wildcard imports are also restricted. If you find any safe function that is blocked, please contact us, and we can make it available.