EA error

 

Hi,


I have coded this EA - for some reason, MT4 cannot backtest it - it says Using M1, then once that is finished, it gets stuck. Any suggestions?


Thank you in advance

//+------------------------------------------------------------------+
//|                                                   MACD Bands.mq4 |
//|                                                          Gulzaar |
//|                                    http://gulzaarfx.blogspot.com |
//+------------------------------------------------------------------+

#property copyright "Gulzaar"
#property link      "http://gulzaarfx.blogspot.com"

//External Variables//-------------------------

extern double Lots = 0.1;
extern int MagicNumber = 78652;
extern int MACDfast = 21,
           MACDslow = 55,
           MACDsignal = 8,
           Window = 10;



//Global Variables//---------------------------

double TwentySMA1,TwentySMA2,TwentySMA3,TwentySMA4,MACDmain1,MACDsignal1,MACDmain2,MACDsignal2,MACDmain3,MACDsignal3;
double MACDmain4,MACDsignal4, StopLoss;
int CrossPoint, Ticket;
bool IsTrade;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits == 5)
      {
         Window = Window * 10;
         
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
double TwentySMA1 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
double TwentySMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,2);
double TwentySMA3 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,3);
double MACDmain = iMACD(NULL,0,MACDfast,MACDslow,MACDsignal,PRICE_CLOSE,MODE_MAIN,0);
double MACDmain1 = iMACD(NULL,0,MACDfast,MACDslow,MACDsignal,PRICE_CLOSE,MODE_MAIN,1);
double MACDsignal = iMACD(NULL,0,MACDfast,MACDslow,MACDsignal,PRICE_CLOSE,MODE_SIGNAL,0);
double MACDsignal1 = iMACD(NULL,0,MACDfast,MACDslow,MACDsignal,PRICE_CLOSE,MODE_SIGNAL,1);

//Check Open positions

for(int a = OrdersTotal(); a >= 0; a++)
   {
      if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES)){
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) IsTrade = true; 
//Close Buy orders 
      if(OrderType() == OP_BUY)
         {
            if(Close[1] < TwentySMA1 - Window * Point) OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
            
          }
//Close Sell Orders 
       if(OrderType() == OP_SELL)
         {
            if(Close[1] > TwentySMA1 + Window * Point) OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
         }
         
         }
     }
//Check conditions

//BUY

if(Close[1] > TwentySMA1 && Close[2] < TwentySMA2 && Close[3] < TwentySMA3)
   {
      if(MACDmain > MACDsignal && MACDmain1 < MACDsignal1) bool BuyOne = true;
      
    }

//SELL

if(Close[1] < TwentySMA1 && Close[2] > TwentySMA2 && Close[3] > TwentySMA3)
   {
      if(MACDmain < MACDsignal && MACDmain1 > MACDsignal1) bool SellOne = true;
      
    }

//End Check conditions

if(BuyOne == true && IsTrade == false)
   {
      for(int x =0; x<= 4; x++)
         {
            int Shift;
            double SMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,x);
            if(Close[x] > SMA) Shift = x;
            
          }
          StopLoss = Low[Shift];
      Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,MarketInfo(Symbol(),MODE_SPREAD),StopLoss,0,NULL,MagicNumber,0,Green); 
      IsTrade = true;
      if (Ticket==-1)   ShowERROR(Ticket,0,0);
    }
    
if(SellOne == true && IsTrade == false)
   {
            for(x =0; x<= 4; x++)
         {
            SMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,x);
            if(Close[x] > SMA) Shift = x;
            
          }

      StopLoss = High[Shift];
      Ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,MarketInfo(Symbol(),MODE_SPREAD),StopLoss,0,NULL,MagicNumber,0,Red); 
      IsTrade = true;
      if (Ticket==-1)   ShowERROR(Ticket,0,0);

    }      
      
   


            
//----
   return(0);
  }
//+------------------------------------------------------------------+


void ShowERROR(int Ticket,double SL,double TP)
{
   int err=GetLastError();
   switch ( err )
   {                  
      case 1:                                                                               return;
      case 2:   Alert("No connection with the trading server ",        Ticket," ",Symbol());return;
      case 130: Alert("Error close foot Ticket ",                      Ticket," ",Symbol());return;
      case 134: Alert("Not enough money ",                             Ticket," ",Symbol());return;
      case 146: Alert("Error Subsystem busy trade ",                   Ticket," ",Symbol());return;
      case 129: Alert("Error Invalid price ",                          Ticket," ",Symbol());return;
      case 131: Alert("Error Invalid volume ",                         Ticket," ",Symbol());return;
      default:  Alert("Error : ",err, "   ",                           Ticket," ",Symbol());return;
   }
}


 
for(int a = OrdersTotal(); a >= 0; a++)

replace with

for(int a = OrdersTotal()-1; a >= 0; a--)

 
qjol:

thanks:D - didn't catch that earlier
 

it still takes only one trade for some reason, which closes normally(not close at stop that usually happens when its stuck).


Any clues?

 
gulzaar:

Any clues?

Probably not connected to your issues . . but . . .

You aren't selecting Orders to be closed by Symbol and MagicNumber . . I'm sure you think you are, but you aren't.

You are trying to close Buy and Sell orders at the Bid price . . . if you have enough Slippage factored in that might work. Use OrderClosePrice() instead . . .

When you place a trade IsTrade is set = true . . . . when does it get set back to false so you can place a 2nd trade ?

 
RaptorUK:

Probably not connected to your issues . . but . . .

You aren't selecting Orders to be closed by Symbol and MagicNumber . . I'm sure you think you are, but you aren't.

You are trying to close Buy and Sell orders at the Bid price . . . if you have enough Slippage factored in that might work. Use OrderClosePrice() instead . . .

When you place a trade IsTrade is set = true . . . . when does it get set back to false so you can place a 2nd trade ?


Thanks lol - istrade was the culprit I had forgotten to declare it to false again. Thats why it never took any more trades.


I guess I shouldn't copy paste code;)

 
gulzaar:

Thanks lol - istrade was the culprit I had forgotten to declare it to false again. Thats why it never took any more trades.


I guess I shouldn't copy paste code;)



Also - could you please point out what you meant by not selecting orders by symbols and magic numbers?
 
gulzaar:


Also - could you please point out what you meant by not selecting orders by symbols and magic numbers?

Of course . . .

for(int a = OrdersTotal(); a >= 0; a++)
   {
      if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES)){
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) IsTrade = true;   // <--- only this is done if Symbol & Magic Number match

//Close Buy orders 
      if(OrderType() == OP_BUY)                
         {
            if(Close[1] < TwentySMA1 - Window * Point) OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);  //  <---  this happens if type == OP_BUY regardless
                                                                                                                                             //         of Symbol & Magic Number
            
          }
 

Thanks again - I thought I had nested it under the first if(OrderSymbol().... statement.

So I changed the code now to this:

for(int a = OrdersTotal(); a >= 0; a--)
   {
      OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) 
//Close Buy orders 
         {
            if(Close[1] < TwentySMA1 - Window * Point) OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE); IsTrade = False;
             
          }
//Close Sell Orders 
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) 
         {
            if(Close[1] > TwentySMA1 + Window * Point) OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE); IsTrade = false;
         }
         
         
     }
 

But the graph now looks like this:



All of the trades just close at stop

 

I would add this amendment . . just to be sure . .

if(Close[1] < TwentySMA1 - Window * Point) if ( OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE) )  IsTrade = False;
Reason: