Back

Need help with runtime error

Hi, I am new to this platform and I am trying to implement universe equity selection that will filter equity with gap down and 50 EMA above 200 EMA but i can't resolve the runtime error of my code.

using System.Collections.Concurrent;
namespace QuantConnect.Algorithm.CSharp
{
public class QuantumVentralAutosequencers : QCAlgorithm
{

private readonly ConcurrentDictionary<Symbol, SelectionData> averages = new ConcurrentDictionary<Symbol, SelectionData>();
private RollingWindow<TradeBar> window;

public override void Initialize()
{
SetStartDate(2010, 1, 1);
SetEndDate(2019, 4, 1);
SetCash(100000);
AddUniverse(CoarseSelectionFilter);
UniverseSettings.Resolution = Resolution.Minute;

}

public IEnumerable<Symbol> CoarseSelectionFilter(IEnumerable<CoarseFundamental> universe)
{
var selected = new List<Symbol>();
universe = universe
.Where(x => x.Price > 10)
.OrderByDescending(x => x.DollarVolume).Take(10);

foreach (var coarse in universe)
{
string symbol = coarse.Symbol;

if (!averages.ContainsKey(symbol))
{
//1. Call history to get an array of 200 days of history data
var history = History(symbol, 200, Resolution.Daily);

//2. Adjust SelectionData to pass in the history result
averages[symbol] = new SelectionData(history);
}
averages[symbol].Update(Time, coarse.AdjustedPrice);

window = new RollingWindow<TradeBar>(2);
window.Add(CurrentSlice[symbol]);


decimal alpha = window[0].Open - window[1].Close;
decimal gapSize = 1/100*(window[1].High - window[1].Low);

if (averages[symbol].IsReady() && averages[symbol].Fast > averages[symbol].Slow)
{
if (alpha < -1*gapSize){
selected.Add(coarse.Symbol);
}
}
}

return selected.Take(10);
}
public override void OnSecuritiesChanged(SecurityChanges changes)
{
foreach (var security in changes.RemovedSecurities)
{
if (security.Invested)
{
Liquidate(security.Symbol);
}
}

foreach (var security in changes.AddedSecurities)
{
SetHoldings(security.Symbol, 0.10m);
}
}
}

public partial class SelectionData
{
public readonly ExponentialMovingAverage Fast;
public readonly ExponentialMovingAverage Slow;
public bool IsReady() {return Slow.IsReady && Fast.IsReady;}

//3. Update the constructor to accept an IEnumerable<TradeBar> history parameter
public SelectionData(IEnumerable<TradeBar> history)
{
Fast = new ExponentialMovingAverage(50);
Slow = new ExponentialMovingAverage(200);

//4. Loop over history data and pass the bar.EndTime and bar.Close values to Update()
foreach(var bar in history)
{
Update(bar.EndTime, bar.Close);
}
}

public bool Update(DateTime time, decimal value)
{
Slow.Update(time, value);
Fast.Update(time, value);
return IsReady();
}
}
}

 

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.


Hey Darylkouk,

You are getting a

Runtime Error: Object reference not set to an instance of an object

because CurrentSlice[symbol]) is returning null. Since within universe selection, no symbols have been added to our data subscriptions yet, there is no trade bar data for that symbol. Instead, you should initialize the window with historical data in SelectionData.

I reorganized your code by moving the RollingWindow into SelectionData and then updating it with historical data. We continue to update the windows with new bars for our symbols in OnData. If a symbol is removed from our universe, we also remove it from our dictionary of SelectionData.

1

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