How to execute the code if j[ i ] != j[ i - 1 ]?

I would like to execute the trading logic if j[ i ] != j[ i - 1 ]. How to execute the code if j[ i ] != j[ i - 1 ]? From the log, the code insides if ( j[ i ] != j[ i - 1 ] ) is not executed because j[ i ] == j[ i - 1 ]. How do I solve it? Thank you!

Screenshot

// ### TRADING LOGICS ###

for( i = 1; i < BarCount; i++ )
{
	if ( i < BarCount - 1 )
	{
		Buy[i] = RBC[i] > trailArray[i-1];
		Cover[i] = Buy[i];
		Short[i] = RBC[i] < trailArray[i-1];
		Sell[i] = Short[i];
	}
	else
	{
		 if ( j[i] != j[i-1] )
		 {
			Buy[i] = RBC[i] > trailArray[i-1];
			Cover[i] = Buy[i];
			Short[i] = RBC[i] < trailArray[i-1];
			Sell[i] = Short[i];
			if(DebugOn) _TRACE("#, signal else trigger, BarCount="+BarCount+" i="+i+" j="+j+" j[-1]="+j[i-1]+" Close="+Close[i]+" RBC="+RBC[i]+" RBC[-1]="+RBC[i-1]+" trailArray="+trailArray[i]+" trailArray[-1]="+trailArray[i-1]);
			if(DebugOn) _TRACE("#, signal else indicator="+iif(RBC[i] > trailArray[i-1], 1, iif(RBC[i] < trailArray[i-1], -1, 0 ) ) );
			if(DebugOn) _TRACE("#, signal else 1, Buy="+Buy[i]+" Cover="+Cover[i]+" Short="+Short[i]+" Sell="+Sell[i]);
		}
		if(DebugOn) _TRACE("#, signal else, BarCount="+BarCount+" i="+i+" j="+j[i]+" j[-1]="+j[i-1]+" Close="+Close[i]+" RBC="+RBC[i]+" RBC[-1]="+RBC[i-1]+" trailArray="+trailArray[i]+" trailArray[-1]="+trailArray[i-1]);
	}
}

@ngterry without seeing the part of the code where you assign the j variable is difficult to give you a precise answer, but in any case, I would look closely at the lines where you define and assign values to such j. variable (is it an array?)

If you are still lost, please, post the full code (in a way that is executable without any error), so other users can help you with a more accurate answer.

If i is a nested loop and j is set to 0. Would make sense why i-1 == j[i] are the same same since i starts at 1. But like below mentioned no one knows without j being defined.

Also keep in mind that if you are looping through all the bars it is likely that you could have written your code much more efficiently by taking advantage of AmiBroker's array processing capabilities. For example, if we wanted to find instances where today's closing price is equal to yesterday's closing price, we could write:

isSameClose = C == Ref(C,-1);

This is equivalent to but much faster than:

for (bar=1; bar < BarCount; ++bar)
{
   isSameClose[bar] = C[bar] == C[bar-1]; 
}
2 Likes