//+------------------------------------------------------------------+
//|                                                    машки.mq4 |
//|                                                         Samuliak |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Samuliak"
#property link      ""

extern int    TakeProfit  = 60;
extern double Lots        = 0.1;
extern int    StopLoss    = 60;
extern int    Magic       = 123;
extern int    period_1    = 10;
extern int    period_2    = 40;
extern int    period_3    = 375;
extern bool   CloseRevers = true;
extern int    PipStop     = 50;
extern int    TakeProfit2 = 60;
extern int    StopLoss2   = 60;


int ticket;
double SL, TP, TP2, SL2;
double MA10, MA11, MA20, MA21, MA3;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
     if(Digits == 3 || Digits == 5)
     {
        TakeProfit  *= 10;        
        StopLoss    *= 10;
        Lots        *= 10;    
        PipStop     *= 10;
        TakeProfit2 *= 10; 
        StopLoss2   *= 10; 
     }
     



   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
   double MA10 = iMA(NULL,0,period_1,0,MODE_EMA,PRICE_OPEN,0);
   double MA11 = iMA(NULL,0,period_1,0,MODE_EMA,PRICE_OPEN,1);
   double MA20 = iMA(NULL,0,period_2,0,MODE_LWMA,PRICE_OPEN,0);
   double MA21 = iMA(NULL,0,period_2,0,MODE_LWMA,PRICE_OPEN,1);   
   double MA3  = iMA(NULL,0,period_3,0,MODE_LWMA,PRICE_OPEN,0); 
   
      
   
   if(MA10>MA20 && MA11<MA21 && MA20>MA3 && CountBuy() <= 1)
   
   {
      TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
      SL = NormalizeDouble(Ask - StopLoss * Point, Digits);
      
      ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, 0, 0, "123", Magic, 0, Blue);
      if(ticket > 0)
      {   
         if (CloseRevers) CLOSEORDER(OP_SELL);
         if (OrdersTotal()==0)

         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, Black);
      }   
   }
      else if(Ask <= MA3 && High[2] > MA3 && High[1] > MA3 && CountBuyStop() <= 1)
   {
      TP2 = NormalizeDouble(Ask + TakeProfit2 * Point, Digits);
      SL2 = NormalizeDouble(Ask - StopLoss2 * Point, Digits);
   
      ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, NormalizeDouble(Ask + PipStop * Point, Digits), 5, 0, 0, "999", Magic, 0, Black);
      if(ticket > 0)
      {
         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL2, TP2, 0, Black);
      }
   }
      else if(Ask <= MA3 && High[2] > MA3 && High[1] > MA3 && CountSellLimit() <= 1)
   {
      ticket = OrderSend(Symbol(), OP_SELLLIMIT, Lots, NormalizeDouble(Ask - PipStop * Point, Digits), 5, 0, 0, "000", Magic, 0, Black);
      if(ticket > 0)
      {
         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL2, TP2, 0, Black);
      }
     
   }
   
   
   
   
   if(MA10<MA20 && MA11>MA21 && MA20<MA3 && CountSell() <= 1)
   {
      TP = NormalizeDouble(Bid - TakeProfit * Point, Digits);
      SL = NormalizeDouble(Bid + StopLoss * Point, Digits);
      
      if (CloseRevers) CLOSEORDER(OP_BUY);
      if (OrdersTotal()==0)      
      ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, 0, 0, "321", Magic, 0, Red);
      if(ticket > 0)
      {
         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, Black);
      }
   }  
   else if(Bid >= MA3 && High[2] < MA3 && High[1] < MA3 && CountSellStop() <= 1)
   {
      TP2 = NormalizeDouble(Ask + TakeProfit2 * Point, Digits);
      SL2 = NormalizeDouble(Ask - StopLoss2 * Point, Digits);
   
      ticket = OrderSend(Symbol(), OP_SELLSTOP, Lots, NormalizeDouble(Bid - PipStop * Point, Digits), 5, 0, 0, "999", Magic, 0, Black);
      if(ticket > 0)
      {
         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL2, TP2, 0, Black);
      }
   }
   else if(Bid >= MA3 && High[2] < MA3 && High[1] < MA3 && CountBuyLimit() <= 1)
   {
      ticket = OrderSend(Symbol(), OP_BUYLIMIT, Lots, NormalizeDouble(Bid + PipStop * Point, Digits), 5, 0, 0, "000", Magic, 0, Black);
      if(ticket > 0)
      {
         if(OrderSelect(ticket, SELECT_BY_TICKET) == true)
         OrderModify(ticket, OrderOpenPrice(), SL2, TP2, 0, Black);
      }
     
   }
     
     
      return(0);
  }
//+------------------------------------------------------------------+
int CountBuy()
{
      int count = 0;
   for (int trade = OrdersTotal()-1; trade >= 0; trade --)
      {
         OrderSelect(trade, SELECT_BY_POS);
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               {
               if (OrderType() == OP_BUY)
                  count++;
               }
      }
return (count);
}

//+------------------------------------------------------------------+
int CountSell()
{
      int count = 0;
            for (int tr= OrdersTotal()-1; tr >= 0; tr --)
               {
                   OrderSelect(tr, SELECT_BY_POS);
                    if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                     {
                       if (OrderType() == OP_SELL)
                        count++;
                     }
                 }
return (count);
}
//--------------------------------------------------------------------
void CLOSEORDER(int ord)
{
   for (int i=0; i<OrdersTotal(); i++)
   {                                               
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==123)
         {
            if (OrderType()==OP_BUY && ord==OP_BUY)
               OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,CLR_NONE);
            if (OrderType()==OP_SELL && ord==OP_SELL)
               OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,CLR_NONE);
         }
      }   
   }
}

//+------------------------------------------------------------------+
int CountBuyStop()
{
      int count = 0;
   for (int tr = OrdersTotal()-1; tr >= 0; tr --)
      {
         OrderSelect(tr, SELECT_BY_POS);
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               {
               if (OrderType() == OP_BUYSTOP)
                  count++;
               }
               }
return (count);
}

//+------------------------------------------------------------------+
int CountSellStop()
{
      int count = 0;
            for (int tr= OrdersTotal()-1; tr >= 0; tr --)
               {
                   OrderSelect(tr, SELECT_BY_POS);
                    if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                     {
                       if (OrderType() == OP_SELLSTOP)
                        count++;
                     }
                 }
return (count);
}

//+------------------------------------------------------------------+
int CountBuyLimit()
{
      int count = 0;
   for (int trade = OrdersTotal()-1; trade >= 0; trade --)
      {
         OrderSelect(trade, SELECT_BY_POS);
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               {
               if (OrderType() == OP_BUYLIMIT)
                  count++;
               }
      }
return (count);
}
     

//+------------------------------------------------------------------+
int CountSellLimit()
{
      int count = 0;
            for (int tr= OrdersTotal()-1; tr >= 0; tr --)
               {
                   OrderSelect(tr, SELECT_BY_POS);
                    if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                     {
                       if (OrderType() == OP_SELLLIMIT)
                        count++;
                     }
                 }
return (count);
}
//+------------------------------------------------------------------+

