How can I reference to the i-1 in real time quote?

Because real time data the data is keep updating, the BarCount is always the same, how can I reference to the i-1 in real time quote?

Screenshot

I expect the debug output at # signal else after1, the Pretrail1 and Pretrail2 are not 0.

// ### TRADING LOGICS ###
PreRBC2[0] = 0; PreRBC1[0] = 0; // RBC[i-2] = RBC[i-1]
Pretrail2[0] = 0; Pretrail1[0] = 0; // trailArry[i-2] = trailArray[i-1]
for( i = 1; i < BarCount; i++ )
{	
	PreRBC2[i] = PreRBC2[i-1];
	PreRBC1[i] = PreRBC1[i-1];
	Pretrail2[i] = Pretrail2[i-1];
	Pretrail1[i] = Pretrail1[i-1];

	if ( i < BarCount - 1 )  // for plotting the chart even the condition nextbar != 1
	{
		Buy[i] = RBC[i] > trailArray[i-1] AND RBC[i-1] < trailArray[i-2];
		Cover[i] = Buy[i];
		Short[i] = RBC[i] < trailArray[i-1] AND RBC[i-1] > trailArray[i-2];
		Sell[i] = Short[i];
	}
	else
	{
		 if ( nextbar[i] == 1 )
		 {
			if(DebugOn) _TRACE("#, signal else after1, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);
			if ( PreRBC1[i] != 0 AND Pretrail1[i] != 0 AND Pretrail2[i] != 0 )
			{
				Buy[i] = RBC[i] > Pretrail1[i] AND PreRBC1[i] < Pretrail2[i];
				Cover[i] = Buy[i];
				Short[i] = RBC[i] < Pretrail1[i] AND PreRBC1[i] > Pretrail2[i];
				Sell[i] = Short[i];
			}
			PreRBC2[i] = PreRBC1[i]; PreRBC1[i] = RBC[i];
			Pretrail2[i] = Pretrail1[i]; Pretrail1[i] = trailArray[i];
			if(DebugOn) _TRACE("#, signal else after2, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);
			if(DebugOn) _TRACE("#, signal else 1, Buy="+Buy[i]+" Cover="+Cover[i]+" Short="+Short[i]+" Sell="+Sell[i]);
		}
	}
}

Thanks in advance.

hello @ngterry at this moment this afl is little bit :heart_eyes: so i am not going to touch it.

Using the lookup() function can solve your problem.
search the forum also for examples.

I appreciate your reply.

I am sorry that I did not mention I am using tick data.

Moreover, the main issue is the following code does not work as expected in tick data:

PreRBC2[i] = PreRBC2[i-1];
PreRBC1[i] = PreRBC1[i-1];
Pretrail2[i] = Pretrail2[i-1];
Pretrail1[i] = Pretrail1[i-1];

Looks like you need to re-read the basics

https://www.amibroker.com/guide/h_understandafl.html

Thank you for your involvement; however, I don't understand what you mean. Did I miss something?

From the debug log, BarCount keeps at i=49999, RBC[i] keeps updating the tick price and other values should bring forward using code:
e.g. PreRBC1[i] = PreRBC[i-1]
but i keeps at 49999, does not change to 50000, then the values keep resetting.

How many bars did you set at Database settings ?
if you set 50000 bars, you get 0 to 49999
each new tick that arrives, the array is shifted

Yes, I set it to 50000. The array is shifted by updating the last bar e.g. RBC[49999]. Then, how can I refer to the previous bar? The previous bar also is RBC[49999].

Check out here
http://www.amibroker.com/guide/afl/ref.html

Thank you for your help. However, I don't think there is a difference between array ref and loops [i-1].

There is difference in speed.

I have modified the code to add _TRACE to have a more clearly picture:
Screenshot%20(19)

// ### TRADING LOGICS ###
PreRBC2[0] = 0; PreRBC1[0] = 0; // RBC[i-2] = RBC[i-1]
Pretrail2[0] = 0; Pretrail1[0] = 0; // trailArry[i-2] = trailArray[i-1]
for( i = 1; i < BarCount; i++ )
{	
	PreRBC2[i] = PreRBC2[i-1];
	PreRBC1[i] = PreRBC1[i-1];
	Pretrail2[i] = Pretrail2[i-1];
	Pretrail1[i] = Pretrail1[i-1];

	if ( i < BarCount - 1 )
	{
		Buy[i] = RBC[i] > trailArray[i-1] AND RBC[i-1] < trailArray[i-2];
		Cover[i] = Buy[i];
		Short[i] = RBC[i] < trailArray[i-1] AND RBC[i-1] > trailArray[i-2];
		Sell[i] = Short[i];
	}
	else
	{
		if(DebugOn) _TRACE("#, signal else before1, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);
		if(DebugOn) _TRACE("#, signal else before2, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);

		if ( nextbar[i] == 1 )
		{
			if(DebugOn) _TRACE("#, signal else after1, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);
			if ( PreRBC1[i] != 0 AND Pretrail1[i] != 0 AND Pretrail2[i] != 0 )
			{
				Buy[i] = RBC[i] > Pretrail1[i] AND PreRBC1[i] < Pretrail2[i];
				Cover[i] = Buy[i];
				Short[i] = RBC[i] < Pretrail1[i] AND PreRBC1[i] > Pretrail2[i];
				Sell[i] = Short[i];
			}

			PreRBC2[i] = PreRBC1[i]; PreRBC1[i] = RBC[i];
			Pretrail2[i] = Pretrail1[i]; Pretrail1[i] = trailArray[i];

			if(DebugOn) _TRACE("#, signal else after2, i="+i+" RBC[i]="+RBC[i]+" trailArray[i]="+trailArray[i]+" Pretrail1[i]="+Pretrail1[i]+" PreRBC1[i]="+PreRBC1[i]+" Pretrail2[i]="+Pretrail2[i]);
			if(DebugOn) _TRACE("#, signal else 1, Buy="+Buy[i]+" Cover="+Cover[i]+" Short="+Short[i]+" Sell="+Sell[i]);
		}
	}
}

I don't quite understand where you see the problem.

There is absolutely NO difference between real time or non real-time. Last bar value is array[ BarCount - 1 ] (or LastValue(array)). The value of the bar before last is array[ BarCount - 2 ] (or LastValue( Ref( array, -1 ) ).

That is all that is to it. It works the same regardless of data source or RT or not RT.