//+------------------------------------------------------------------+
//|                                                 einshtein_01.mq4 |
//|                                                     Vorchunozavr |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Vorchunozavr"
#property link      ""

extern double  Profit         = 50;
extern int     Slippage       = 2;

double Lots;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if (OrdersTotal() > 0)
   {
      for(int i=OrdersTotal()-1; i>=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if (OrderSymbol() == Symbol())
            Lots  = OrderLots();
            {
               if (OrderType() == OP_BUY)
               {
                  if ((OrderProfit() + OrderSwap()) >= Profit)
                  OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);
                  {
                     if (CountSell() == 0 && CountBuy() == 0)
                     {
                        OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", 0, 0, Red);
                     }
                  }
               }
               if (OrderType() == OP_SELL)
               {
                  if ((OrderProfit()  + OrderSwap()) >= Profit)
                  OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
                  {
                     if (CountBuy() ==0 && CountSell() == 0)
                     {
                        OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", 0, 0, Blue);
                     }
                  }       
               }    
            }     
         }
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int CountBuy()
{
   int count = 0;
   
   for(int i = OrdersTotal()-1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
            count++;
      }
   }
   return(count);
}
//+------------------------------------------------------------------+
int CountSell()
{
   int count = 0;
   
   for(int i = OrdersTotal()-1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderType() == OP_SELL)
            count++;
      }
   }
   return(count);
}
//+------------------------------------------------------------------+

