//+------------------------------------------------------------------+
//|                                                        ToMuv.mq4 |
//|                                                       Серов Илья |
//|                                            https://vk.com/surf32 |
//+------------------------------------------------------------------+
#property copyright "Серов Илья"
#property link      "https://vk.com/surf32"
#property version   "1.00"
#property strict

extern double Lot     = 0.1;
extern int OrderCount =0;
extern int MALPeriod  = 20;
extern int MALShift   = 3;
extern int MAQPeriod  = 10;
extern int MAQShift   =-1;
extern int PeriodBP   = 20;
extern int PeriodCCI  = 20;
extern int StopLoss   = 50;
extern int TakeProfit = 150;
extern int Sleep = 3;

int Magic = 123;
int ticket;
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   double CCI = iCCI (NULL,0,PeriodCCI,PRICE_CLOSE,0);
   double BP  = iBullsPower (NULL,0,PeriodBP,PRICE_CLOSE,0);
   double MAL = iMA (NULL,0,MALPeriod,MALShift,MODE_SMA,PRICE_CLOSE,0);
   double MAQ = iMA (NULL,0,MAQPeriod,MAQShift,MODE_EMA,PRICE_CLOSE,0);
   
   if (CountBuy()==0 && MAQ>MAL)
  
     ticket=OrderSend (NULL,OP_BUY,Lot,Ask,Sleep,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,
       Magic,0,Green);
   
   if (CountSell()==0 && MAQ<MAL)
       ticket=OrderSend (NULL,OP_SELL,Lot,Bid,Sleep,Bid+StopLoss*Point,Bid-TakeProfit*Point,NULL,
         Magic,0,Red);
       
         
      
   }
//+------------------------------------------------------------------+
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);
 }