Hello All,
I am having trouble with, what I would think, a very simple problem. I am trying to compare the current price of the TQQQ with the current value of the 50 day Exponential Moving Average (EMA50) of the TQQQ.
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
# Set the cash we'd like to use for our backtest
# This is ignored in live trading
self.SetCash(100000)
# Start and end dates for the backtest.
# These are ignored in live trading.
self.SetStartDate(2015,1,1)
self.SetEndDate(2017,12,31)
# Assets predetermined
self.tqqq = self.AddEquity("TQQQ", Resolution.Daily).Symbol
# Indicators
self.emaSmaller = self.EMA("TQQQ", 50, Resolution.Daily)
def OnData(self, slice):
if not self.Portfolio.Invested:
self.Debug("Working after PortfolioInvested")
if float(self.Securities["TQQQ"].Price) > self.emaSmaller:
self.Debug("Working after first comparison")
self.SetHoldings(self.tqqq, 1)
When I compare self.Securities["TQQQ"].Price and self.emaSmaller, I get this error:
Runtime Error: Python.Runtime.PythonException: TypeError : Cannot get managed object
at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, object, QuantConnect.Data.Slice)
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData (QuantConnect.Data.Slice slice) [0x000c6] in <91afafcb96134cb1934e681486862439>:0
at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x01260] in <7573aaf6e6a74f29b31f8e11628c060e>:0 (Open Stacktrace)
How do I fix this problem.
Thanks,
Malachi
CloudTrader
Hi Malachi,
To fix this, you should compare the two values as following:
if self.Securities["TQQQ"].Price > self.emaSmaller.Current.Value:
No need to convert self.Securities["TQQQ"].Price to float since self.Securities["TQQQ"].Price and self.emaSmaller.Current.Value are decimal types.
Malachi Bazar
Worked perfectly!
Alexander M. Garcia
Can you also translate that into C#? Â I am trying to do the same thing but comparing current market price to the current value of PSAR.
Douglas Stridsberg
Alexander - it's largely identical in C#:
if ( Securities["TQQQ"].Price > emaSmaller.Current.Value ) { }
Again - the key is to access <indicator>.Current.Value to get the current value.
Jared Broad
Also, we implicitly cast indicators to decimals so you can actually directly compare them in C#:Â
if ( Securities["TQQQ"].Price > emaSmaller ) { }
Â
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.
Malachi Bazar
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!