Hi Everyone !
I get this runtime error :Object reference not set to an instance of an object
Is appear at a certain date (March 2012), if i skip the date the error disappear. Is it possible that you have a Data bug or other ?
I put in the code many check != null and !=0, but nothing to do.
System.NullReferenceException: Object reference not set to an instance of an object
at Test.TestAlgo.OnData (QuantConnect.Data.Slice slice) [0x00165] in :0
at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job,
QuantConnect.Interfaces.IAlgorithm algorithm,
QuantConnect.Lean.Engine.DataFeeds.ISynchronizer synchronizer,
QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions,
QuantConnect.Lean.Engine.Results.IResultHandler results,
QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime,
QuantConnect.Lean.Engine.Server.ILeanManager leanManager,
QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas,
System.Threading.CancellationToken token) [0x0139a] in Lean.Engine.Alpha.IAlphaHandler alphas,
System.Threading.CancellationToken token) [0x0139a] in :0
Algo :
namespace Test
{
public class TestAlgo : QCAlgorithm
{
public RollingWindow<decimal> BidPriceSP500 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> AskPriceSP500 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> VolumeSP500 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> BidPriceDOW30 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> AskPriceDOW30 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> VolumeDOW30 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> BidPriceNSQ100 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> AskPriceNSQ100 = new RollingWindow<decimal>(4);
public RollingWindow<decimal> VolumeNSQ100 = new RollingWindow<decimal>(4);
public const string TestalgoSP500 = Futures.Indices.SP500EMini;
public Symbol SP500 = QuantConnect.Symbol.Create(TestalgoSP500, SecurityType.Future, Market.USA);
public const string TestalgoDOW30 = Futures.Indices.Dow30EMini;
public Symbol DOW30 = QuantConnect.Symbol.Create(TestalgoDOW30, SecurityType.Future, Market.USA);
public const string TestalgoNSQ100 = Futures.Indices.NASDAQ100EMini;
public Symbol NSQ100 = QuantConnect.Symbol.Create(TestalgoNSQ100, SecurityType.Future, Market.USA);
public override void Initialize()
{
SetStartDate(2012, 01, 01);
SetEndDate(DateTime.Now);
SetCash(1000000);
var SP500 = AddFuture(TestalgoSP500, Resolution.Minute);
SP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
var DOW30 = AddFuture(TestalgoDOW30, Resolution.Minute);
DOW30.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
var NSQ100 = AddFuture(TestalgoNSQ100, Resolution.Minute);
NSQ100.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
var benchmark = AddEquity("SPY");
SetBenchmark(benchmark.Symbol);
Schedule.Event().Every(DayOfWeek.Friday).At(15, 59).Run(() =>
Schedule.Event().Every(DayOfWeek.Friday).At(15, 60).Run(() =>
Schedule.Event().Every(DayOfWeek.Friday).At(16, 00).Run(() =>
{
foreach (var contract in Portfolio.Values)
{
if (contract.HoldStock)
{
MarketOrder(contract.Symbol, -contract.Quantity, tag: "Liquidate Market Close");
}
}
})));
}
public override void OnData(Slice slice)
{
if (!Portfolio.Invested)
{
foreach (var chain in slice.FutureChains)
{
var contractSP500 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "ES").ThenBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(90)
select futuresContract).FirstOrDefault();
var contractDOW30 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "YM").ThenBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(90)
select futuresContract).FirstOrDefault();
var contractNSQ100 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "NQ").ThenBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(90)
select futuresContract).FirstOrDefault();
BidPriceSP500.Add(contractSP500.BidPrice);
AskPriceSP500.Add(contractSP500.AskPrice);
VolumeSP500.Add(contractSP500.Volume);
BidPriceDOW30.Add(contractDOW30.BidPrice);
AskPriceDOW30.Add(contractDOW30.AskPrice);
VolumeDOW30.Add(contractDOW30.Volume);
BidPriceNSQ100.Add(contractNSQ100.BidPrice);
AskPriceNSQ100.Add(contractNSQ100.AskPrice);
VolumeNSQ100.Add(contractNSQ100.Volume);
var LongSP500 = BidPriceSP500[0] > AskPriceSP500[0];
var LongDOW30 = BidPriceDOW30[0] > AskPriceDOW30[0];
var LongNSQ100 = BidPriceNSQ100[0] > AskPriceNSQ100[0];
// This check is needed to prevent errors from accessing entries that do not exist
if (!BidPriceSP500.IsReady || !AskPriceSP500.IsReady || !VolumeSP500.IsReady || BidPriceSP500 != null || AskPriceSP500 != null || VolumeSP500 != null || BidPriceSP500[0] != 0 || AskPriceSP500[0] != 0 || VolumeSP500[0] != 0)
continue;
Debug("BidPriceSP500 :" + BidPriceSP500[0]);
Debug("AskPriceSP500 :" + AskPriceSP500[0]);
if (LongSP500)
if (contractSP500.Symbol.StartsWith("ES") || contractSP500 != null || BidPriceSP500[0] != null || AskPriceSP500[0] != null || VolumeSP500[0] != null || BidPriceSP500[0] != 0 || AskPriceSP500[0] != 0 || VolumeSP500[0] != 0)
MarketOrder(contractSP500.Symbol, 10);
Debug("contract en cours : " + chain.Value.Symbol);
Debug("contract en cours : " + contractSP500);
if (!BidPriceDOW30.IsReady || !AskPriceDOW30.IsReady || !VolumeDOW30.IsReady || BidPriceDOW30[0] != null || AskPriceDOW30[0] != null || VolumeDOW30[0] != null || BidPriceDOW30[0] != 0 || AskPriceDOW30[0] != 0 || VolumeDOW30[0] != 0)
continue;
Debug("BidPriceDOW30 :" + BidPriceDOW30[0]);
Debug("AskPriceDOW30 :" + AskPriceDOW30[0]);
if (LongDOW30)
if (contractDOW30.Symbol.StartsWith("YM") || contractDOW30 != null || BidPriceDOW30[0] != null || AskPriceDOW30[0] != null || VolumeDOW30[0] != null | BidPriceDOW30[0] != 0 || AskPriceDOW30[0] != 0 || VolumeDOW30[0] != 0)
MarketOrder(contractDOW30.Symbol, 10);
Debug("contract en cours : " + chain.Value.Symbol);
Debug("contract en cours : " + contractDOW30);
if (!BidPriceNSQ100.IsReady || !AskPriceNSQ100.IsReady || !VolumeNSQ100.IsReady || BidPriceNSQ100[0] != null || AskPriceNSQ100[0] != null || VolumeNSQ100[0] != null || BidPriceNSQ100[0] != 0 || AskPriceNSQ100[0] != 0 || VolumeNSQ100[0] != 0)
continue;
Debug("BidPriceNSQ100 :" + BidPriceNSQ100[0]);
Debug("AskPriceNSQ100 :" + AskPriceNSQ100[0]);
if (LongNSQ100)
if (contractNSQ100.Symbol.StartsWith("NQ") || contractNSQ100 != null || BidPriceNSQ100[0] != null || AskPriceNSQ100[0] != null || VolumeNSQ100[0] != null || contractNSQ100 != null || BidPriceNSQ100[0] != 0 || AskPriceNSQ100[0] != 0 || VolumeNSQ100[0] != 0)
MarketOrder(contractNSQ100.Symbol, 10);
Debug("contract en cours : " + chain.Value.Symbol);
Debug("contract en cours : " + contractNSQ100);
}
}}
}
}
Gmamuze Cht
It seems some check "!= 0" prevent to place order.
Runtime error already appear.
using System; using System.Collections.Generic; using QuantConnect.Data; using QuantConnect.Data.Market; using System.Linq; using QuantConnect.Interfaces; using QuantConnect.Indicators; using QuantConnect.Securities; using QuantConnect.Orders; using QuantConnect.Data.Consolidators; namespace Test { public class TestAlgo : QCAlgorithm { public RollingWindow<decimal> BidPriceSP500 = new RollingWindow<decimal>(4); public RollingWindow<decimal> AskPriceSP500 = new RollingWindow<decimal>(4); public RollingWindow<decimal> VolumeSP500 = new RollingWindow<decimal>(4); public RollingWindow<decimal> BidPriceDOW30 = new RollingWindow<decimal>(4); public RollingWindow<decimal> AskPriceDOW30 = new RollingWindow<decimal>(4); public RollingWindow<decimal> VolumeDOW30 = new RollingWindow<decimal>(4); public RollingWindow<decimal> BidPriceNSQ100 = new RollingWindow<decimal>(4); public RollingWindow<decimal> AskPriceNSQ100 = new RollingWindow<decimal>(4); public RollingWindow<decimal> VolumeNSQ100 = new RollingWindow<decimal>(4); public const string TestalgoSP500 = Futures.Indices.SP500EMini; public Symbol SP500 = QuantConnect.Symbol.Create(TestalgoSP500, SecurityType.Future, Market.USA); public const string TestalgoDOW30 = Futures.Indices.Dow30EMini; public Symbol DOW30 = QuantConnect.Symbol.Create(TestalgoDOW30, SecurityType.Future, Market.USA); public const string TestalgoNSQ100 = Futures.Indices.NASDAQ100EMini; public Symbol NSQ100 = QuantConnect.Symbol.Create(TestalgoNSQ100, SecurityType.Future, Market.USA); public override void Initialize() { SetStartDate(2010, 01, 01); SetEndDate(DateTime.Now); SetCash(1000000); var SP500 = AddFuture(TestalgoSP500, Resolution.Minute); SP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182)); var DOW30 = AddFuture(TestalgoDOW30, Resolution.Minute); DOW30.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182)); var NSQ100 = AddFuture(TestalgoNSQ100, Resolution.Minute); NSQ100.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182)); var benchmark = AddEquity("SPY"); SetBenchmark(benchmark.Symbol); Schedule.Event().Every(DayOfWeek.Friday).At(15, 59).Run(() => Schedule.Event().Every(DayOfWeek.Friday).At(15, 60).Run(() => Schedule.Event().Every(DayOfWeek.Friday).At(16, 00).Run(() => { foreach (var contract in Portfolio.Values) { if (contract.HoldStock) { MarketOrder(contract.Symbol, -contract.Quantity, tag: "Liquidate Market Close"); } } }))); } public override void OnData(Slice slice) { if (!Portfolio.Invested) { foreach (var chain in slice.FutureChains) { var contractSP500 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "ES").ThenBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault(); var contractDOW30 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "YM").ThenBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault(); var contractNSQ100 = (from futuresContract in chain.Value.OrderBy(x => x.Symbol == "NQ").ThenBy(x => x.Expiry) where futuresContract.Expiry > Time.Date.AddDays(90) select futuresContract).FirstOrDefault(); BidPriceSP500.Add(contractSP500.BidPrice); AskPriceSP500.Add(contractSP500.AskPrice); VolumeSP500.Add(contractSP500.Volume); BidPriceDOW30.Add(contractDOW30.BidPrice); AskPriceDOW30.Add(contractDOW30.AskPrice); VolumeDOW30.Add(contractDOW30.Volume); BidPriceNSQ100.Add(contractNSQ100.BidPrice); AskPriceNSQ100.Add(contractNSQ100.AskPrice); VolumeNSQ100.Add(contractNSQ100.Volume); var LongSP500 = BidPriceSP500[0] > AskPriceSP500[0]; var LongDOW30 = BidPriceDOW30[0] > AskPriceDOW30[0]; var LongNSQ100 = BidPriceNSQ100[0] > AskPriceNSQ100[0]; // This check is needed to prevent errors from accessing entries that do not exist if (!BidPriceSP500.IsReady || !AskPriceSP500.IsReady || !VolumeSP500.IsReady) continue; Debug("BidPriceSP500 :" + BidPriceSP500[0]); Debug("AskPriceSP500 :" + AskPriceSP500[0]); if (LongSP500) if (contractSP500.Symbol.StartsWith("ES") || contractSP500 != null) MarketOrder(contractSP500.Symbol, 10); Debug("contract en cours : " + chain.Value.Symbol); Debug("contract en cours : " + contractSP500); if (!BidPriceDOW30.IsReady || !AskPriceDOW30.IsReady || !VolumeDOW30.IsReady) continue; Debug("BidPriceDOW30 :" + BidPriceDOW30[0]); Debug("AskPriceDOW30 :" + AskPriceDOW30[0]); if (LongDOW30) if (contractDOW30.Symbol.StartsWith("YM") || contractDOW30 != null) MarketOrder(contractDOW30.Symbol, 10); Debug("contract en cours : " + chain.Value.Symbol); Debug("contract en cours : " + contractDOW30); if (!BidPriceNSQ100.IsReady || !AskPriceNSQ100.IsReady || !VolumeNSQ100.IsReady) continue; Debug("BidPriceNSQ100 :" + BidPriceNSQ100[0]); Debug("AskPriceNSQ100 :" + AskPriceNSQ100[0]); if (LongNSQ100) if (contractNSQ100.Symbol.StartsWith("NQ") || contractNSQ100 != null) MarketOrder(contractNSQ100.Symbol, 10); Debug("contract en cours : " + chain.Value.Symbol); Debug("contract en cours : " + contractNSQ100); } }} } }
Gmamuze Cht
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!