concept: how to compare current bar condition with already established condition within last x bars, but only flag the first instance of a match

 
Conceptually, this seems quite easy. But, I'm struggling with getting it into MQL4 code. The screen shot below demonstrates the required logic.

1. Bar marked with Red X represents condition A (for this example, I'm using volume=xxxxx, it could be anything)
2. For the next x bars (here I use 5 bars), if condition B is found, mark it (marked with a purple x)
3. If the same purple X condition is met again within the 5 bar check, do NOT mark it

Should be easy, right?

How do I code this?

I assume there's a loop within a loop and the Red X condition gets set as a variable. It's just translating that to MQL4 code isn't delivering the expected results.
 
A quick clarification please:

Assume ConditionA occurred on previous bar (bar index 1) and also occurs on current bar (bar index 0). Also assume that ConditionB occurs on current bar (i.e. volume is less than previous bar). What's the desired result?
1. Criteria for both conditions has been met on current bar, display both X's.
2. ConditionA overrides check for ConditionB on current bar, display red X only.
3. ConditionB overrides check for ConditionA on current bar, display purple X only.
 
billworld:
Conceptually, this seems quite easy. But, I'm struggling with getting it into MQL4 code. The screen shot below demonstrates the required logic.

1. Bar marked with Red X represents condition A (for this example, I'm using volume=xxxxx, it could be anything)
2. For the next x bars (here I use 5 bars), if condition B is found, mark it (marked with a purple x)
3. If the same purple X condition is met again within the 5 bar check, do NOT mark it

Should be easy, right?

How do I code this?

I assume there's a loop within a loop and the Red X condition gets set as a variable. It's just translating that to MQL4 code isn't delivering the expected results.
You have to understand that in EA, the start() function is fired on every tick arrival and hence more like an event based language.
Loop within a loop in a traditional way of programming logic does not work here.
Setup two functions, one to set/reset A condition and check for the condition lifetime.
Another function to use the A condition, mark the B marker if condition is met, and reset A condition.

#define RESET -1
 
int start() {
    if( some new condition is created ) {
        ACondition( volume, bars );
    }
    BMarker();
}
 
int ACondition( int v=0, int b=0 ) {
    static int volume;
    static int bars;
    static datetime A;
 
    int barDistance = (Time[1]-A)/(60*Period());
    if( barDistance >= bars ) {
        volume = 0;
        bars   = 0;
    }
    if( v == RESET ) {
        A = 0;
    }
    else if( v > 0 ) {
        volume = v;
        bars = b;
        A = Time[1];
    }
    else {
        return(volume);
    }
}
 
int BMarker() {
    int v = ACondition();
    if( v > 0 && Volume() == v ) {
        Print( "Last bar is the first and only that meets such condition" );
        ACondition(RESET);
    }
}

Have fun.
 
tesla:
A quick clarification please:

Assume ConditionA occurred on previous bar (bar index 1) and also occurs on current bar (bar index 0).  Also assume that ConditionB occurs on current bar (i.e. volume is less than previous bar).  What's the desired result?
  1. Criteria for both conditions has been met on current bar, display both X's.
  2. ConditionA overrides check for ConditionB on current bar, display red X only.
  3. ConditionB overrides check for ConditionA on current bar, display purple X only.

Excellent point. The desired result is #1.
Reason: