Back

Custom indicator in python algorithm

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?

Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Hi, Alexandre,

How can I use existing indicator in custom ones by python?

To use e.g. BB, we need to specify symbol, which is only available from "input" parameter in Update function?

Thanks for help. 

0

the symbol does not have to be set!

just define your bolinger band and update it with the value you  have. whatever it is.

0

Hello all,

  I am new to the platform so please forgive me if I am missing something obvious. I think I have a similar question to @21pinegrove but am not sure how to implement to the solution mentioned by Michael. 

I would like to use an indicator as an input to my custom indicator with something along the lines of this:

class testIndicatr:
'''
Custom Indicator info:
https://www.quantconnect.com/forum/discussion/3383/custom-indicator-in-python-algorithm
'''
def __init__(self, name):
self.Name = name
self.Time = datetime.min
self.Value = 0
self.IsReady = False
self.queue = deque(maxlen=2)
self.Last_Value = 0
self.Roc = QCAlgorithm.ROC(period=1, selector=Field.Close)


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):
# append left makes sure most recent value = [0]
self.queue.appendleft(input.Volume)
count = len(self.queue)
self.Time = input.EndTime
self.IsReady = count == self.queue.maxlen

Of course, this doesn't work. I am not sure I am approaching it in the correct way. 

The desired result is that the indicator can take a look at the ROC indicator value and use that to calculate the custom indicator. 

Any pointers would be greatly appreciated!

0

Hi Dave,

I'm also trying to make use of a built-in indicator (LWMA in my case) within my custom indicator, also quite lost. From your code I can only point out that putting the call to Roc where you have on line 13 won't do anything because there is no context about what the current data bar is (like there would be in the initial algorithm module 'main.py' that you start with). I had more luck putting my call to LWMA inside the 'Update' function. I believe the parameter passed to 'Update' is of type 'IBaseData', however it's not as simple as passing 'input.Close' to your Roc function... I also tried using 'Field.Close' here but it complained it was the wrong type. It turns out it actually needs a 'selector', so what you should type instead is something like: Func[IBaseData, Decimal](self.convert)

...and then write a separate function called 'convert(self, bar)' that takes the current IBaseData as input and returns a decimal. I figured this out from the 'IndicatorSuiteAlgorithm.py' example posted on this page:

https://www.quantconnect.com/docs/algorithm-reference/indicators

Hope this helps you... though I'm still stuck. This seems to satisfy the compiler but my LWMA values are just returning zeros. 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed