PlotIndicator for Custom Indicator written in Python
Expected Behavior
PlotIndicator method can be used by indicators defined in C# and Python.
Actual Behavior
The following exception is thrown: InvalidCastException : cannot convert object to target type.
Potential Solution
Create a C# PythonIndicator class with a public method Update, a public event Updated and protected method OnUpdated (see IndicatorBase.cs). The Update method should wrap the Update method defined in the custom indicator class and call OnUpdated.
In PlotIndicator method with the PyObject overload, we need to try to convert into a PythonIndicator in order to use Update event.
Reproducing the Problem
Add
self.PlotIndicator('cSMA', self.custom)
in Initialize method of CustomIndicatorAlgorithm.py and run the algorithm.
Checklist
- [x] I have completely filled out this template
- [x] I have confirmed that this issue exists on the current
masterbranch - [x] I have confirmed that this is not a duplicate issue by searching issues
- [x] I have provided detailed steps to reproduce the issue
Any news about this one?
I have the same issue when applying IndicatorExtensions.SMA to a custom SimpleReturn python indicator:
self.spy_simple_return = SimpleReturn('spy_sr')
self.spy_indicator = IndicatorExtensions.SMA(self.spy_simple_return, 10)
Then it get an exception as follows:
During the algorithm initialization, the following exception has occurred: InvalidCastException : cannot convert object to target type
at Python.Runtime.PyObject.AsManagedObject (System.Type t) [0x00011] in <acb2876d541541d38dcc29b7394ceb4f>:0
at QuantConnect.Indicators.IndicatorExtensions.SMA (Python.Runtime.PyObject left, System.Int32 period, System.Boolean waitForFirstToReady) [0x00008] in <300c943c785f405ea99e8312ac681a60>: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 <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at Initialize in main.py:line 37
InvalidCastException : cannot convert object to target type
at Python.Runtime.PyObject.AsManagedObject (System.Type t) [0x00011] in <acb2876d541541d38dcc29b7394ceb4f>:0
at QuantConnect.Indicators.IndicatorExtensions.SMA (Python.Runtime.PyObject left, System.Int32 period, System.Boolean waitForFirstToReady) [0x00008] in <300c943c785f405ea99e8312ac681a60>: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 <b0e1ad7573a24fd5a9f2af9595e677e7>:0
Trying to inherit SimpleReturn indicator from Indicator class produces another exception:
TypeError : cannot instantiate abstract class