extern double Lots          = 0.1;   // Если UseMM = false, то работать с постоянным лотом 

extern bool   UseMM         = true;
extern string MMTypeDescr   = "1 - balance; 2 - equity; 3 - free margin; any other value - equal to Lot parameter";
extern int    MMType        = 1;
extern double LotsFor1000   = 0.1;

extern int    MagicNumber   = 123;
extern string comment       = "Still Basketting";

double mLots;
int timeprev = 0,
    ticket;

double Ex0, Ex1, Ex2, Ex3;
double Tr0, Tr1, Tr2, Tr3;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
  
   // работаем на новом баре
   if (timeprev == Time[0]) return (0);
   timeprev = Time[0];
  

   Ex0    = iCustom(Symbol(), 0, "VQzz2", 0, 1); // Sell
   Ex1    = iCustom(Symbol(), 0, "VQzz2", 1, 1); // Buy

   Tr0    = iCustom(Symbol(), 0, "Heiken_Ashi_Smoothed", 0, 1); // Sell
   Tr2    = iCustom(Symbol(), 0, "Heiken_Ashi_Smoothed", 2, 1); // Sell
   Tr1    = iCustom(Symbol(), 0, "Heiken_Ashi_Smoothed", 1, 1); // Buy
   Tr3    = iCustom(Symbol(), 0, "Heiken_Ashi_Smoothed", 3, 1); // Buy
   
      
  
   if (UseMM) mLots = GetLots(); else mLots = Lots;
   
   if (CountSell() == 0 && Ex0 == 1 && Ex1 == 0 && Close[1] < Tr0 && Close[1] < Tr2)
   {
      if (CountBuy() > 0)
        CloseAll();

      
      OrderSend(Symbol(), OP_SELL, mLots, Ask, 300, 0, 0, comment, MagicNumber, 0, Red);
   }

   if (CountBuy() == 0 && Ex1 == 1 && Ex0 == 0 && Close[1] > Tr1 && Close[1] > Tr3)
   {
      if (CountSell() > 0)
        CloseAll();

      ticket = OrderSend(Symbol(), OP_BUY, mLots, Ask, 300, 0, 0, comment, MagicNumber, 0, Blue);
   }
   
           
   return(0);
}
//---------------------------------------------------------------------------------------------------------
int CountBuy() 
{
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) 
   {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_BUY) count++;
   }
   return (count);
}
//---------------------------------------------------------------------------------------------------------
int CountSell() 
{
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) 
   {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL) count++;
   }
   return (count);
}
//---------------------------------------------------------------------------------------------------------
void CloseAll() 
{
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) 
   {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) 
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) 
         {
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
         }
         Sleep(1000);
      }
   }
}
//---------------------------------------------------------------------------------------------------------
double GetLots() 
{
   double clots = Lots;
   if (UseMM) 
   {
      if (MMType == 1) clots = AccountBalance()    / 1000.0 * LotsFor1000;
      if (MMType == 2) clots = AccountEquity()     / 1000.0 * LotsFor1000;
      if (MMType == 3) clots = AccountFreeMargin() / 1000.0 * LotsFor1000;
   }
   clots = MathMax(clots, MarketInfo(Symbol(), MODE_MINLOT));
   clots = MathMin(clots, MarketInfo(Symbol(), MODE_MAXLOT));
   clots = NormalizeDouble(clots, 2);
   
   return (clots);
}
//---------------------------------------------------------------------------------------------------------

