Custom Indicators


Investfly comes with a set of standard fundamental and technical indicators such as SMA, RSI, MACD etc. These technical indicators can be used in screeners, alerts, trading strategy or simply plotted in a price chart. If these standard technical indicators are not enough, Investfly allows you to define your own technical indicator by writing python code. Once you have defined a custom indicator, it can be used in all places (screeners, trading strategy, plotted in price chart) just like standard indicators. The rest of the document describes how to define and add custom indicators in Investfly. We also have many samples that can be used together with this document. This document assumes that you are proficient in Python coding language and object oriented programming.

Indicator Data Types

  • Indicator

    Class Indicator is the most abstract representation of a technical indicator. This class is designed to be flexible enough to model any arbitrary indicator that computes value based on any arbitrary input. For e.g, you can write an indicator to compute value based on social media sentiment, historical price data, or even the number of stars in the sky. We inject your indicator with SecurityDataProvider class that can be used to retrieve historical price data, but if your indicator is based on other kinds of data (e.g number of mentions in social media like twitter), it is your responsibility to retrieve the input data your indicator requires. You can use Python requests module to make web service API calls to other data sources to fetch data. To define custom technical indicator, you must extend from Indicator base class and implement all abstract methods defined below.

    @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 Indicator must provide its definition that includes its name, description, required parameters, and value type. The following table lists the required attributes of IndicatorDefinition object. You must return IndicatorDefinition from your Indicator implementation class.

    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 as “fast_period”, “slow_period”, “signal_period” parameters. Dictionary of paramName → ParamType. ParamType is string Enum with following values: INTEGER, FLOAT, BOOLEAN, STRING, BARINTERVAL
    valueType Enum ValueType DOLLAR: Indicators the value for this indicator can be plotted in the same y-axis in the 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 indicator code is run under restricted and sandboxed Python environment. This is for obvious reason - since we run your code in our server, we need to ensure that it cannot do any malicious tasks. In such restricted environment, only a few white-listed safe modules can be imported. Obviously, running system operations such as file IO will not work. In addition, wildcard imports will also not work. If you discover any safe function that is blocked, please contact us and we can make it available.