TradeHedgeCLOSEALL_v5





//+-----------------------+
//| Close Everything PAIR |
//|-----------------------+

// used for verbose error logging
#include <stdlib.mqh>


//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled

int init()
  {
   Comment(" ");
   Print("Init complete - script");
  }

//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart

int deinit()
  {
   Comment(" ");
   Print("DE-Init complete - script");
  }


//+-----------+
//| Main      |
//+-----------+
// Called EACH TICK and each Bar[]
int start()
  {

   CloseEverything();   

  } // start()


//+-----------------+
//| CloseEverything |
//+-----------------+
// Closes all OPEN and PENDING orders

int CloseEverything()
  {
   int    myTyp;

   int i;

   for( i=(OrdersTotal()-1); i>=0; i-- )
     {
      OrderSelect(i, SELECT_BY_POS);
   
      if(OrderMagicNumber()==200809241653)
        {
         switch( OrderType() )
           {
            //Close opened long positions
            case OP_BUY      :CloseBuy("TrendHedge close script  BUY");
            break;
      
            //Close opened short positions
            case OP_SELL     :CloseSell("TrendHedge close script SELL");
            break;
           }

        }//if
        
     } //for
  
  } // closeeverything


void CloseBuy (string myInfo)
  {
   int gle;
   int cnt;
   
   int loopcount=0;
   
   while(true)
     {
      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),2,White);
      gle=GetLastError();
      if(gle==0)
        {
         Print("CLOSE BUY "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit() );
         break;
        }
       else 
        {
         Sleep(500);
         RefreshRates();
         Print("-----ERROR----- CLOSE BUY PROFIT Bid="+MarketInfo(OrderSymbol(),MODE_BID)+" error="+gle+" "+ErrorDescription(gle));
        }

      loopcount++;
      if(loopcount>25)
        {
         Alert("Order failed to CLOSE - See Journal for errors");
         break;
        }

                     
     }//while
  
  }


void CloseSell (string myInfo)
  {
   int gle;
   int cnt;
   
   int loopcount=0;
   
   while(true)
     {
      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),2,Red);
      gle=GetLastError();
      if(gle==0)
        {
         Print("CLOSE SELL "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit() );
         break;
        }
      else 
        {
         Sleep(500);
         RefreshRates();
         Print("-----ERROR----- CLOSE SELL PROFIT Ask="+MarketInfo(OrderSymbol(),MODE_ASK)+" error="+gle+" "+ErrorDescription(gle));
        }
                    
      loopcount++;
      if(loopcount>25)
        {
         Alert("Order failed to CLOSE - See Journal for errors");
         break;
        }

                 
     }//while                 
  }








Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It Closes Orders by itself

Other Features:

It issuies visual alerts to the screen