We have added a new feature yesterday: custom indicator for python algorithm.
It is quite simple! Basically we just need to create a class with an Update method:

class MyCustomIndicator: def __init__(self, params): self.params = params def Update(self, input): pass

where input can be any IBaseData object (Tick, TradeBar, QuoteBar, etc).

The Update method with one argument is important because if we want to register the indicator for automatic updates, the engine will look for that method in order to pass an IBaseData object.

Here is an example of a simple moving average indicator:

# Python implementation of SimpleMovingAverage. # Represents the traditional simple moving average indicator (SMA). class CustomSimpleMovingAverage: def __init__(self, name, period): self.Name = name self.Time = datetime.min self.Value = 0 self.IsReady = False self.queue = deque(maxlen=period) def __repr__(self): return "{0} -> IsReady: {1}. Time: {2}. Value: {3}".format(self.Name, self.IsReady, self.Time, self.Value) # Update method is mandatory def Update(self, input): self.queue.appendleft(input.Close) count = len(self.queue) self.Time = input.EndTime self.Value = sum(self.queue) / count self.IsReady = count == self.queue.maxlen

We advice users to implement the boolean IsReady attribute, since it is ofter used when dealing with built-in indicators.

Like the built-in indicators, we can register the indicator in Initialize method:

# In Initialize self.custom = CustomSimpleMovingAverage('custom', 60) self.RegisterIndicator("SPY", self.custom, Resolution.Minute)

C# users, please check out this tutorial: How Do I Create an Indicator?

Author