//+------------------------------------------------------------------+
//|                                                     MA NEW 3.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



// datetime LastActionTime = 0;
// extern int stoploss = 100;
// extern double lot = 0.03;
extern int magic = 1234;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   // for (int i=OrdersTotal()-1; i >=0; i--)
    //  {    
     // if (LastActionTime != Time[0])
     // {
            // Comparing LastActionTime with the current starting time for the candle.
         
            // current chart, current period, 7 candles, no shift, simple, close price
            double SlowMovingAverage = iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
          
            // current chart, current period, 2 candles, no shift, simple, close price
            double FastMovingAverage = iMA(NULL,0,21,0,MODE_SMA,PRICE_CLOSE,0);
            
          
            // if the fast SMA is now above
            if (FastMovingAverage > SlowMovingAverage)
            {
              // CloseSell();     
               if (OrdersTotal()<1)
               {       
                  openBuy(); 
               } 
            }  RefreshRates();
            
          //   } 
          //   for (int x=OrdersTotal()-1; x >=0; x--)
    //  {  
            // if the fast SMA is now above
            if (FastMovingAverage < SlowMovingAverage)
            {
               //CloseBuy();
               if (OrdersTotal()<1)
               {     
               openSell(); 
               }
            } RefreshRates();
            // break;
       //     LastActionTime = Time[0];
         // }  
    //  }    
  }
//+------------------------------------------------------------------+


void openBuy()
   {
      int buyticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Ask-500*Point,0,NULL,magic,0,Green); 
   }
   
void openSell()
   {
      int sellticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,3,Bid+500*Point,0,NULL,magic,0,Red);
   }
 
  void CloseSell()
  {
      // Go through all the open orders
      for (int i=OrdersTotal()-1; i >=0; i--)
      {
          // select an order
          OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
          
          // get the order currency pair
          string CurrencyPair=OrderSymbol();
          
          // if chart and order currency are equal
          if(_Symbol==CurrencyPair)
          
          // check if it is a sell position
          if(OrderType()==OP_SELL)
          {
               // Close the current position
               OrderClose(OrderTicket(),OrderLots(),Ask,3,NULL);
          }
          
      } // END FOR
      
  } //END FUNCTION
  
  
    void CloseBuy()
  {
      // Go through all the open orders
      for (int i=OrdersTotal()-1; i >=0; i--)
      {
          // select an order
          OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
          
          // get the order currency pair
          string CurrencyPair=OrderSymbol();
          
          // if chart and order currency are equal
          if(_Symbol==CurrencyPair)
          
          // check if it is a sell position
          if(OrderType()==OP_BUY)
          {
               // Close the current position
               OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);
          }
          
      } // END FOR
      
  } //END FUNCTION