Hi all,
I am relatively new to Python and trying to play around to understand how to build an algorithm.
I have a really simple question, but dont understand why I cannot figure this out.
This part:
if self.macd > 0:
self.Debug(str("macd is over 0"))
is causing me alot of headache. Why is this not working when I have a variable > 0 in the if formula? I have tried to change it from self.macd to other variables without success. but replacing the variable with a number, like numer 1 would work.
Would anyone know why and help me out with this?
Appreciate the help!
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017,01,01) #Set Start Date
self.SetEndDate(2017,10,10) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddEquity("TSLA", Resolution.Daily)
self.smaFast = self.SMA("TSLA", 10, Resolution.Daily);
self.smaSlow = self.SMA("TSLA", 20, Resolution.Daily);
self.macd = self.MACD("TSLA", 12, 26, 9);
def OnData(self, data):
#self.Debug(str(self.macd))
if self.smaFast > self.smaSlow:
self.SetHoldings("TSLA", 1)
elif self.smaFast < self.smaSlow:
self.SetHoldings("TSLA", 0)
if self.macd > 0:
self.Debug(str("macd is over 0"))
Alexandre Catarino
Lean/QuantConnect indicators are not numbers, so you cannot compare an indicator object against a number.
Please use .Current.Value:
if self.macd.Current.Value > 0: self.Debug(str("macd is over 0"))
Harvey Vo
Many thanks Alexandre! I was not aware of this.
Would you be able to explain how I can compare self.smaFast against self.smaSlow if they are not numbers?
Appreciate the help! It least I understand where the problem was :)
Derek Tishler
To get a number from an Indicator object, you want to use ".Current.Value" as Alex mentioned.
So the Indicator is a Class with all kinds of naughty bits inside; you just want to get the current value of that indicator for use.
if self.sma_fast.Current.Value > self.sma_slow.Current.Value:
#do stuff
Harvey Vo
Many thanks for the help. Appreciate it! :)
Harvey Vo
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!