Iteration problems.

 
Excuse me experienced traders I have a real easy question that would mean a lot to me if answered.

Im trying to make my EA wait for an X amount of canddles above or below a moving average (but not in the middle) to trade a signal. Currently I have a problem in the loop iteration when I'm trying to get a loop to count the canddles that fully opened/closed above/below it. Could you give me a hand?

Thank you very much!

Here goes the code:


int MAHANDLE;
double ma[],bid,ask,tpoint,tradenow;
input int NumofCanddles;
input int NumofPips;
input int StopLoss;
input int Takeprofit;
input int MAPERIOD;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   MAHANDLE=iMA(_Symbol,_Period,MAPERIOD,0,MODE_SMA,PRICE_CLOSE);

//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
   ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   tpoint=Point();
   
   MqlRates mrates[];
   ArraySetAsSeries(mrates,true);
   ArraySetAsSeries(ma,true);
   
   bool openposition=PositionSelect(_Symbol);

   CopyRates(_Symbol,PERIOD_M5,0,6,mrates);
   CopyBuffer(MAHANDLE,0,0,6,ma);
   
   int Canddlewait=0;

   do{if(mrates[0].open>ma[0]&&mrates[0].close>ma[0];
   Canddlewait++;}
   while(Canddlewait<NumofCanddles)

   if(mrates[0].open>ma[0]&&mrates[0].close>ma[0])
      {Canddlewait++;}
     
      
   if(mrates[0].open<ma[0]&&mrates[0].close>ma[0]&&mrates[0].close-ma[0]>NumofPips*tpoint&&openposition==false)
      { MqlTradeRequest request;
      MqlTradeResult result;
      ZeroMemory(request);
      ZeroMemory(result);

      request.action=TRADE_ACTION_DEAL;
      request.volume=0.3;
      request.symbol=_Symbol;
      request.type=ORDER_TYPE_BUY;
      request.price=ask;
      request.tp=request.price+(Takeprofit*tpoint);
      request.sl=request.price-(StopLoss*tpoint);
      request.type_filling=ORDER_FILLING_IOC;
      tradenow=0;

      OrderSend(request,result);
      }
      
     if(mrates[0].open>ma[0]&&mrates[0].close<ma[0]&&ma[0]-mrates[0].close>NumofPips*tpoint&&openposition==false)
      { MqlTradeRequest request;
      MqlTradeResult result;
      ZeroMemory(request);
      ZeroMemory(result);

      request.action=TRADE_ACTION_DEAL;
      request.volume=0.3;
      request.symbol=_Symbol;
      request.type=ORDER_TYPE_SELL;
      request.price=bid;
      request.tp=request.price-(Takeprofit*tpoint);
      request.sl=request.price+(StopLoss*tpoint);
      request.type_filling=ORDER_FILLING_IOC;
      tradenow=0;

      OrderSend(request,result);
      }
   
 
   do{if(mrates[0].open>ma[0]&&mrates[0].close>ma[0];
   Canddlewait++;}
   while(Canddlewait<NumofCanddles)
  1. You are looking at the same candle each loop.
  2. You need to loop over your 'X amount of canddles" and count above and below. You are looping NumofCanddles not X.
 

Thank you very much for your answer!!

Sadly, I copied the code but it didnt work it only returns canddlewait as 5 which is the same number as NumofCanddles. How do I iterate through the whole range?

Every time I run it, it returns the value of NumofCanddles but it doesnt go up in units. It goes from 0 to whatever other value is there. no 1,2,3,4 from 0 to 15.

 
julian lagier:

Thank you very much for your answer!!

Sadly, I copied the code but it didnt work it only returns canddlewait as 5 which is the same number as NumofCanddles. How do I iterate through the whole range?

Every time I run it, it returns the value of NumofCanddles but it doesnt go up in units. It goes from 0 to whatever other value is there. no 1,2,3,4 from 0 to 15.

datetime tim;

int numbars = 5;



if (ma0 < ma1 && ma1 > ma2)

        tim = iTime(NULL,0,0);

else if (ma0 > ma1 && ma1 < ma2)

        tim = iTime(NULL,0,0);

bool trade = false;

if iTime(NULL,0,0) - tim >= 60*Period() * numbars)

{

        //5 bars after the last reversal

        trade = true;

}
 
Thank you very much!! Could you please explain what variables you mean with ma0 ma1 and ma2? the canddles or the ma itself?
 
  1. you should add a new bar detection
  2. whenever a new bar occurs, loop over the recent X-1 candles to get the counts above/below MA, and remember the result in 2 static variables (say countAbove, countBelow)
  3. for every tick, if the current close is above/below your threshold and the static variable's value equals X-1, you're ready to open a position
Jean's approach would also work (with small fixes), up to you.