Back

Runtime Error: Cannot perform runtime binding on a null reference

Hi!

I'm having trouble with changing the date on my backtest. It works fine when the date is > 2018. But when i change it i get the message "Runtime Error: Cannot perform runtime binding on a null reference". Do anyone know how to fix it?

/Hampus

Update Backtest








Hi Hampus,

You should check if the slice object contains the symbol before accessing the price by symbol index. In addition, instead of using data[symbol], use data.Bars[symbol] for trade bar and use data.QuoteBars[symbol] for trade bar data.

if (data.ContainsKey(symbol))
{
high.Add(data.Bars[symbol].High);
low.Add(data.Bars[symbol].Low);
close.Add(data.Bars[symbol].Close);
open.Add(data.Bars[symbol].Open);
}
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.


Thanks. It still dosent work, this time i get the message "Runtime Error: '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")"

0

The problem with using data.ContainsKey(symbol) is that it's possible for the Dictionary to contain the key "SPY", but the associated value be null. Which means data.ContainsKey(symbol) returns true, but it still wrecks your algo. You can see this yourself if you do:

if (data[symbol] == null)
{
Debug(data.Keys[0]);
Debug(data.Values[0].ToString());
}

It'll print "SPY", because it finds that key, but then die because there's only a null value for it.

If you start OnData like this instead:

public override void OnData(Slice data)
{
if (data[symbol] == null)
{
return;
}

 

that appears to do what you want. I tried it myself, and the backtest runs fine, for several different date ranges.

0

Sorry about that guys. This issue was addressed in a PR early this morning: 

https://github.com/QuantConnect/Lean/pull/1734
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.


Works now, thank you so much!

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