Hi, I'm creating an algorithm where I try to scalp on certain signalbars. However, I would really like to clean op the code, so that I can check if the current bar is bearish or bullish.
The difficult way would be like so:
if currentBar.Close - currentBar.Open < 0: #This means the bar i bearish.
# And if it has to be Bullish:
if curenBar.Close - CurrentBar.Open > 0: # Bar is bullish.
What I really want to do instead, is writing it so that I in an ‘if-statement’ can do like so:
if currentBar.isBear:
How do I go about making an Attribute to a TradeBar or QuoteBar. Or whatever what I am trying to do is called.
Hope someone can help me clean up my code.
Thanks in Advance:))
Fred Painchaud
Hi Bertram,
First thing you can do is use this code to determine bearish/bullish:
if bar.Close > bar.Open: #bullish
if bar.Open > bar.Close: #bearish
But beware because bar.Open can also be == to bar.Close… and then, your algo will see neither bearish or bullish which can affect what you do thenafter.
Now, in Python you can dynamically add attributes to objects. So you can do:
Thereafter, you can use:
Beware that your attributes have not been added to the class defining “bar” (TradeBar most likely). So doing the above won't add isBear and isBull to all “bars”. Just the single one there. But if your algo adds the attributes consistently, it will work. It's not elegant, but it works.
You can also dynamically add attributes to classes in Python. But as I never tested that in LEAN which involves Python.NET to bridge between Python and C#, I don't know if it works - plus, it is an advanced subject anyway.
Finally, because TradeBar (say it is what you are using here) is public, you can inherit from it and define your two additional attributes in it. Then, you would want to look at inheritance in Python. It's less advanced than dynamically adding attributes to classes but it is not something that can be summarized in a post like this one.
Hope this helps,
Fred
Bertram Petersen
Thank you so much. This was very insightful.
However, I just spent some time looking in to inheritance, and it looks like the way to go. But there is one thing that stops me.
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:
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:
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,
Glad it helped.
I answered your last post in your other thread… https://www.quantconnect.com/forum/discussion/13207/inheriting-classes
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!