Back

Request for help

I am recieving the follwing error Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 0 min after MarketOpen', AttributeError : 'EquityHolding' object has no attribute 'historicalPERatio' AttributeError : 'EquityHolding' object has no attribute 'historicalPERatio' (Open Stacktrace)I cannnot seem to figure out why I am getting the error. The following is some of my code#All appropriate variables are imported before this line
# Demonstration of using coarse and fine universe selection together to filter down a smaller universe of stocks.
class FrameworkAlgorithm(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(2014,1,1) #Set Start Date
self.SetEndDate(2015,1,1) #Set End Date
self.SetCash(50000) #Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily

self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
self.AddEquity("SPY", Resolution.Daily)
self.SetBenchmark("SPY")

self.__numberOfSymbols = 100
self.__numberOfSymbolsFine = 30

self._changes = None

self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), Action(self.Rebalancing))

self.historicalPERatio = {}
self.Y = {}

self.rollingWindowSize = 20;

self.averages = { };

def CoarseSelectionFunction(self, coarse):
x = list(coarse)
CoarseWithFundamental = [x for x in coarse if (x.HasFundamentalData) and (float(x.Price) > 5)]
#in between above and Return statement is the rest of the function
return top

def FineSelectionFunction(self, fine):


self.r = [x for x in fine if x.ValuationRatios.PERatio]

for x in self.r:
if x.Symbol not in self.historicalPERatio.keys():
# Set up rolling window for new ticker
self.historicalPERatio[x.Symbol] = RollingWindow[Decimal](self.rollingWindowSize)
self.historicalPERatio[x.Symbol].Add(x.ValuationRatios.PERatio) #your job is to find the decimal current version for all the following variables
#self.prices_y[x.Symbol] = list(history.loc[x.Value]['close'])[1:]

self.r = [x for x in fine if PredictionEngine(x.historicalPERatio, x.Y ).predictionScore() > float(x.Price) or PredictionEngine(x.historicalPERatio , x.Y ).predictionScore() < float(x.Price)]
r = self.r
topFine = sorted(self.r, key=lambda x: x.ValuationRatios.PERatio, reverse = False)
self.topFine = [x.Symbol for x in topFine]


self.symbols = [i.Symbol for i in topFine]
self.y = self.History(self.symbols, self.rollingWindowSize, Resolution.Daily)
for i in self.symbols:
self.Debug(i.Symbol)

return self.symbols

def OnData(self, data):

symbols = self.symbols
for x in symbols:
if PredictionEngine(x.historicalPERatio,x.y).predictionScore() > float(x.Price):


self.SetHoldings(security, 0.1)
print("Security")
self.Debug(security)
self._changes = None
# if we have no changes, do nothing
def Rebalancing(self):
print("b")
for security in self.Portfolio.Values:
if PredictionEngine(security.historicalPERatio,security.y).predictionScore() < float(security.Price)*1.08 and security.Invested:
self.Liquidate(security.Symbol)

class PredictionEngine(object):
def __init__(self,historicalPERatio):
self.historicalPERatio = historicalPERatio
self.y = Y
def predictionScore(self):
scalar = StandardScaler()

pe = pd.DataFrame(self.historicalPERatio)

#after this line is where I do all the magical things needed to predict prices

return predictions

 

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.


Line 65. A symbol does not have a "historicalPERatio" property. Looks like you meant to use self.historicalPERatio[x].

Line 76. A security holding object also doesn't have a "historicalPERatio" property. Again, it looks like you meant to use self.historicalPERatio[security.Symbol].

0

Even after the fix I am still recieving the following error

 Runtime Error: In Scheduled Event 'SPY: MonthStart: SPY: 0 min after MarketOpen', Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the key exist in the collection and/or that collection is not empty. KeyError : (<QuantConnect.Symbol object at 0x7f0ed33c6860>,) (Open Stacktrace)

 

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