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

Hi Subarno,

Thank you for posting your code. I think the CoarseSelectionFunction() part is not complete in your algorithm: the top variable is not defined. Here I give you a clear and nice version to do Coarse Selection:

def CoarseSelectionFunction(self, coarse):
selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5],
key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in selected[:self.num_coarse]]

Besides, these algorithm examples using Coarse and Fine selection might be helpful to you. It would be easier to build new algorithms based on existing examples provided by QC. I would also recommend you work on Boot Camps to get familiar with the API in the fastest way. Thank you!

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.


I don't believe that is waht is causing the error because even after I implmented the recommended fix I still continue to recieve the following error which is basically identical to the one I got earlier

 BacktestingRealTimeHandler.Run(): There was an error in a scheduled event SPY: MonthStart: SPY: 0 min after MarketOpen. The error was KeyError : (<QuantConnect.Symbol object at 0x7f123c874f28>,)11 | 03:00:39: Algorithm (3fed5b18f528e93379b9ac8fc25a8625) Completed.12 | 03:00:40: 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 0x7f123c874f28>,) (Open Stacktrace)

 

0

Hi Subarno,

Would you mind attaching the most up-to-date version code so we can take a look and see why there is an error?

Thanks!

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.


import pandas as pd
#import datetime
import requests
import sys
from time import time, sleep
import time as systime
from statistics import mean, median
import numpy as np
import math
import collections, itertools
from sklearn.ensemble import RandomForestRegressor
import tensorflow as tf
#import tensorflow_probability as tfp
from sklearn.preprocessing import Imputer, StandardScaler, Normalizer, MinMaxScaler
from sklearn.decomposition import KernelPCA, PCA
from sklearn.preprocessing import Imputer, StandardScaler

#sorted(sortedByfactor1, key=lambda x: x.ValuationRatios.NormalizedPERatio, reverse = False)
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from clr import AddReference
AddReference("System.Core")
AddReference("System.Collections")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")

from System import *
from System.Collections.Generic import List
from QuantConnect import *
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Data.UniverseSelection import *


# 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).Symbol
self.SetBenchmark("SPY")

self.__numberOfSymbols = 400
self.__numberOfSymbolsFine = 200

self._changes = None

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

self.splotName = 'Strategy Info'
sPlot = Chart(self.splotName)
sPlot.AddSeries(Series('Leverage', SeriesType.Line, 0))
self.AddChart(sPlot)

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

self.rollingWindowSize = 90;

self.averages = { };


# Your New Python File
def CoarseSelectionFunction(self, coarse):
selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5],
key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in selected[:self.__numberOfSymbols]]

#return [ x.Symbol for x in sortedByDollarVolume[:self.__numberOfSymbols]]

#this is the code to get fundamentals data

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.historicalCFOPerShare[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 self.r if FScore(x.historicalPERatio, x.Y ).predictionScore() > float(x.Price) or FScore(x.historicalPERatio, x.Y ).predictionScore() < float(x.Price)]
#self.a = (x for x in self.r
r = self.r
#self.a = sorted(r, key=lambda x: r, reverse = False) #sorted(filtered_fine, key=lambda x: filtered_fine.predictionScore(), reverse = False)
#factor = [x for x in filtered_fine if FScore(x.
#a = self.a
topFine = sorted(self.r, key=lambda x: x.ValuationRatios.PERatio, reverse = False)
#self.dollarVolumeBySymbol[x.Symbol]
#self.topFine = [x.Symbol for x in topFine]
#self.Debug(topFine)
#topFine = [x for x in topFine if FScore(x.historicalPERatio, x.y).predictionScore()]

"""
for x in topFine:

FinalFactor.append(FScore(x.historicalPricetoCashRatio,x.TotalDebtEquityRatioGrowth,x.CFOPerShare,x.FCFRatio,x.currentPrice).predictionScore)
self.Debug(FinalFactor)
finalFactor = sorted(FinalFactor, key=lambda x: x.PredictionScore, reverse = False)
#SortedFinalFactor = sorted(FinalFactor, key=lambda x: x.ValuationRatios.NormalizedPERatio, reverse = False)
"""

#dict = {}
#for i in self.Securities:
#price = self.Securities[i].Price
#dict[i] = price

#self.price.append(dict,ignore_index=True)
#self.Debug(self.price)
#df_price = pd.DataFrame(self.price, columns=self.price.keys())
#rets = np.log(df_price).dropna()#.diff().dropna()
"""
df_price = pd.DataFrame(self.price, columns=self.price.keys())
rets = np.log(df_price).diff().dropna()
lock_value = df_price.iloc[-1]
"""

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


#self.Debug(self.symbols)

self._changes = None
return [x.Symbol for x in topFine[:self.__numberOfSymbolsFine] ]

#BluePrints for completing this:

"""

2. put the data into dataframes and split it into training and test

3. plug it into the prediction function, standardize the prediction and assign the alpha value to each security. THen do a print statement of the
dataframe with the alpha values associated with the securities

4. conduct optimization using cvxpy?

"""
def OnSecuritiesChanged(self, changes):
self._changes = changes

def OnData(self, data):
print("asdf")
symbols = self.symbols
for x in symbols:
if FScore(historicalPERatio[x],y[x]).predictionScore() > float(x.Price):

#USE THE SAME FORMAT AS FACTOR 1 in THE CLONE EXCEPT MAKE IT SO THAT IT IS BASED ON PRICE

self.SetHoldings(x.Symbo, 0.1)
print("Security")
self.Debug(x.Symbol)
#self.historicalPERatio[x.Symbol]
self._changes = None
#self.Debug("asdf")
# if we have no changes, do nothing
#if self.changes == None: return
# liquidate removed securities
#for security in self._changes.RemovedSecurities:
#if security.Invested:

#for security in self.symbols:
#self.SetHoldings(security, 0.1*finalFactor)
def Rebalancing(self):
print("b")
for security in self.Portfolio.Values:
if FScore(self.historicalPERatio[security.Symbol],self.y[security.Symbol]).predictionScore() < float(security.Price)*1.08 and security.Invested:
self.Liquidate(security.Symbol)

#self.SetHoldings(security,0)
#elif finalFactor > price*1.02 && factor or holding <=0:
#self.SetHoldings(security,0)
class FScore(object):
def __init__(self,historicalPERatio):
self.historicalPERatio = historicalPERatio
self.y = Y

def predictionScore(self):
vector_factor_direction= 0
scalar = StandardScaler()
p = 1
pe = pd.DataFrame(self.historicalPERatio)

X = pe
X = scalar.fit_transform(X)
X = pca.fit_transform(X)
Y = self.Y
Y = scalar.fit_transform(Y)
#This is the place where I generate my predictions

return predictions

 

0

This is my full code

0

Hi Subarno Sen ,

I cannot reproduce the issue with this code. There are other bugs...
For example:
Line 144: It is a list of Symbol, so items don't have Symbol attribute.
Line 171: self in historicalPERatio is missing, we get a missing attribute exception

Could you please try to fix the other bugs until we can reproduce the one you reported.

I would suggest using the Debugging Feature.

 

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.


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