Back

Compare Current Price to EMA Indicator

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

Update Backtest








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.

1

Worked perfectly!

0

Update Backtest





0

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.


Loading...

This discussion is closed