问题描述
我看过其他线程,但无法根据它弄清楚。
类DataConsolidationAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required,as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2017,1,1) #Set Start Date
self.SetEndDate(2020,1) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetbrokerageModel(brokerageName.Fxcmbrokerage)
symbols = [self.AddForex(ticker,Resolution.Minute).Symbol
for ticker in ["EURUSD"]]
self.SetBenchmark('SPY')
self.slow = self.EMA("EURUSD",200,Resolution.Daily)
self.SetWarmUp(200)
def OnData(self,data):
# Simple buy and hold template
self.low = self.MIN("EURUSD",7,Resolution.Daily,Field.Low)
self.high = self.MAX("EURUSD",Field.High)
#fxQuoteBars = data.QuoteBars
#QuoteBar = fxQuoteBars['EURUSD'].Close
#self.QuoteBar = self.History("EURUSD",TimeSpan.FromDays(1),Resolution.Daily)
self.quoteBar = data['EURUSD'] ## EURUSD QuoteBar
#self.Log(f"Mid-point open price: {quoteBar.Open}")
self.closeBar = (self.quoteBar.Close) ## EURUSD Bid Bar
self.history7days = self.History(["EURUSD"],Resolution.Daily)
if self.closeBar <= self.low and self.Forex["EURUSD"].Price > self.slow.Current.Value:
self.SetHoldings("EURUSD",1.0)
if self.closeBar > self.high:
self.SetHolding("EURUSD",0.0)
运行时错误:类型错误:无法获取托管对象 在 main.py:line 50 中的 OnData :: if self.closeBar self.slow.Current.Value: 类型错误:无法获取托管对象
解决方法
我遇到了类似的错误,并通过确保我尝试在 if 语句中与 、= 等进行比较的数据类型属于相同类型来解决它。
重新定义您要比较的指标作为 OnData 中的本地指标,如下所示,您的所有指标都将是相同的数据类型:
def Initialize(self):
'''Initialise the data and resolution required,as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2017,1,1) #Set Start Date
self.SetEndDate(2020,1) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
symbols = [self.AddForex(ticker,Resolution.Minute).Symbol
for ticker in ["EURUSD"]]
self.SetBenchmark('SPY')
self.slow = self.EMA("EURUSD",200,Resolution.Daily)
self.SetWarmUp(200)
# Simple buy and hold template
self.low = self.MIN("EURUSD",7,Resolution.Daily,Field.Low)
self.high = self.MAX("EURUSD",Field.High)
#fxQuoteBars = data.QuoteBars
#QuoteBar = fxQuoteBars['EURUSD'].Close
#self.QuoteBar = self.History("EURUSD",TimeSpan.FromDays(1),Resolution.Daily)
self.quoteBar = data['EURUSD'] ## EURUSD QuoteBar
#self.Log(f"Mid-point open price: {quoteBar.Open}")
self.closeBar = (self.quoteBar.Close) ## EURUSD Bid Bar
self.history7days = self.History(["EURUSD"],Resolution.Daily)
def OnData(self,data):
closebar = self.closeBar.Current.Value
low = self.low.Current.Value
price = self.Forex["EURUSD"].Price
slow = self.slow.Current.Value
high = self.high.Current.Value
if closeBar <= low and price > slow :
self.SetHoldings("EURUSD",1.0)
if closeBar > high:
self.SetHolding("EURUSD",0.0)