//+------------------------------------------------------------------+
//|                                                        SD EA.mq4 |
//+------------------------------------------------------------------+
extern string com="This EA only use for testing,Do not in Live Account";

extern bool BuyPermit=true;
extern bool SelPermit=true;

extern int tf=0;
extern double lots=0.1;
extern int slip=5;

extern string com1="MACD Setting";
extern int Fast=108;
extern int Slow=234;
extern int Sigl=81;
extern int MAPc=PRICE_CLOSE;
extern int shift=0;

extern string com2="Stochastic Setting";
extern int Kperiod=22;
extern int Dperiod=6;
extern int Slowing=6;
extern int MAmthod=MODE_SMA;
extern int PCfield=0;//0 - Low/High or 1 - Close/Close.
extern int RoundLow=20;
extern int RoundHig=80;

extern string com3="MA Setting";
extern int Period1=14;
extern int Period2=28;
//+------------------------------------------------------------------+
int init(){
  if(lots<MarketInfo(Symbol(),MODE_MINLOT))
     lots=MarketInfo(Symbol(),MODE_MINLOT);
  return(0);
}
//+------------------------------------------------------------------+
int start()
  {
//----
if(IsTesting()==false) {
   Alert(com);
   PlaySound("news.wav");
   return;
  }
if(Total()==0) {
   double
   MACDu =iCustom(NULL,tf,"MTF_MACD_inColor",tf,Fast,Slow,Sigl,0,2,shift),
   MACDd =iCustom(NULL,tf,"MTF_MACD_inColor",tf,Fast,Slow,Sigl,0,3,shift),
   Stom1 =iStochastic(NULL,tf,Kperiod,Dperiod,Slowing,MAmthod,PCfield,MODE_MAIN,1),
   Stom2 =iStochastic(NULL,tf,Kperiod,Dperiod,Slowing,MAmthod,PCfield,MODE_MAIN,2),
   Stos1 =iStochastic(NULL,tf,Kperiod,Dperiod,Slowing,MAmthod,PCfield,MODE_SIGNAL,1),
   Stos2 =iStochastic(NULL,tf,Kperiod,Dperiod,Slowing,MAmthod,PCfield,MODE_SIGNAL,2);
   
   if(MACDu!=0 && Stom1<=RoundLow && Stom1>Stos1 && Stom2<Stos2 && BuyPermit)
      buy();
   if(MACDd!=0 && Stom1>=RoundHig && Stom1<Stos1 && Stom2>Stos2 && SelPermit)
      sel();
  }
  
if(Total()>0) {
   double
   Maf1  =iMA(NULL,tf,Period1,0,MODE_SMA,PRICE_CLOSE,1),
   Maf2  =iMA(NULL,tf,Period1,0,MODE_SMA,PRICE_CLOSE,2),
   Mas1  =iMA(NULL,tf,Period2,0,MODE_SMA,PRICE_CLOSE,1),
   Mas2  =iMA(NULL,tf,Period2,0,MODE_SMA,PRICE_CLOSE,2);
   
   for(int i=0; i<OrdersTotal(); i++) {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderSymbol()!=Symbol()) continue;
       if(OrderType()==OP_BUY && Maf1<Mas1 && Maf2>Mas2)
          OrderClose(OrderTicket(),OrderLots(),Bid,slip,White);
       if(OrderType()==OP_SELL&& Maf1>Mas1 && Maf2<Mas2)
          OrderClose(OrderTicket(),OrderLots(),Ask,slip,White);
      }
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+
int Total() {
   int on;
   for(int i=0; i<OrdersTotal(); i++) {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderSymbol()!=Symbol()) continue;
          on ++;
      }
   return(on); 
}

void buy() {
   int ticket1 =OrderSend(Symbol(), OP_BUY , lots, MarketInfo(Symbol(),MODE_ASK), 3, 0, 0, NULL, NULL, 0, Blue);
   Sleep(4000);
   if(ticket1<0)
      Print("buy "+Symbol()+" infomation Error trading: "+GetLastError());
   return(0);
}

void sel() {
   int ticket1 =OrderSend(Symbol(), OP_SELL , lots, MarketInfo(Symbol(),MODE_BID), 3, 0, 0, NULL, NULL, 0, Red);
   Sleep(4000);
   if(ticket1<0)
      Print("sell "+Symbol()+" infomation Error trading: "+GetLastError());
   return(0);
}