//+------------------------------------------------------------------+
//|                                                         Тест.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double  Lots            =  0.1;
extern int     TrailingStop    =  20;
extern int     TrailingStep    =  10;
extern int     Magic           =  123;
extern int     Slippage        = 5;
extern int     TakeProfit      = 20;
extern int     StopLoss        = 40;

extern int     MAPeriod        =  12;
extern int     MAShift         =  1;
extern int     ADXperiod       =  5;

// Переменные которые учавствуют в советнике

double MAPrice;
double MA2;
double SL; 
double Stop;
double Profit;
double ADX_m5;
double ADX_m5_2;
double ADX_h1;
double ADX_plus_M5;
double ADX_minus_M5;
double ADX_plus_H1;
double ADX_minus_H1;
double ADX_plus_H1_0;
double ADX_minus_H1_0;
double ADX_plus_M5_2;
double ADX_minus_M5_2;
double ADX_plus_M5_3;
double ADX_minus_M5_3;
                                                   
datetime    TimePrev;
int         ticket;
int         total;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      if(Digits == 3 || Digits == 5)
      {
         TrailingStop *=1;
         TrailingStep *=1;
         Slippage     *=1;
      }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
int start()
{
   if(TimePrev == Time[0]) return(0);                                               // проверка закрыласт ли свеча
   
   TimePrev = Time [0];
   
   int CountB = CountBuy();
   int CountS = CountSell();
         
   MAPrice = iMA (Symbol(),0,MAPeriod,MAShift,0,0,1);                            // параметры MA
   MA2     = iMA (Symbol(),PERIOD_H1,12,MAShift,0,0,1);
   
   ADX_m5         = iADX (NULL, PERIOD_M5, ADXperiod, 0, 0, 1);                          // параметры ADX
   ADX_m5_2       = iADX (NULL, PERIOD_M5, ADXperiod, 0, 0, 2);
   ADX_plus_M5    = iADX (NULL, PERIOD_M5, ADXperiod, 0, 1, 1);      
   ADX_minus_M5   = iADX (NULL, PERIOD_M5, ADXperiod, 0, 2, 1);
   ADX_plus_M5_2  = iADX (NULL, PERIOD_M5, ADXperiod, 0, 1, 2);      
   ADX_minus_M5_2 = iADX (NULL, PERIOD_M5, ADXperiod, 0, 2, 2);
   ADX_plus_M5_3  = iADX (NULL, PERIOD_M5, ADXperiod, 0, 1, 3);      
   ADX_minus_M5_3 = iADX (NULL, PERIOD_M5, ADXperiod, 0, 2, 3); 
    
   ADX_h1         = iADX (NULL, PERIOD_H1, ADXperiod, 0, 0, 1);
   ADX_plus_H1    = iADX (NULL, PERIOD_H1, ADXperiod, 0, 1, 1);
   ADX_plus_H1_0  = iADX (NULL, PERIOD_H1, ADXperiod, 0, 1, 0);
   ADX_minus_H1   = iADX (NULL, PERIOD_H1, ADXperiod, 0, 2, 1);
   ADX_minus_H1_0 = iADX (NULL, PERIOD_H1, ADXperiod, 0, 2, 0);
         
   if (CountB + CountS == 0 && Close[1] > MAPrice && Close[1] > MA2 && ADX_m5 > 55 && ADX_m5_2 <55 && 
       ADX_plus_M5 > ADX_minus_M5 && ADX_plus_H1 > ADX_minus_H1 && ADX_minus_H1_0 < ADX_minus_H1)                       // условия для входа в покупку
   {      
      Stop   = NormalizeDouble(Ask - StopLoss*Point, 5);
      Profit = NormalizeDouble(Ask + TakeProfit*Point, 5);
      ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Stop, Profit, "", Magic, 0, Blue);
            
      if  (ticket < 0) Print ("Не открылся ордер на покупку");
    }    

    if (CountB + CountS == 0 && Close[1] < MAPrice && Close[1] < MA2 && ADX_m5 > 55 && ADX_m5_2 <55 &&  
        ADX_plus_M5 < ADX_minus_M5 && ADX_plus_H1 < ADX_minus_H1 && ADX_plus_H1_0 < ADX_plus_H1)                            // условия для входа на продажу
    {
       Stop   = NormalizeDouble(Bid + StopLoss*Point, 5);
       Profit = NormalizeDouble(Bid - TakeProfit*Point, 5);   
       ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Stop, Profit, "", Magic, 0, Red);  

       if  (ticket < 0) Print ("Не открылся ордер на продажу");
   }
   
   Trailing();
        
   return(0);    
}
//+------------------------------------------------------------------+

void Trailing()
{
   for(int i=0; i<OrdersTotal();  i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)  
         {
            if(OrderType() == OP_BUY) 
            {
               if(Bid - OrderOpenPrice() > TrailingStop*Point)
               {
                  if(OrderStopLoss() < Bid - (TrailingStop + TrailingStep)*Point)
                  {
                     SL = NormalizeDouble(Bid - TrailingStop*Point, Digits);
                     if(OrderStopLoss() != SL)
                     bool check = OrderModify(OrderTicket(),OrderOpenPrice(), SL, 0, 0);
                  }
               }   
            }
            
            if(OrderType() == OP_SELL) 
            {
               if(OrderOpenPrice() - Ask > TrailingStop*Point)
               {
                  if(OrderStopLoss() > Ask + (TrailingStop + TrailingStep)*Point)
                  {
                     SL = NormalizeDouble(Ask + TrailingStop * Point, Digits);
                     if(OrderStopLoss() != SL)
                     bool check = OrderModify(OrderTicket(),OrderOpenPrice(), SL, 0, 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() && OrderMagicNumber() == Magic &&  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() && OrderMagicNumber() == Magic &&  OrderType() == OP_SELL)
            count ++;
         
      }
   }
   return(count);
}

//+------------------------------------------------------------------+

