
#property strict

extern int MagicNumber1=2016;
extern double LotSize1=1;
extern double LotsL=1;
extern double LotsS=1;
extern bool UseMartingle=true;
extern double MultiplyBy=2;
extern int StopLoss=0;
extern int TakeProfit=0;
extern bool AutoClose=true;
extern bool UseTrailingStop=true;
extern int TrailingStop=10;
extern double X=50;
extern bool EmailAlert=true;
extern string Mode1="=====FIRST MODE=====";
extern bool UseFirstMode=true;
extern int FirstMA1=300;
extern ENUM_MA_METHOD FirstMAmode1=MODE_SMA;
extern int SecondMA1=700;
extern ENUM_MA_METHOD SecondMAmode1=MODE_SMA;

extern int StopXpip=20;

double point;
int digits,Q;
double SL,TP,sell,buy,close,move;
int ThisBarTrade=0;
bool NewBar;
double Lots;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
                        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(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      if (Bars != ThisBarTrade ) {
NewBar=true;
ThisBarTrade = Bars;  
NewBar=true;  
}

double FirstEMA2=iMA(Symbol(),0,FirstMA1,0,FirstMAmode1,PRICE_CLOSE,2);
double FirstEMA1=iMA(Symbol(),0,FirstMA1,0,FirstMAmode1,PRICE_CLOSE,1);
double SecondEMA2=iMA(Symbol(),0,SecondMA1,0,SecondMAmode1,PRICE_CLOSE,2);
double SecondEMA1=iMA(Symbol(),0,SecondMA1,0,SecondMAmode1,PRICE_CLOSE,1);

if(NewBar){


//=========FIRST MOOOOOOOOOODE
  if(FirstEMA1>SecondEMA1&&FirstEMA2<=SecondEMA2&&UseFirstMode){
  if(AutoClose)CloseOrders(MagicNumber1,OP_SELL);
  if(AutoClose)CloseOrders(MagicNumber1,OP_SELLSTOP);
   if(StopLoss==0){SL=0;}else{SL=Ask-StopLoss*point;}
   if(TakeProfit==0){TP=0;}else{TP=Ask+TakeProfit*point;}
      if(UseMartingle&&HistoryProfit(MagicNumber1)<0){Lots=NextLot(MagicNumber1);}else{Lots=LotSize1;}
buy=OrderSend(Symbol(),OP_BUY,     Lots, Ask,                 3*Q,0                   ,TP,"Buy  MODE 1",MagicNumber1,0,clrBlue);
buy=OrderSend(Symbol(),OP_BUYSTOP,LotsL/2,Ask+StopXpip*1*point,3*Q,Ask,TP,"Buy Stop MODE 1",MagicNumber1,0,clrBlue);
buy=OrderSend(Symbol(),OP_BUYSTOP,LotsL/2,Ask+StopXpip*2*point,3*Q,Ask+StopXpip*1*point,TP,"Buy Stop MODE 1",MagicNumber1,0,clrBlue);
buy=OrderSend(Symbol(),OP_BUYSTOP,LotsL/2,Ask+StopXpip*3*point,3*Q,Ask+StopXpip*2*point,TP,"Buy Stop MODE 1",MagicNumber1,0,clrBlue);







if(EmailAlert)SendMail(Symbol(),"Order Opened Buy MODE 1");

}

  if(FirstEMA1<SecondEMA1&&FirstEMA2>=SecondEMA2&&UseFirstMode){
  if(AutoClose)CloseOrders(MagicNumber1,OP_BUY);
  if(AutoClose)CloseOrders(MagicNumber1,OP_BUYSTOP);
   if(StopLoss==0){SL=0;}else{SL=Bid+StopLoss*point;}
   if(TakeProfit==0){TP=0;}else{TP=Bid-TakeProfit*point;}
      if(UseMartingle&&HistoryProfit(MagicNumber1)<0){Lots=NextLot(MagicNumber1);}else{Lots=LotSize1;}
sell=OrderSend(Symbol(),OP_SELL,    Lots,            Bid           ,3*Q,0               ,TP,"Sell  MODE 1",MagicNumber1,0,clrRed);
sell=OrderSend(Symbol(),OP_SELLSTOP,LotsS/2,Bid-StopXpip*1*point,3*Q,Bid,TP,"Sell Stop MODE 1",MagicNumber1,0,clrRed);
sell=OrderSend(Symbol(),OP_SELLSTOP,LotsS/2,Bid-StopXpip*2*point,3*Q,Bid-StopXpip*1*point,TP,"Sell Stop MODE 1",MagicNumber1,0,clrRed);





if(EmailAlert)SendMail(Symbol(),"Order Opened Sell MODE 1");

}



NewBar=false;
}
   
   if(UseTrailingStop){
   sub_trailingsl(MagicNumber1);
   }
  
  }
//+------------------------------------------------------------------+
 int orderscnt(int magic){
 int cnt=0;
   for(int i =0;i<OrdersTotal();i++){
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol() && magic==OrderMagicNumber()){
         cnt++;
       }
     }
    return(cnt);
  } 
  //+++++++++++++++++++//
          int CloseOrders(int magic,int type) {
         int cnt=OrdersTotal();
         for(int i=cnt-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == magic&&OrderType()==type)
                 {
                 
                     close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3*Q,clrWhite);   
           }
       }
   

  return(0);
} 
//*********
void ModifyStopLoss(double ldStop) 
{
  bool   fm;
  double ldOpen=OrderOpenPrice();
  double ldTake=OrderTakeProfit();

  fm=OrderModify(OrderTicket(), ldOpen, ldStop, ldTake, 0, Pink);
}

void sub_trailingsl(int magic) 
{
  for (int i=0; i<OrdersTotal(); i++) 
  {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
    {
      if (OrderSymbol()==Symbol()&&OrderMagicNumber()==magic ) 
      {
         if (OrderType()==OP_SELL) 
         {
       if (OrderOpenPrice()-Ask>=TrailingStop*point&&(OrderStopLoss()==0||OrderStopLoss()>=OrderOpenPrice())
       &&OrderStopLoss()!=NormalizeDouble(Ask+TrailingStop*point,digits))
            {
                  ModifyStopLoss(OrderOpenPrice()-X*point);
            }
            
       if (OrderStopLoss()!=0&&OrderStopLoss()!=NormalizeDouble(Ask+TrailingStop*point,digits)
       &&OrderStopLoss()<OrderOpenPrice()&&OrderStopLoss()-Ask>TrailingStop*point)
            {
                  ModifyStopLoss(OrderOpenPrice()-X*point);
            }
            

         }
         if (OrderType()==OP_BUY)
         {
       if (Bid-OrderOpenPrice()>=TrailingStop*point&&(OrderStopLoss()==0||OrderStopLoss()<=OrderOpenPrice())
       &&OrderStopLoss()!=NormalizeDouble(Bid-TrailingStop*point,digits))
            {
                  ModifyStopLoss(OrderOpenPrice()+X*point);
            }
            
       if (OrderStopLoss()!=0&&OrderStopLoss()!=NormalizeDouble(Bid-TrailingStop*point,digits)
       &&OrderStopLoss()>OrderOpenPrice()&&Bid-OrderStopLoss()>(TrailingStop*point))
            {
                  ModifyStopLoss (OrderOpenPrice()+X*point);
            }
            

         }
      }
    }
  }
}

//==========
   double HistoryProfit(int magic){
    double hp=0;
      for(int i=0;i<=OrdersHistoryTotal();i++){
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic){
          hp=OrderProfit();
          }
       }    
         
   return(hp);
 }
double NextLot(int magic)
{

    double lot=0;
      for(int i=0;i<=OrdersHistoryTotal();i++){
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
                  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic){
          lot=OrderLots()*MultiplyBy;
          }
       }    
         
   return(lot);
 }
         

