//+------------------------------------------------------------------+
//|                              MA_PriceTouch_OrdersCloseAll_EA.mq4 |
//|                                          Copyright 2022 mql5.com |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
  
enum enCloseMode
  {
   CloseAll,          // Close All Orders on Platform
   CloseBySymbol,     // Close All Orders on Symbol
  };

input enCloseMode                                            Close_Mode                  = CloseBySymbol;   // Select Close Orders Mode:
input double                                                 Close_All_Price             = 0;               // Price to close all orders when MA touches:
input ENUM_TIMEFRAMES                                        MA_Timeframe                = PERIOD_CURRENT;  // Timeframe for MA:
input ENUM_MA_METHOD                                         MA_Method                   = MODE_SMA;        // Method of MA calculations:
input ENUM_APPLIED_PRICE                                     MA_Price                    = PRICE_CLOSE;     // Applied price fro MA calculations:
input int                                                    MA_Period                   = 10;              // Period of MA:
input int                                                    Apply_To_Candle             = 0;               // MA from which candle must touch:
input int                                                    Study_Candles               = 3;               // How many candles back to study MA:

double MA_Base,MA_Prev;
string errorComment = "MA_PriceTouch_OrdersCloseAll_EA";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
     MA_Base=iMA(_Symbol,MA_Timeframe,MA_Period,0,MA_Method,MA_Price,Apply_To_Candle);
     MA_Prev=iMA(_Symbol,MA_Timeframe,MA_Period,0,MA_Method,MA_Price,Apply_To_Candle+Study_Candles-Apply_To_Candle);
     
     if(MA_Prev<MA_Base&&
        MA_Base>=Close_All_Price&&
        Close_All_Price>0)
       { 
        if(Close_Mode==CloseAll)
        OrdersCloseAll();
        else
        if(Close_Mode==CloseBySymbol)
        OrdersCloseAllBySymbol();
       }
     
     if(MA_Prev>MA_Base&&
        MA_Base<=Close_All_Price&&
        Close_All_Price>0)
       { 
        if(Close_Mode==CloseAll)
        OrdersCloseAll();
        else
        if(Close_Mode==CloseBySymbol)
        OrdersCloseAllBySymbol();
       }
   
  }
//+------------------------------------------------------------------+
int OrdersCloseAll()
   {
  int total = OrdersTotal();
  bool result = false; 
  for(int cnt=total-1;cnt>=0;cnt--)
   {
    if(OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES))
     {
    int ordertype   = OrderType();
    switch(ordertype)
     {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), Bid, 0, clrNONE );
                          break;
      case OP_BUYSTOP   : result = OrderDelete( OrderTicket(), clrNONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), Ask, 0, clrNONE );
                          break;
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket(), clrNONE );
                          
     }
     }
    
    if(result == false)
     {
      Print(errorComment," Order ", OrderTicket(), " close failed - error code:", GetLastError()); 
      Sleep(500);
     }
     else
     {
      Print(errorComment," Order ", OrderTicket(), " close success.");
      PlaySound("ok.wav");
     }  
   }
   
   return(0);
   }
//+------------------------------------------------------------------+
int OrdersCloseAllBySymbol()
   {
  int total = OrdersTotal();
  bool result = false; 
  int ordertype;
  for(int cnt=total-1;cnt>=0;cnt--)
   {
    if(OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES))
    ordertype   = OrderType();
    if(OrderSymbol()==Symbol())
     {
    switch(ordertype)
     {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), Bid, 0, clrNONE );
                          break;
      case OP_BUYSTOP   : result = OrderDelete( OrderTicket(), clrNONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), Ask, 0, clrNONE );
                          break;
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket(), clrNONE );
                          
     }
    
    if(result == false)
     {
      Print(errorComment," Order ", OrderTicket(), " close failed - error code:", GetLastError()); 
      Sleep(500);
     }
     else
     {
      Print(errorComment," Order ", OrderTicket(), " close success.");
      PlaySound("ok.wav");
     }  
    }
   }
   
   return(0);
   }
//+------------------------------------------------------------------+
