Lastvalue(Close) vs Close_price[BarCount - 1]

Hi All,
I have been using the following statement for getting the last value of Close.

Close_price = Close;
current_close = Close_price[BarCount - 1];

I flip through all the daily charts as part of my daily review. Using the above statements cause Error 10 message to display momentarily(less than a second). I have since used this statement and all is well.

current_close = Lastvalue(Close);

I am using version 6.20.1. It seems like any code with array[BarCount - 1] is having this issue when flipping chart fast using arrow down key. This is just for sharing.

Your code has an error. You are not using boundary checking when accessing the array elements and your code will fail if BarCount is zero. LastValue won't fail because it performs boundary checking inside.

See:

In majority of cases your code will not be executed at all if BarCount is zero it might happen in certain situations when data source is plugin driven and data are changed dynamically or when filtering/padding is active.

1 Like

That what I thought. But the Error 10 message just appear momentarily and disappear. This happens during some tickers and repeatable. I have to press up and down arrow keys repeatedly in order to see the error message and the line number. I even do a printf for both BarCount -1 and Lastvalue and both show the same result message appear. I remove all BarCount - 1 from the chart AFL and the Error 10 goes away. But the IF fail to prevent a false from getting through i.e. I can hear sound and the text flashing some colour momentarily. It only happens on some tickers and it's repeatable.

Here is the code. I don't expect any solution and it's just for sharing.

	printf("Bar : %g\n",bars);
	check = current_close > LastValue(HHV_50MA) AND LastValue(MA_50) > LastValue(MA_200) AND current_close >= Min_price AND Lookup(BarIndex(),Lastest_datetime,0);
	printf("Check : %g\n", check);
	if (current_close > LastValue(HHV_50MA) AND LastValue(MA_50) > LastValue(MA_200) AND current_close >= Min_price AND Lookup(BarIndex(),Lastest_datetime,0)) {
	    if (LastValue(bars)<22) {
			GfxSetTextColor(colorRed);
			if (audio_alert == "ON") {
				PlaySound("C:\\Program Files\\AmiBroker\\Audio\\win98_ding.wav");
			}
		}
		else {
			if (LastValue(bars)<= 65) {
				GfxSetTextColor(colorBrightGreen);
				if (audio_alert == "ON") {
					PlaySound("C:\\Program Files\\AmiBroker\\Audio\\trigger.wav");
				}
			}
			else
				if (LastValue(bars) > 65){
					GfxSetTextColor(colorBlue);
					if (audio_alert == "ON") {
						PlaySound("C:\\Program Files\\AmiBroker\\Audio\\win98_chimes.wav");
					}
				}
		}
	}

In short, you should never access array without checking that index is valid.

You have to write

if( index >= 0 AND index < BarCount ) 
{
   array[ index ]
}

LastValue function is provided for sole purpose of making things easier and not requiring this check.

Hi Tomasz,
Indeed. This issue is related to BarCount. For tickers with less than 200 bars, Lastvalue() of MA(Close,200) will return a zero value. The following check fixed the problem. Thank you very much.

check = current_close > LastValue(HHV_50MA) AND IIf(BarCount>=200,LastValue(MA_50) > LastValue(MA_200),0) AND current_close >= Min_price AND Lookup(BarIndex(),Lastest_datetime,0);

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.