Why Expert advisor is leaving the start() function ?

 

Hello everyone,

I have to ask you because I am tired of looking for a mistake in here. I have this code and have no idea why it is leaving the start () function after the big (main) for loop.

EDIT: And second thing is - even if (sometimes it happens) program gets through the for loop i get ordersend error

20:33:21 2011.02.09 00:37 Sidus_method_bot EURGBP,H1: OrderSend error 130
20:33:21 2011.02.09 00:37 Sidus_method_bot EURGBP,H1: Alert: SELL error(0): no error

because of this line :

OrderSend(Symbol(),  OP_SELL,  0.2,  Bid,  3, Bid-15*Point,  Bid+15*Point);

#include <WinUser32.mqh>
#include <stdlib.mqh>// for errordescribtion function
//extern
 double EMA18 = 0;
 double EMA28 = 0;
 double WMA5 = 0;
 double WMA8 = 0;
 int ticks;
//---- input parameters
extern int       FastEMA=18;
extern int       SlowEMA=28;
extern int       RSIPeriod=17;
extern bool      Alerts=true;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//double rsi_sig[];
//---- variables
int sigCurrent=0;
int sigPrevious=0;
double pipdiffCurrent=0;
double pipdiffPrevious=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  ticks = 0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {  
   int limit;
   int counted_bars=IndicatorCounted();
   double rsi_sig=0;
   bool entry=false;
   double entry_point=0;
   int j = 0;
   
   //---- check for possible errors
   //if(counted_bars<0) return(-1);
    ticks++;
   if( ticks%50 != 0 ) {return(-1);}
  
   //---- last counted bar will be recounted
   if(counted_bars>0) {counted_bars--;}
   limit = Bars-counted_bars;
   Alert("ticks = ", ticks, " limit = ", limit);
   //---- main loop
   for(int i=0; i<limit; i++)
   {
     //---- ma_shift set to 0 because SetIndexShift called abowe
     ExtMapBuffer1[i]=iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
     ExtMapBuffer2[i]=iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
     rsi_sig = iRSI("EURGBP", 0, RSIPeriod, PRICE_CLOSE, i);
     
     pipdiffCurrent=(iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i)  -  iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i) );
      Alert("pipdiffCurrent = ", pipdiffCurrent, " rsi_sig = ", rsi_sig);
      
     Comment("pipdiffCurrent = "+pipdiffCurrent+" ");
     if (pipdiffCurrent>0 && rsi_sig>50) 
     {
       sigCurrent = 1;  //Up
     }
     else if (pipdiffCurrent<0 && rsi_sig<50)
     {
       sigCurrent = 2;  //Down
     }

     if (sigCurrent==1 && sigPrevious==2)
     {
        ExtMapBuffer4[i-1] = High[i-1]-5*Point;
        //ExtMapBuffer3[i] = Ask;
        entry=true;
        entry_point=Ask;
     } 
     else if (sigCurrent==2 && sigPrevious==1)
     {
        ExtMapBuffer3[i-1] = Low[i-1]-5*Point;
        //ExtMapBuffer4[i] = Bid;
        entry=true;
        entry_point=Bid;
     }

     sigPrevious=sigCurrent;
     pipdiffPrevious=pipdiffCurrent;
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
   Alert("sigCurrent = ", sigCurrent, " sigPrevious = ", sigPrevious); // these alerts are displayed
   Alert("Alerts = ", Alerts, " entry = ", entry );
   }
// END OF FOR LOOP

   Alert("sigCurrent = ", sigCurrent, " sigPrevious = ", sigPrevious); // and these are not  - WHY !?
   Alert("Alerts = ", Alerts, " entry = ", entry );
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------




   if(Alerts && entry)
   {
     PlaySound("alert.wav");
     if (sigPrevious==1)
     {
        MessageBox("Entry point: buy at "+entry_point+"!!", "Entry Point", MB_OK);
        
        if( OrdersTotal() > 0  ) // If there is any order then close it and perform the new one
        {
           for (j = OrdersTotal()-1; j > 0; j--)
            {
               if ( OrderSelect(j, SELECT_BY_POS) )
                {
                 if (OrderType() == OP_BUY)
                  OrderClose(j, 0.1, Bid, 3);
                }
            }
        } 
            int handle = OrderSend(Symbol(),  OP_BUY,  0.2,  Ask,  3, Ask-15*Point,  Ask+15*Point); // stoploss and takeprofit both equal 15 pip
            if(handle<1)
             {
             int err=GetLastError();
             Alert("error(",err,"): ",ErrorDescription(err) );
             return(0);
             }

     }      
     else if (sigPrevious==2)
     {
        MessageBox("Entry point: sell at "+entry_point+"!!", "Entry Point", MB_OK);
        if( OrdersTotal() > 0  ) // If there is any order then close it and perform the new one
        {
           for (int g = OrdersTotal()-1; g > 0; g--)
            {
               if ( OrderSelect(g, SELECT_BY_POS) )
                {
                 if (OrderType() == OP_SELL)
                  OrderClose(g, 0.1, Bid, 3);
                }
            }
         } 
            int handle2 = OrderSend(Symbol(),  OP_SELL,  0.2,  Bid,  3, Bid-15*Point,  Bid+15*Point); // stoploss and takeprofit both equal 15 pip
            if(handle<1)
             {
             int err2=GetLastError();
             Alert("error(",err,"): ",ErrorDescription(err) );
             return(0);
             }        
     }
     
     
     entry=false;
   }
RefreshRates();
 
   return(0);
  }
 

Note: I have not run the code, only looked at it briefly. However, ...

A) I don't like the line

  if( ticks%50 != 0 ) {return(-1);}

I think that this means that the EA will only process every 50th tick

B) Then I don't understand how lines like

     ExtMapBuffer1[i]=iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
     ExtMapBuffer2[i]=iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);

can work as IIRC these buffers are not allocated (which they would be in a 'usual' indicator in the init() function)

Reason: