Back

Beginner question.

I'm trying to learn how to do the most basic python start on quantconnect, but I keep running into what seems to be a very basic error. Could anyone give a newcomer some support?

Thank you.

Code:

class UncoupledParticleEngine(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2015, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("SPY", Resolution.Minute) # self.AddEquity("SPY", Resolution.Minute)
self.rsi = self.RSI("SPY", 10, MovingAverageType.Simple, Resolution.Daily)
# set a warm-up period to initialize the indicator
self.SetWarmUp(timedelta(20))
# Warm-up the indicator with bar count
# self.SetWarmUp(10, Resolution.Daily)

def OnData(self, data):
## You can access the TradeBar dictionary in the slice object and then subset by symbol
## to get the TradeBar for SPY
tradeBars = data.Bars
spyTradeBar = tradeBars['SPY']
spyOpen = spyTradeBar.Open ## Open price
spyClose = spyTradeBar.Close ## Close price
# if not self.Portfolio.Invested:
# self.SetHoldings("SPY", 1)

Error:

94 | 18:11:18:
Runtime Error: KeyNotFoundException : 'SPY' wasn't found in the TradeBars object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY")
at QuantConnect.Data.Market.DataDictionary`1[T].get_Item (QuantConnect.Symbol symbol) [0x00023] in <b4ad34a4a52041c4accaeff6c8d015c6>:0
at QuantConnect.Data.Market.TradeBars.get_Item (QuantConnect.Symbol symbol) [0x00000] in <b4ad34a4a52041c4accaeff6c8d015c6>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at OnData in main.py:line 17
KeyNotFoundException : 'SPY' wasn't found in the TradeBars object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY")
at QuantConnect.Data.Market.DataDictionary`1[T].get_Item (QuantConnect.Symbol symbol) [0x00023] in <b4ad34a4a52041c4accaeff6c8d015c6>:0
at QuantConnect.Data.Market.TradeBars.get_Item (QuantConnect.Symbol symbol) [0x00000] in <b4ad34a4a52041c4accaeff6c8d015c6>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0 (Open Stacktrace)
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.


Hi there,

You should not assume that the data object necessarily contains data for all the securities you have subscribed to. It can sometimes be empty and it is thus good practice (necessary) to check its contents before using them.

One way is to check whether data contains the key SPY before trying to access it. As data (in live trading) comes indirectly from the broker, you should never assume its contents.

0

Hi Sterling,

Douglas's answer is spot-on! I'll add a few implementation details. To check if data contains the content, you can use:

if data.Bars.ContainsKey("SPY"):
spyTradeBar = data.Bars['SPY']

I've attached a backtest, and you can read the Debugging logs for more details.

I hope this helps.

Xin

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