Hi, Im trying to make a subclass of the TradeBar class, so that I can give the new class custom attributes.
What thing I'm trying to accomplish is to be able to check if a bar is bullish or bearish. And I realise that I can simply write something like this:
...
bar = ...
if bar.Close > bar.Open:
bar.isBull = True
bar.isBear = False
if bar.Close < bar.Open:
bar.isBull = False
bar.isBear = True
But I would really like to make a class called current bar and give it the attributes “isBear” and “isBull”.
Say i want to create a class called SignalBar as a subclass of TradeBar, or inheriting the TradeBar class. I would do it like this:
class SignalBar(TradeBar):
def __init__(????):
super().__init__(????)
if SignalBar.Close < SignalBar.Open:
SignalBar.isBear = True
elif SignalBar.Close > Signal.Open:
SignalBar.isBull =True
else:
SignalBar.isBull = False
SignalBar.isBear = False
Or something like that. What my problem is that everywhere I search, people seem to define the __init__ method with specific arguments.
But since I don't know where to find the original TradeBar class, I do not know what to write in the __init__ .
Would it be possible to write it like so:
class SignalBar (TradeBar):
def __init__(TradeBar):
...
I know this is a bunch of newbie questions. However, being able to ask questions directly to quantconnect-users, really gives much more insight and understanding than taking a course online or reading all of stackoverflow haha. At least thats what I've found.
Thanks for your time and help,
Bertram
Fred Painchaud
Hi Bertram,
I highly recommend you read more about programming in general and object-oriented programming in particular. Finally, about Python also. But I know this sounds wrong and elitist…
So, I will answer your question.
When you inherit a class, you want to properly initialize your super class. Because your class depends on it. And as with any class, you also want your class to be properly initialized. So, your initializer method (__init__ in Python) needs to have everything needed to do both. The minimum you'll need in your __init__ are the params needed by the super class (TradeBar). So, Time (start time), Symbol, Open, High, Low, Close, Volume, Period. If your class needs to get params on top of top to initialize your own attributes, then you also need those params.
Now, the way you pass those params to your class is up to you. You simply need the values. When you inherit a class, you usually not design it as a container of that class. So inheriting from TradeBar is different than designing a class that contains a TradeBar. Thus, while you can do everything you want, getting a TradeBar as a param to __init__ for a class that inherits from TradeBar is strange but you could use TradeBar as the container for the params you need to init your parent class…
Fred
Bertram Petersen
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!