if "Condition met for last 10 bars" then ...

 

Hello forum!

So the logic is: if, say, Moving Average 50 for the last 10 consecutive bars has been below each bar, then trade.

The script below though trades immediately after the first time MA50 falls below a bar, instead of waiting for 10 consecutive bars..

Any insight will be appreciated, I'm keeping fingers crossed it's only a quick fix and not a more fundamental failure in my understanding of "for" .. :P

Best,

Dan.


--- in Global scope ---

  bool Condition10Bars(){
     bool Condition10 = false;
      for (int iCond10 = 1; iCond10 <= 10; iCond10 ++){
           double MA50_10 = iMA(symbol,timeframe,50,0,MODE_SMA,PRICE_CLOSE,iCond10);
           double Min10 =iLow(symbol,timeframe,iCond10);
             if(MA50_10 < Min10) 
              Condition10 = true;
        }
    return( Condition10 );
    }


--- in OnTick ---
 
 bool OnTick_Condition10Bars = Condition10Bars();
 if (OnTick_Condition10Bars == true) OrderSend (etc. etc.);
 

If you find one MA50_10 < Min10 you set Condition10 true; thus you trade.

return false if you find any MA50_10 > Min10 return true at loop exit.

 
WHRoeder:

If you find one MA50_10 < Min10 you set Condition10 true; thus you trade.

return false if you find any MA50_10 > Min10 return true at loop exit.

Thank you very much sir. I finally nailed it, your cryptic comment kicked me clear on the right course though ! :P