investfly.models.indicator.Indicator

class Indicator(abc.ABC):

The primary class to implement a custom Indicator. A Custom Indicator is like standard indicator (e.g SMA, RSI) and can be used in any place that standard indicator can be used (e.g screener, charts, strategy etc) Investfly comes with a set of standard indicators. If you find that the indicator you want is not supported or you can a small variation (e.g SMA but with using Heikin Ashi Candles), then you can use this function

@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

def getDataSourceType(self) -> investfly.models.marketdata.DataSource:

Return the DataSource that this indicator is based on. Possible values are: DataSource.BARS, DataSource.QUOTE, DataSource.NEWS, DataSource.FINANCIAL

Returns

investfly.models.DataParams.DataSource

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

Compute indicator series based on provided input timed data series and parameter values. 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 timestamps in the investfly.models.DatedValue.DatedValue must correspond to timestamps in input data The length of input data depends on context (e.g is this indicator being evaluated for backtest or screener?) and dataCountToComputeCurrentValue function below

Parameters
  • params: User supplied indicator parameter values. The keys match the keys from IndicatorSpec.params
  • data: List of timed data values as specified in Indicator.getDataSourceType.
Returns

List of investfly.models.DatedValue.DatedValue representing indicator values for each timeunit

def dataCountToComputeCurrentValue(self, params: Dict[str, Any]) -> int | None:

When this indicator is used in screener and trading strategy when is evaluated in real-time, only the "current" value of the indicator is required. The historical values are NOT required. Therefore, when the system calls computeSeries above with all available data (e.g 10 years of historical bars), then it is un-necessarily slow and wasteful. This function is used to control the size of input data that will be passed to computeSeries method above.

The default implementation tries to make the best guess, but override as needed

Parameters
  • params: User supplied input parameter values
Returns

integer representing how many input data points are required to compute the 'current' indicator value. For e.g, if this was SMA indicator with period=5, then you should return 5

def validateParams(self, paramVals: Dict[str, Any]):
def addStandardParamsToDef( self, indicatorDef: investfly.models.indicator.IndicatorSpec):