PDA

View Full Version : Most Recent Occurence


NinjaTrader_Ray
01-18-2007, 10:30 AM
To check for the most recent occurence of a particular condition you can write something like:

private int occuredBarsAgo = -1;

for (int barsAgo = 0; barsAgo < CurrentBar; barsAgo++)
if (SMA(10)[barsAgo] > SMA(20)[barsAgo])
{
occuredBarsAgo = barsAgo;
break;
}


Ray

ThePatientOne
02-19-2007, 08:22 AM
Wouldn't this code snippet find the FIRST occurrence of the test case ... not the most recent. I believe you'd want to go backwards or count down from Current bar to 0 to find the most recent occurrence.

Please advise if I am mistaken.

NinjaTrader_Ray
02-19-2007, 08:29 AM
You are mistaken.

Do not confuse "CurrentBar" with an index value that represents the number of bars ago.

Ray

ThePatientOne
02-19-2007, 08:34 AM
Sorry ... you are correct. I misread how the index was being used. I thought the code was just cycling through the bars on the chart starting at index 0.