Download MetaTrader 5

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

To add comments, please log in or register
Having questions to the author of an article? Discuss them on the Forum!
Dannoo007
195
Dannoo007 2016.01.07 12:05 

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.);
whroeder1
14514
whroeder1 2016.01.07 13:06  

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.

Dannoo007
195
Dannoo007 2016.01.07 23:38  
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

To add comments, please log in or register