//+------------------------------------------------------------------+
//|                                               Zone_Recovery_Area |
//|             http://algorithmic-trading-ea-mt4.000webhostapp.com/ |
//|                                                    AHARON TZADIK |
//+------------------------------------------------------------------+
#property copyright   "AHARON TZADIK"
#property link        "http://algorithmic-trading-ea-mt4.000webhostapp.com/"
#property version   "1.00"
#property strict

extern double TakeProfit=150;             //Take Profit(in pips)
extern double Zone_Recovery_Area=50;      //Zone_Recovery_Area(in pips)
input string  str5="Fast moving average";
input int     Period1=20;                 //Fast moving average(in pips)
input string  str6="Slow moving average";
input int     Period2=200;                //Slow moving average(in pips)

double buyprice;
bool result;

int    MAGICNUMBER1=1111;
int    MAGICNUMBER2=2222;
int    MAGICNUMBER3=3333;
int    MAGICNUMBER4=4444;
int    MAGICNUMBER5=5555;
int    MAGICNUMBER6=6666;
int    MAGICNUMBER7=7777;
int    MAGICNUMBER8=8888;
int    MAGICNUMBER9=9999;
int    MAGICNUMBER10=1010;
int    MAGICNUMBER11=9111;
int    MAGICNUMBER12=1212;

int           err;
double        pips;
//+-----------------

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.00001 || Point==0.01)
      pips=ticksize*10;
   else pips=ticksize;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

   if(isnewcandle())     //If it is a new open bar
      ZONE_RECOVERY1();  //6 upper trades
   ZONE_RECOVERY2();     //6 lower trades
  }
//+---------------------------------------------------------------------------+
//|  We work only with open bar price  -        isnewcandle()                 |
//+---------------------------------------------------------------------------+
bool isnewcandle()
  {
   static int  barsonchart=0;
   if(Bars==barsonchart)
      return(false);
   barsonchart=Bars;
   return(true);
  }
//+---------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|     Upper 6 trades            ZONE RECOVERY 1                    |
//+------------------------------------------------------------------+
void ZONE_RECOVERY1()
  {
//                                    TRADE 1+ 2 ZONE RECOVERY1 1 LOT-BUY ITERATION + 1.4 LOT- SELL ITERATION
// ===================================================================================================================== 
   int iCount=0;
   int Count_one_trade_a=0;
   int Count_one_trade_b=0;
   int Count_one_trade_c=0;
   int Count_one_trade_d=0;
   int Count_one_trade_e=0;
 
   if(OrdersTotal()==0)
      //Here you put your conditions for trading//
     {
      if((iMA(Symbol(),0,Period1,0,0,0,1)>iMA(Symbol(),0,Period2,0,0,0,1)
         && Ask>iMA(Symbol(),0,Period2,0,0,0,0)))

         int ticket16=OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-(TakeProfit*pips+Zone_Recovery_Area*pips),Ask+TakeProfit*pips,"1",MAGICNUMBER1,0,Green);

     }
   if(OrdersTotal()>=1)
     {
      for(iCount=0;iCount<OrdersTotal();iCount++)
        {

         for(int buy1=OrdersTotal()-1;buy1>=0;buy1--)
           {
            if(Count_one_trade_a<1)
               if(OrderSelect(buy1,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER1)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_BUY)
                           if(OrderOpenPrice()-Bid>Zone_Recovery_Area*pips)
                              int ticket2=OrderSend(Symbol(),OP_SELL,1.4,Bid,3,OrderOpenPrice()+TakeProfit*pips,OrderOpenPrice()-(TakeProfit*pips+Zone_Recovery_Area*pips),"2",MAGICNUMBER2,0,Red);
            buyprice=OrderOpenPrice();

            exitbuys();  //CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_a++;
           }

         //                                       TRADE 3 ZONE RECOVERY1 1 LOT BUY ITERATION
         // ===================================================================================================================== 
         for(int buy2=OrdersTotal()-1;buy2>=0;buy2--)
           {
            if(Count_one_trade_b<1)
               if(OrderSelect(buy2,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER2)
                     if(OrderSymbol()==Symbol()) 
                        if(OrderType()==OP_SELL) 
                           if(Ask>buyprice)
                              int ticket3=OrderSend(Symbol(),OP_BUY,1,Ask,3,buyprice-(TakeProfit*pips+Zone_Recovery_Area*pips),buyprice+TakeProfit*pips,"3",MAGICNUMBER3,0,Green);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_b++;
           }
         //                                     TRADE 4 ZONE RECOVERY1 1.4 LOT SELL ITERATION
         // ===================================================================================================================== 
         for(int buy3=OrdersTotal()-1;buy3>=0;buy3--)
           {
            if(Count_one_trade_c<1)
               if(OrderSelect(buy3,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER3)
                     if(OrderSymbol()==Symbol()) 
                        if(OrderType()==OP_BUY) 
                           if(buyprice-Zone_Recovery_Area*pips>Bid)
                              int ticket4=OrderSend(Symbol(),OP_SELL,1.4,Bid,3,buyprice+TakeProfit*pips,buyprice-(TakeProfit*pips+Zone_Recovery_Area*pips),"4",MAGICNUMBER4,0,Red);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_c++;
           }

         //                                   TRADE 5 ZONE RECOVERY1 1.9 LOT BUY ITERATION
         // ===================================================================================================================== 
         for(int buy4=OrdersTotal()-1;buy4>=0;buy4--)
           {
            if(Count_one_trade_d<1)
               if(OrderSelect(buy4,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER4)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_SELL)
                           if(Ask>buyprice)
                              int ticket5=OrderSend(Symbol(),OP_BUY,1.9,Ask,3,buyprice-(TakeProfit*pips+Zone_Recovery_Area*pips),buyprice+TakeProfit*pips,"5",MAGICNUMBER5,0,Green);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_d++;
           }
         //                                   TRADE 6 ZONE RECOVERY1 2.5 LOT SELL ITERATION
         // =====================================================================================================================  
         for(int buy5=OrdersTotal()-1;buy5>=0;buy5--)
           {
            if(Count_one_trade_e<1)
               if(OrderSelect(buy5,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER5)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_BUY)
                           if(buyprice-Zone_Recovery_Area*pips>Bid)
                              int ticket6=OrderSend(Symbol(),OP_SELL,2.5,Bid,3,buyprice+TakeProfit*pips,buyprice-(TakeProfit*pips+Zone_Recovery_Area*pips),"6",MAGICNUMBER6,0,Red);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_e++;
           }
        }
     }

  }
//           
//===================================================================================================================== 

//+------------------------------------------------------------------+
//|    Lower 6 trades                      ZONE RECOVERY2            |
//+------------------------------------------------------------------+


void ZONE_RECOVERY2()
  {
//                                    TRADE 1+ 2 ZONE RECOVERY2 1 LOT SELL ITERATION + 1.4 LOT- BUY ITERATION
// ===================================================================================================================== 
   int iCount=0;
   int Count_one_trade_f=0;
   int Count_one_trade_g=0;
   int Count_one_trade_h=0;
   int Count_one_trade_i=0;
   int Count_one_trade_j=0;
   int Count_one_trade_k=0;


   if(OrdersTotal()==0)
      //Here you put your conditions for trading//
     {
      if(( iMA(Symbol(),0,Period1,0,0,0,1)<iMA(Symbol(),0,Period2,0,0,0,1)
         && Bid<iMA(Symbol(),0,Period2,0,0,0,0)))

         int ticket55=OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid+TakeProfit*pips+Zone_Recovery_Area*pips,Bid-TakeProfit*pips,"7",MAGICNUMBER7,0,Red);

     }

   if(OrdersTotal()>=1)
     {
      for(iCount=0;iCount<OrdersTotal();iCount++)
        {
         for(int sell1=OrdersTotal()-1;sell1>=0;sell1--)
           {
            if(Count_one_trade_f<1)
               if(OrderSelect(sell1,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER7)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_SELL)
                           if(OrderOpenPrice()+Zone_Recovery_Area*pips<Ask)
                              int ticket66=OrderSend(Symbol(),OP_BUY,1.4,Ask,3,OrderOpenPrice()-TakeProfit*pips,OrderOpenPrice()+TakeProfit*pips+Zone_Recovery_Area*pips,"8",MAGICNUMBER8,0,Green);
            buyprice=OrderOpenPrice();

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_f++;
           }
         //                                       TRADE 3 ZONE RECOVERY2 1 LOT BUY ITERATION
         // ===================================================================================================================== 
         for(int sell2=OrdersTotal()-1;sell2>=0;sell2--)
           {
            if(Count_one_trade_g<1)
               if(OrderSelect(sell2,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER8)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_BUY)
                           if(buyprice>Bid)
                              int ticket8=OrderSend(Symbol(),OP_SELL,1,Bid,3,buyprice+TakeProfit*pips+Zone_Recovery_Area*pips,buyprice-TakeProfit*pips,"9",MAGICNUMBER9,0,Red);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_g++;
           }
         //                                     TRADE 4 ZONE RECOVERY2 1.4 LOT SELL ITERATION
         // ===================================================================================================================== 
         for(int sell3=OrdersTotal()-1;sell3>=0;sell3--)
           {
            if(Count_one_trade_h<1)
               if(OrderSelect(sell3,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER9)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_SELL)
                           if(OrderOpenPrice()+Zone_Recovery_Area*pips<Ask)
                              int ticket9=OrderSend(Symbol(),OP_BUY,1.4,Ask,3,buyprice-TakeProfit*pips,buyprice+TakeProfit*pips+Zone_Recovery_Area*pips,"10",MAGICNUMBER10,0,Green);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_h++;
           }
         //                                   TRADE 5 ZONE RECOVERY2 1.9 LOT BUY ITERATION
         // ===================================================================================================================== 
         for(int sell4=OrdersTotal()-1;sell4>=0;sell4--)
           {
            if(Count_one_trade_i<1)
               if(OrderSelect(sell4,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER10)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_BUY)
                           if(buyprice>Bid)
                              int ticket10=OrderSend(Symbol(),OP_SELL,1.9,Bid,3,buyprice+TakeProfit*pips+Zone_Recovery_Area*pips,buyprice-TakeProfit*pips,"11",MAGICNUMBER11,0,Red);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_i++;
           }

         //                                   TRADE 6 ZONE RECOVERY2 2.5 LOT SELL ITERATION
         // ===================================================================================================================== 
         for(int sell5=OrdersTotal()-1;sell5>=0;sell5--)
           {
            if(Count_one_trade_j<1)
               if(OrderSelect(sell5,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==MAGICNUMBER11)
                     if(OrderSymbol()==Symbol())
                        if(OrderType()==OP_SELL)
                           if(OrderOpenPrice()+Zone_Recovery_Area*pips<Ask)
                              int ticket11=OrderSend(Symbol(),OP_BUY,2.5,Ask,3,buyprice-TakeProfit*pips,buyprice+TakeProfit*pips+Zone_Recovery_Area*pips,"12",MAGICNUMBER12,0,Green);

            exitbuys();//CLOSE BUY
            exitsells();//CLOSE SELL
            Count_one_trade_j++;
           }
        }
     }

  }
//=====================================================================================================================  

//+------------------------------------------------------------------+
//|                                      exitbuys()                  |
//+------------------------------------------------------------------+
void exitbuys()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderType()==OP_BUY && OrderMagicNumber()==MAGICNUMBER1)
            || (OrderType() == OP_BUY && OrderMagicNumber()==MAGICNUMBER3)
            || (OrderType() == OP_BUY && OrderMagicNumber()==MAGICNUMBER5)
            || (OrderType() == OP_BUY && OrderMagicNumber()==MAGICNUMBER8)
            || (OrderType() == OP_BUY && OrderMagicNumber()==MAGICNUMBER10)
            || (OrderType() == OP_BUY && OrderMagicNumber()==MAGICNUMBER12))
           {
            if((Ask==OrderTakeProfit()) || Bid==OrderStopLoss()) //If one order closed than close all
               result=OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);//actual order closing
            if(result!=true)//if it did not close
              {
               err=GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
              }

           }
        }

     }
  }
//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
//|                    exitsells()                                   |
//+------------------------------------------------------------------+
void exitsells()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {

         if((OrderType()==OP_SELL && OrderMagicNumber()==MAGICNUMBER2)
            || (OrderType() == OP_SELL && OrderMagicNumber()==MAGICNUMBER4)
            || (OrderType() == OP_SELL && OrderMagicNumber()==MAGICNUMBER6)
            || (OrderType() == OP_SELL && OrderMagicNumber()==MAGICNUMBER7)
            || (OrderType() == OP_SELL && OrderMagicNumber()==MAGICNUMBER9)
            || (OrderType() == OP_SELL && OrderMagicNumber()==MAGICNUMBER11))
           {
            if((Ask==OrderTakeProfit()) || Bid==OrderStopLoss())//If one order closed than close all
               result=OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);//actual order closing
            if(result!=true)//if it did not close
              {
               err=GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
              }

           }
        }

     }
  }
//+------------------------------------------------------------------+
