Using the same algorithm code that I have in live. From today the backtest is no longer run, the strange thing that 10 minutes after running the last backtest, it stopped working. After about an hour of trying, one was successful and immediately afterwards kept giving the same error in the events section.
The source code is that of this topic: Intersection of Roc,
all the algorithms contained in this topic have stopped working and have the same error.
Runtime Error: In Scheduled Event 'EveryDay: SPY: 100 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 SPY key exist in the collection and/or that collection is not empty.
at _validate_read_indexer
key=key in indexing.py:line 1177
KeyError : "None of [Index([SPY], dtype='object', name='symbol')] are in the [columns]"
import numpy as np
# ------------------------------------------------------------------
STK = ['QQQ']; BND = ['TLT']; VOLA = 126; BASE_RET = 85; LEV = 1.00;
PAIRS = ['SLV', 'GLD', 'XLI', 'XLU', 'DBB', 'UUP']
# ------------------------------------------------------------------
class DualMomentumInOut(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2021, 3, 4)
self.cap = 10000
self.SetCash(self.cap)
self.STK = self.AddEquity('QQQ', Resolution.Minute).Symbol
self.BND = self.AddEquity('TLT', Resolution.Minute).Symbol
self.ASSETS = [self.STK, self.BND]
self.SLV = self.AddEquity('SLV', Resolution.Daily).Symbol
self.GLD = self.AddEquity('GLD', Resolution.Daily).Symbol
self.XLI = self.AddEquity('XLI', Resolution.Daily).Symbol
self.XLU = self.AddEquity('XLU', Resolution.Daily).Symbol
self.DBB = self.AddEquity('DBB', Resolution.Daily).Symbol
self.UUP = self.AddEquity('UUP', Resolution.Daily).Symbol
self.MKT = self.AddEquity('SPY', Resolution.Daily).Symbol
self.pairs = [self.SLV, self.GLD, self.XLI, self.XLU, self.DBB, self.UUP]
self.bull = 1
self.count = 0
self.outday = 0
self.wt = {}
self.real_wt = {}
self.mkt = []
self.SetWarmUp(timedelta(350))
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 100),
self.daily_check)
symbols = [self.MKT] + self.pairs
for symbol in symbols:
self.consolidator = TradeBarConsolidator(timedelta(days=1))
self.consolidator.DataConsolidated += self.consolidation_handler
self.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
self.history = self.History(symbols, VOLA + 1, Resolution.Daily)
if self.history.empty or 'close' not in self.history.columns:
return
self.history = self.history['close'].unstack(level=0).dropna()
def consolidation_handler(self, sender, consolidated):
self.history.loc[consolidated.EndTime, consolidated.Symbol] = consolidated.Close
self.history = self.history.iloc[-(VOLA + 1):]
def daily_check(self):
vola = self.history[[self.MKT]].pct_change().std() * np.sqrt(252)
wait_days = int(vola * BASE_RET)
period = int((1.0 - vola) * BASE_RET)
r = self.history.pct_change(period).iloc[-1]
exit = ((r[self.SLV] < r[self.GLD]) and (r[self.XLI] < r[self.XLU]) and (r[self.DBB] < r[self.UUP]))
if exit:
self.bull = False
self.outday = self.count
if self.count >= self.outday + wait_days:
self.bull = True
self.count += 1
if not self.bull:
for sec in self.ASSETS:
self.wt[sec] = LEV if sec is self.BND else 0
self.trade()
elif self.bull:
for sec in self.ASSETS:
self.wt[sec] = LEV if sec is self.STK else 0
self.trade()
def trade(self):
for sec, weight in self.wt.items():
if weight == 0 and self.Portfolio[sec].IsLong:
self.Liquidate(sec)
cond1 = weight == 0 and self.Portfolio[sec].IsLong
cond2 = weight > 0 and not self.Portfolio[sec].Invested
if cond1 or cond2:
self.SetHoldings(sec, weight)
def OnEndOfDay(self):
mkt_price = self.Securities[self.MKT].Close
self.mkt.append(mkt_price)
mkt_perf = self.mkt[-1] / self.mkt[0] * self.cap
self.Plot('Strategy Equity', 'SPY', mkt_perf)
account_leverage = self.Portfolio.TotalHoldingsValue / self.Portfolio.TotalPortfolioValue
self.Plot('Holdings', 'leverage', round(account_leverage, 1))
for sec, weight in self.wt.items():
self.real_wt[sec] = round(self.ActiveSecurities[sec].Holdings.Quantity * self.Securities[sec].Price / self.Portfolio.TotalPortfolioValue,4)
self.Plot('Holdings', self.Securities[sec].Symbol, round(self.real_wt[sec], 3))
Nguyen Huu Quan
I got similar error. In my case it's a long list
Runtime Error: 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 VIAC XA367FHRKACL, LYFT X380ZYOECX2D, CRWD X59VIZ423I3P,\n CHTR UPXX4G43SIN9, CMG TFNWQ96IBZ39, UBER X4DDRW1HKLT1,\n V U12VRGLO8PR9, VEEV VKSLDWQI3JOL, LMT R735QTJ8XC9X,\n DPZ T0AJHPLNBYW5, NFLX SEWJWLJNHZDX, MAR R9GTMYCXKZ8L,\n SQ W5OUXC7GJYAT, MO R735QTJ8XC9X, PAYC VPQTATY1Y3QD,\n CVS R735QTJ8XC9X, LUV R735QTJ8XC9X, FISV R735QTJ8XC9X,\n SNAP WIINL6RMFSKL, DRI R735QTJ8XC9X, TWLO WBMIXML35O4L,\n SBUX R735QTJ8XC9X, FDX R735QTJ8XC9X, GOOG T1AZ164W5VTX,\n GOOCV VP83T1ZUHROL, ORCL R735QTJ8XC9X, UNP R735QTJ8XC9X,\n AMT RBASL7V8PIZP, NEM R735QTJ8XC9X, NOW V7TVHLCHTXLX,\n LULU TUPGLODCEXD1, SBAC RLL0ODEPH7C5, CME SKAIZ0LWKM05,\n UPS RPLRE46IQ3XH, HRS R735QTJ8XC9X, LRCX R735QTJ8XC9X,\n TXN R735QTJ8XC9X, FB V6OIPNZEM8V9, PSX V67S42RJBF51,\n NOC R735QTJ8XC9X, ISRG RVIFYB6Z92LH, VFC R735QTJ8XC9X,\n ETE TFVSB03UY0DH, AVGO UEW4IOBWVPT1, UAUA TFUSUWQOV97P,\n RH VB9XTDFRGZTX, DLTR R735QTJ8XC9X, MCD R735QTJ8XC9X,\n DAL TSAT9G6HOJ8L, CHV R735QTJ8XC9X, ROKU WO9FGTL2I89X,\n GILD R735QTJ8XC9X, COST R735QTJ8XC9X, MPC UXVGRK71ZB6T,\n TJX R735QTJ8XC9X, MHP R735QTJ8XC9X, IBM R735QTJ8XC9X,\n ALGN S1PYFJ8AMC11, D R735QTJ8XC9X, ATVI R735QTJ8XC9X,\n CSCO R735QTJ8XC9X, EBAY REC37LGZ79T1, AMAT R735QTJ8XC9X,\n XLNX R735QTJ8XC9X, ADBE R735QTJ8XC9X, MCHP R735QTJ8XC9X,\n NKE R735QTJ8XC9X, SYK R735QTJ8XC9X, NVDA RHM8UTD8DT2D,\n MU R735QTJ8XC9X, BDX R735QTJ8XC9X, TMO R735QTJ8XC9X,\n VRTX R735QTJ8XC9X, ULTA TX34HT712KBP, BEL R735QTJ8XC9X,\n TWTR VLE97YS7S57P, MMM R735QTJ8XC9X, WMT R735QTJ8XC9X,\n ROST R735QTJ8XC9X, HD R735QTJ8XC9X, MA TIX2XDPLFR6T,\n INTC R735QTJ8XC9X, WAG R735QTJ8XC9X, EW RTKJ7O7ZTI79,\n EQIXD SKZ55CR4HH0L, HAL R735QTJ8XC9X, DG UHLLRXJMGEP1,\n P R735QTJ8XC9X, PE R735QTJ8XC9X, WMB R735QTJ8XC9X,\n TSLA UNU3P8Y3WFAD, BSX R735QTJ8XC9X, F R735QTJ8XC9X,\n AMZN R735QTJ8XC9X, NSC R735QTJ8XC9X, CBRNA R735QTJ8XC9X,\n KMI UU1M6GZF9OPX, FCX R735QTJ8XC9X, CAT R735QTJ8XC9X,\n WDC R735QTJ8XC9X key exist in the collection and/or that collection is not empty.
at _validate_read_indexer
key=key in indexing.py:line 1175
Strongs
Nguyen Huu Quan The strange thing is until a few hours ago everything worked. The live algorithm with the same code continues to work.
We await a response from the quantconnect team.
Spacetime
suggestion: check history if it is null/empty before accessing, for example the close price. there should be examples out there on the forum.
Spacetime
hmm… there is a check being done on history… hmm… maybe try the debug function to go through the code… could be mkt_price
Strongs
spacetime Do you know why the code worked for months and suddenly no longer today? Indeed, why does the same code in live continue to work?
Spacetime
Strongs , I would not know the cause of it as such. Will have to conduct root cause analysis. If the same code was working for backtesting and all of sudden stopped working for backtesting due to “key error” issue, then there could a possibility of a data corruption. Maybe someone from support staff can explain/ look into. But, regardless, accessing any fields such as, “close” should be checked before using it.
Spacetime
Are you running this algorithm live with broker's data or quantconnect's data?
Strongs
spacetime with quantconnect's data
Spacetime
Strongs , let's just wait on what the support team or someone more knowledgeable with these types of problems has to say.
Colton Sellers
Hey guys, looking into this issue currently. We launched a refactor to our Pandas mapper yesterday that brought some significant performance improvements but it looks like we may have missed a case in our testing.
Thank you for reporting, and I will update you ASAP as I move forward 👍
Colton
Spacetime
Colton, Thanks!
Colton Sellers
No problem! Got a fix working in testing. Adding an additional unit test to Lean to ensure this type of indexing bug doesn't happen again.
Will post a link to the PR here shortly, and get it merged. 😁
Jared Broad
Morning all, sorry for the issues. We've been wrestling some things this morning.
The research index issue here is that we shipped a change to how we index in Pandas yesterday which was 40% faster but we've discovered a specific index that was missing. We've got a fix for its building now and it'll fix this issue for people.
We passed all unit and regression tests before pushing this new index update but we missed one. We've updated the tests to cover this new case and it won't happen again.
Sorry for the inconvenience, we strive for 100% backward compatibility at all times so it's disappointing when we miss the mark. It'll be back to normal within the next 2 hours.
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.
Nguyen Huu Quan
Thanks for the quick turnaround. Great to hear that the platform is getting faster :)
Spacetime
Jared and the team,
I appreciate all the hard work provided towards Quantconnect.
Thank you looking into this issue and finding the cause of it.
Colton Sellers
Hey guys, just an update.
Have a PR open addressing the issue here. Once we are all clear on our test set and quick review will be merged. 😁
Thanks again for your reports and patience, helps our team a lot.
Colton
Colton Sellers
Again, thank you for your patience! The fix has been deployed.
Strongs
Colton Sellers Has been solved? Because I still don't like it
Colton Sellers
Hey Strongs, the patch has been merged and from my testing your algorithm is no longer crashing.
Please ensure that you are running on the latest version of Lean (v12492) by selecting the version in your project settings. You can see the version your last build was compiled for in the console like so:
I'm attaching a backtest for your original algorithm above. If any issues persist please let us know.
Thanks!
Strongs
Colton Sellers Jared Broad, It works perfectly again, thanks for fixing guys
Strongs
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!