I am trying to create a vortex indicator class however I'm struggling when trying to output multiple values from the indicator. As I am not outputing one Value I don't set the self.Value but rather self.Uptrend and self.Downtrend corresponding to the respective lines for the Vortex Indicator:
from collections import deque
import numpy as np
from datetime import datetime
class VI:
def __init__(self, period):
self.Name = "Vortex"
self.Time = datetime.min
self.IsReady = False
self.Uptrend = 0
self.Downtrend = 0
self.queue = deque(maxlen=period+1)
self.Period = period
def __repr__(self):
return f"{self.Name} -> IsReady: {self.IsReady}, Time: {self.Time}, Value: {self.Value}"
def Update(self, input):
self.queue.append(input)
self.Time = input.EndTime
sum_trn = 0
sum_vm_uptrend = 0
sum_vm_downtrend = 0
lows = np.array([x.Low for x in self.deque])
highs = np.array([x.High for x in self.deque])
closes = np.array([x.Close for x in self.deque])
for i in range(0, self.queue.maxlen - 1):
sum_trn += np.nanmax(
np.array([
highs[i] - lows[i],
highs[i] - closes[i+1],
lows[i] - closes[i+1]
]))
sum_vm_uptrend += abs(highs[i] - lows[i+1])
sum_vm_downtrend += abs(lows[i] - highs[i+1])
self.Downtrend = sum_vm_uptrend / sum_trn
self.Uptrend = sum_vm_downtrend / sum_trn
self.IsReady = len(self.queue) == self.queue.maxlen
return self.IsReady
This however throws an error as I have not used self.Value:
During the algorithm initialization, the following exception has occurred: NotImplementedException : Indicator.Value must be implemented. Please implement this missing method in VI
at QuantConnect.Indicators.PythonIndicator.SetIndicator (Python.Runtime.PyObject indicator) [0x00097] in :0
at QuantConnect.Indicators.PythonIndicator..ctor (Python.Runtime.PyObject indicator) [0x0000c] in :0
at QuantConnect.Algorithm.QCAlgorithm.WrapPythonIndicator (Python.Runtime.PyObject pyObject) [0x0003c] in :0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, Python.Runtime.PyObject indicator, QuantConnect.Data.Consolidators.IDataConsolidator consolidator, Python.Runtime.PyObject selector) [0x0007d] in :0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, Python.Runtime.PyObject indicator, System.Nullable`1[T] resolution, Python.Runtime.PyObject selector) [0x0000d] in :0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in :0
at Initialize in main.py:line 101
NotImplementedException : Indicator.Value must be implemented. Please implement this missing method in VI
at QuantConnect.Indicators.PythonIndicator.SetIndicator (Python.Runtime.PyObject indicator) [0x00097] in :0
at QuantConnect.Indicators.PythonIndicator..ctor (Python.Runtime.PyObject indicator) [0x0000c] in :0
at QuantConnect.Algorithm.QCAlgorithm.WrapPythonIndicator (Python.Runtime.PyObject pyObject) [0x0003c] in :0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, Python.Runtime.PyObject indicator, QuantConnect.Data.Consolidators.IDataConsolidator consolidator, Python.Runtime.PyObject selector) [0x0007d] in :0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, Python.Runtime.PyObject indicator, System.Nullable`1[T] resolution, Python.Runtime.PyObject selector) [0x0000d] in :0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in :0
Is there any way to output two values from a custom indicator, and then be able to plot that indicator using self.PlotIndicator()?
Shile Wen
Hi Marcel,
An indicator needs to define a .Value field, e.g.
class VI: def __init__(self, period): self.Value = 0
I suggest having one of the indicator values be the .Value field, and to plot the indicator values with the vanilla self.Plot method.
Best,
Shile Wen
Marcel Madden
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!