Reference not set to an instance of an object

Hello!

I have a short code as test of an indicator based on 5 minutes time frame and I want this indicator to be ploted on a tick chart.

When I plot this indicator on the chart, the plateforme freezees and I obtaine an error message: Object reference not set to an instance of an object.

Usually, the such message comes, that means there is a variable that is not initialized.
But I am not seeing a variable in my code that is not initialized. I would really appreciate if someone could point out where I am mistaking.

I have the following code:

private EMA EMA1;
…
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 5);
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(BarsArray[1], 21);
}

protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1 || CurrentBars[1] < 20)
return;

if(BarsInProgress == 0)
Value[0] = EMA1[0];
}

Thanks in advance!

1 Like

You’re crossing over logic. Your EMA is on the 5 minute series so it updates every 5 minutes, but you’re assigning it from the other series which can start running as soon as there’s 1 bar on the chart. So if there’s 1 bar on the primary series, there perhaps isn’t any bar on the secondary.

I think that’s it.

Many thanks Several for your reply!

Event after modifying the following condition
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 500 || CurrentBars[1] < 500)
return;

if(BarsInProgress == 0)
Value[0] = EMA1[0];
}

the same error essage appears again and the plateforme freezes.

Does it work if you remove this line:

Value[0] = EMA1[0];

When I remove the line
Value[0] = EMA1[0];
and attach the indicator on the chart, as expected, there is no line on the chart.
The plateforme does not freeze again.

How are you adding your plot?

	if (State == State.SetDefaults)
		{
		AddPlot(Brushes.Orange, "Plot1");
		}
		else if (State == State.Configure)
		{
			AddDataSeries(Data.BarsPeriodType.Minute, 5);
		}
		else if (State== State.DataLoaded)
		{
			EMA1 = EMA(BarsArray[1], 21);
		}
	}

	protected override void OnBarUpdate()
	{
	
		if (CurrentBars[1] < 22)
			return;
		
		if (BarsInProgress == 0 && CurrentBars[0] > 0)
    		Value[0] = EMA1[0];

	}

works for me

1 Like

Many thanks Several for your help!
I have the line now on the chart with your modification!

...&& CurrentBars[0] > 0 was very important. Thanks again!

But the line on the chart is weird. It looks like stairs!!! Is that so OK?

It’s like that because you have 2 series with different intervals.

If you had a 1m primary and a 5m secondary, drawing the line as you are. You’d would see only a change in value every 5 bars so you would see flat line > jump > flat line > jump.

Several is completely spot on about the step effect but I want to add a crucial architectural note here, a lot of retail guys see that staircase look and immediately try to smooth it out using interpolation functions. Do not do that. If you smooth a 5-minute EMA on a 1-minute chart you are essentially leaking future data into your current bar because the intermediate smoothed values do not actually exist until that higher timeframe candle officially closes. The staircase is mathematically correct and represents the true static state of the higher timeframe, if you ever plan to hook this indicator up to an automated execution strategy later you absolutely need those rigid stairs to prevent the backtester from hallucinating fills that never happened in live market conditions