Download MetaTrader 5

Small modification to a very powerful EA

To add comments, please log in or register
Use searching on MQL5.community. It is easy!
Mohamed
66
Mohamed 2013.12.07 20:30 
 datetime Time0;
 double point;
 int digits,cnt,Q;
 extern int MagicNumber=2533;  
 int Di,Do;                         
int init()
{  
 if(AddSpread)
  { 
  if(AddSpreadToBuyOrders){Di=0;Do=MarketInfo(Symbol(),MODE_SPREAD);}
  else{Di=MarketInfo(Symbol(),MODE_SPREAD);Do=0;}  
  }
 else 
  {
  Di=0;Do=0;
  }
  if(Digits==5||Digits==3)Q=10;
  else{Q=1;}
    if(Digits<4)
   {
      point=0.01;
      digits=2;
     
   }
   else
   {
      point=0.0001;
      digits=4;
      
   }
return(0);
}

 //+------------------------------------------------------------------+
 //| FUNCTION DEFINITIONS    deinitialization function                |
 //+------------------------------------------------------------------+

 void deinit() {
    Comment("");
  }

 
 
int _OrdersTotal(int type){
 int cnt=0;
   for(int i =0;i<OrdersTotal();i++){
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
       if(OrderSymbol()==Symbol()&&OrderType()==type && MagicNumber==OrderMagicNumber()){
         cnt++;
       }
     }
   }
    return(cnt);
  }
 //+------------------------------------------------------------------+
 //| FUNCTION DEFINITIONS   Start function                            |
 //+------------------------------------------------------------------+

 int start()
   {
  
   Comment("Programmed by MR.dollar"+"\n"+"Idea Created by mtkhreb"+"\n"+"منتدى المتداول العربي"+"\n"+"www.arabictrader.com/vb");
   if(UseFirstOrderFilter)
    {
     double H=High[iHighest(NULL,0,MODE_HIGH,NumberOfCandles,StartCandle)];
     double L=Low[iLowest(NULL,0,MODE_LOW,NumberOfCandles,StartCandle)];
     if(Close[0]>H+Distance*point)FirstOrder=0;
     if(Close[0]<L-Distance*point)FirstOrder=1;
     if(Close[0]<H+Distance*point&&Close[0]>L-Distance*point)FirstOrder=2;
    }
   if(UseiMASignal)
    {
     FirstOrder=2;
     double u=iCustom(NULL,0,"iMA",period,periodDivisor,method,price,0,1);
     double u2=iCustom(NULL,0,"iMA",period,periodDivisor,method,price,0,2);
     double v=iCustom(NULL,0,"iMA",period,periodDivisor,method,price,1,1);
     double v2=iCustom(NULL,0,"iMA",period,periodDivisor,method,price,1,2);
     if(u2>5000&&u<5000)FirstOrder=0;
     if(v2>5000&&v<5000)FirstOrder=1;
    }
   int Tried;
   if((((Profit()<MaximumLossUSD&&MaximumLossUSD<0)||(MathAbs((Profit()/AccountBalance())*100)>MaximumLossPercent&&Profit()<0))&&CloseOrdersAfterLoss)||((
      (Profit()>MaximumGainUSD)||((Profit()/AccountBalance())*100>MaximumGainPercent&&Profit()>0))&&CloseOrdersAfterProfit)||Close_Delete_All)
     {
      DeleteOrders(OP_BUYSTOP);DeleteOrders(OP_SELLSTOP);
      while(_OrdersTotal(OP_BUY)+_OrdersTotal(OP_SELL)>0&&Tried<20)
       {
        CloseOrders();
        Tried++;
       }
      StopExpert=true;
     }
       //////////////////////////////////////////////////
       double buy,sell,SL,TP,newLot;int tick;
  
     for(cnt=0;cnt<OrdersTotal();cnt++)
      {
       OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
       if(OrderSymbol() == Symbol() && OrderMagicNumber () == MagicNumber&&OrderType()==OP_BUY)
        {
         double openpricebuy = OrderOpenPrice();
        }     
      }
       {
        if(_OrdersTotal(OP_BUY)>=1&&sellnow==true&&PendingOrders(OP_SELLSTOP)+PendingOrders(OP_BUYSTOP)==0)
         {
          if(TakeProfit==0||ECN_Broker){TP=0;}else{TP=openpricebuy-Step*point-TakeProfit*point+Di*Point;}
          if(StopLoss==0||ECN_Broker){SL=0;}else{SL=openpricebuy-Step*point+StopLoss*point+Di*Point;}
          if(IsTradeAllowed())
           {
            tick=OrderSend(Symbol(),OP_SELLSTOP,new(),NormalizeDouble(openpricebuy-Step*point,digits),Slippage*Q,NormalizeDouble(SL,digits),NormalizeDouble(TP,digits),"MR.dollar EA",MagicNumber,0,Red);
           if(tick>0){ buynow=true;sellnow=false;}
           }
          if(ECN_Broker)
           {
            if(tick>0)OrderModify(tick,NormalizeDouble(openpricebuy-Step*point,digits),NormalizeDouble(openpricebuy-Step*point+StopLoss*point+Di*Point,digits),NormalizeDouble(openpricebuy-Step*point-TakeProfit*point+Di*Point,digits),0,0);
           }
         }
       }
          
      for(cnt=0;cnt<OrdersTotal();cnt++)
       {
        OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
        if(OrderSymbol() == Symbol() && OrderMagicNumber () == MagicNumber&&OrderType()==OP_SELL)
         {
          double openpricesell = OrderOpenPrice();
         }     
       }
        {
        if(_OrdersTotal(OP_SELL)>=1&&buynow==true&&PendingOrders(OP_BUYSTOP)+PendingOrders(OP_SELLSTOP)==0)
         {
          if(TakeProfit==0||ECN_Broker){TP=0;}else{TP=openpricesell+Step*point+TakeProfit*point-Do*Point;}
          if(StopLoss==0||ECN_Broker){SL=0;}else{SL=openpricesell+Step*point-StopLoss*point-Do*Point;}
          if(IsTradeAllowed())
           {
            tick=OrderSend(Symbol(),OP_BUYSTOP,new(),NormalizeDouble(openpricesell+Step*point,digits),Slippage*Q,NormalizeDouble(SL,digits),NormalizeDouble(TP,digits),"MR.dollar EA",MagicNumber,0,Blue);
           if(tick>0){sellnow=true;buynow=false;}
           }
          if(ECN_Broker)
           {
            if(tick>0)OrderModify(tick,NormalizeDouble(openpricesell+Step*point,digits),NormalizeDouble(openpricesell+Step*point-StopLoss*point-Do*Point,digits),NormalizeDouble(openpricesell+Step*point+TakeProfit*point-Do*Point,digits),0,0);
           }
         }
       }
          
     
  
      if(_OrdersTotal(OP_BUY)+_OrdersTotal(OP_SELL)<1&&PendingOrders(OP_BUYSTOP)+PendingOrders(OP_SELLSTOP)<2)
        {
         DeleteOrders(OP_BUYSTOP);DeleteOrders(OP_SELLSTOP);
        }
        
      ////////////////////////////////
   if(UseHourTrade){
     if(!(Hour()>=FromHourTrade&&Hour()<=ToHourTrade)){
       Comment("Non-Trading Hours!");
       return(0);
     }
    }
   if(MarketInfo(Symbol(),MODE_SPREAD)>MaxSpread||(StopExpert&&StopExpertAfterProfit)){return(0);}
       
   if(_OrdersTotal(OP_BUY)+_OrdersTotal(OP_SELL)<1)
     {
      if(PendingOrders(OP_SELLSTOP)+PendingOrders(OP_BUYSTOP)<1)
       {
        if(StopLoss==0||ECN_Broker){SL=0;}else{SL=Ask-StopLoss*point;}
        if(TakeProfit==0||ECN_Broker){TP=0;}else{TP=Ask+TakeProfit*point;}
        if(FirstOrder==0)
         {
          if(IsTradeAllowed())
           {
            tick=OrderSend(Symbol(),OP_BUY,Lot1,NormalizeDouble(Ask,digits),Slippage*Q,NormalizeDouble(SL-Do*Point,digits),NormalizeDouble(TP-Do*Point,digits),"MR.dollar EA",MagicNumber,0,Blue);
            buynow=false;sellnow=true;
           }
          if(ECN_Broker){
            if(tick>0)OrderModify(tick,OrderOpenPrice(),NormalizeDouble(Ask-StopLoss*point-Do*Point,digits),NormalizeDouble(Ask+TakeProfit*point-Do*Point,digits),0,0);
          }
         }
        if(StopLoss==0||ECN_Broker){SL=0;}else{SL=Bid+StopLoss*point;}
        if(TakeProfit==0||ECN_Broker){TP=0;}else{TP=Bid-TakeProfit*point;}
        if(FirstOrder==1)
         {
          if(IsTradeAllowed())
           {
            tick=OrderSend(Symbol(),OP_SELL,Lot1,NormalizeDouble(Bid,digits),Slippage*Q,NormalizeDouble(SL+Di*Point,digits),NormalizeDouble(TP+Di*Point,digits),"MR.dollar EA",MagicNumber,0,Red);
            buynow=true;sellnow=false;
           }
          if(ECN_Broker){
            if(tick>0)OrderModify(tick,OrderOpenPrice(),NormalizeDouble(Bid+StopLoss*point+Di*Point,digits),NormalizeDouble(Bid-TakeProfit*point+Di*Point,digits),0,0);
          }
        }
         PlaySound("Alert.wav");
         Time0=Time[0];
         if(ReverseFirstOrder)
          {
           if(FirstOrder==1)FirstOrder=0;
           if(FirstOrder==0)FirstOrder=1;
          }
           
       }
     }
       
       
  }
      
//+------------------------------------------------------------------+
void DeleteOrders(int type)
{
  int total  = OrdersTotal();
  
  for (int cnt = 0 ; cnt < total ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == MagicNumber && OrderSymbol()==Symbol())
    {
      if (OrderType()==type)
      {
        if(OrderDelete(OrderTicket())==false)
         {
           RefreshRates();
         }
         else
         {
           cnt=0;
           total=OrdersTotal();
         }
       }
      
    }
  }
}  

int PendingOrders(int type)
{
 int c=0;
 for(int i=0;i<OrdersTotal();i++)
  {
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  if(OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()&&OrderType()==type)
   {
    c++;
   }
  }
 return(c);
}
void CloseOrders()
{
  for (int cnt = 0 ; cnt < OrdersTotal() ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == MagicNumber && OrderSymbol()==Symbol())
    {
      if (OrderType()==OP_BUY){
        OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,digits),3*Q);
       }
      if(OrderType()==OP_SELL){
      OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,digits),3*Q);
      }
    }
  }
}  
double new(){
 double newLot;
    double A=_OrdersTotal(OP_BUY)+_OrdersTotal(OP_SELL);
      
       if(A==1)newLot=Lot2;
       if(A==2)newLot=Lot3;
       if(A==3)newLot=Lot4;
       if(A==4)newLot=Lot5;
       if(A==5)newLot=Lot6;
       if(A==6)newLot=Lot7;
       if(A==7)newLot=Lot8;
       if(A==8)newLot=Lot9;
       if(A==9)newLot=Lot10;
       if(A==10)newLot=Lot11;
      if(A==11)newLot=Lot12;
       if(A==12)newLot=Lot13;
       if(A==13)newLot=Lot14;
       if(A==14)newLot=Lot15;
       if(A==15)newLot=Lot16;
       if(A==16)newLot=Lot17;
       if(A==17)newLot=Lot18;
       if(A==18)newLot=Lot19;
      if(A==19)newLot=Lot20;
      if(A==20)newLot=Lot21;
       if(A==21)newLot=Lot22;
       if(A==22)newLot=Lot23;
       if(A==23)newLot=Lot24;
       if(A>=24)newLot=Lot25;
    //  default:newLot=15;
      
   return(newLot);
   }
   
double Profit()
 {
  double Prof;
  for(int i=OrdersTotal();i>=0;i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
     {
      Prof+=OrderProfit();
     }
   }
  return(Prof);
 } 

Hi Guys, this EA is working very fine on back test but when put it on live it seems that there is no time between orders so it open the buy or sell order twice ... could you help me by adding sleep time after placing any order?

Ian Venner
2584
Ian Venner 2013.12.08 06:33  

You should post requests for coding work in the jobs forum. This forum is for people who are coding themselves.

To add comments, please log in or register