Hi,
I have a Python custom indicator derived from a C# Indicator class but am unable to override one of the C# methods. The value returned by the Python indicator is the C# value, not the override value.
Can a function in a derived Python class override a C# class method?
I’ve checked out the QC discussion forum but couldn’t find an similar situation, my apologies if I missed one.
The Python custom indicator is derived from the C# QuantConnect.Indicators namespace class:
public class VolumeWeightedAveragePriceIndicator : TradeBarIndicator, IIndicatorWarmUpPeriodProvider
My indicator design needs to override the C# indicator method:
protected virtual decimal GetTimeWeightedAveragePrice(TradeBar input) {
return (input.Open + input.High + input.Low + input.Value) / 4; }
My custom indicator file has the directive:
from QuantConnect.Indicators import VolumeWeightedAveragePriceIndicator as vwapOrig
But not the directive:
from clr import AddReference . . .
Which I assume the Quantconnect Backtest environment provides automatically, because everything runs with and without the clr import directive.
My custom indicator class __init__ function has:
vwapCloseInd = vwapOrig1( aSymbol, period)
Where the derived class is:
class vwapOrig1(vwapOrig):
def __init__(self, name, period):
super().__init__(name, period)
def GetTimeWeightedAveragePrice(self, input):
return input.Close
However the derived vwapOrig1 class function GetTimeWeightedAveragePrice, does not override the C# method, as expected. The custom indicator class, Update function receives the C# average price value and not the python override class input.Close price value.
Should the GetTimeWeightedAveragePrice function of the vwapOrig1 Python class derived from the C# VolumeWeightedAveragePriceIndicator class override the C# method GetTimeWeightedAveragePrice ?
Thanks for any insights.
Chris
ChrisFg
Can a function in a Python class derived from a Lean C# class override a C# method in that class?
Derek Melchin
Hi Chris,
When we subclass a C# indicator, we are able to override the methods. In the situation explained above, the GetTimeWeightedAveragePrice method of our subclass should override the parent class's method. See the attached backtest which shows a working example of this.
Best,
Derek Melchin
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.
ChrisFg
Thanks for taking the time Derek. Your clear example help me resolve my issue.
Regards, Chris
ChrisFg
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!