//+------------------------------------------------------------------+
//|                        Management.mq4                            |
//|                  Copyright © 2009, Mas Awoo                      |
//+------------------------------------------------------------------+
#include <stderror.mqh>
#include <stdlib.mqh>
#property copyright "copyright © 2009, Mas Awoo"
#property link      "mail to : mas_awoo@yahoo.com"

//--------------------------------------
extern string CloseOrdersFunction       = "_________________________________________";
extern string CloseOrdersFunctionCurrentPairs   = "_________________________________________";
extern bool   CloseOrders_At_Price     = False;
extern double CloseBuy_At_Price_Higher_Than  = 0.00000;
extern double CloseSell_At_Price_Lower_Than  = 0.00000;
extern bool   CloseBuyOrders           = False;
extern bool   CloseSellOrders          = False;
extern bool   CloseAllCurrentPairs     = False;
extern string CloseOrdersFunctionAllPairs       = "_________________________________________";
extern bool   CloseBuyAllPairs         = False;
extern bool   CloseSellAllPairs        = False;
extern bool   CloseAllProfitOrders     = False;
extern double MinProfit$               = 0.0; 
extern bool   CloseAllLossOrders       = False;
extern double MinLoss$                 = 0.0; 
extern bool   CloseAllPairs            = False;
extern string DeletePendingOrders      = "_________________________________________";
extern bool   DeleteBuyPendingOrders   = False;
extern bool   DeleteSellPendingOrders  = False;
extern bool   DeleteAllPendingOrders   = False;
extern string CloseALLOnEquityBasket   = "_________________________________________";
extern bool   CloseALLBasedOnEquityBasket    = False;
extern double PercentProfit                  = 100.00;
extern double DollarProfit                   = 100.00;
extern string CloseALLOrdersBasedOnEquity    = "_________________________________________";
extern bool   CloseALLBasedOnProfit          = False;
extern double PercentProfitFromBalanceToClose= 100.00;
extern double DollarProfitFromBalanceToClose = 100.00;
extern bool   CloseALLBasedOnLosses          = False;
extern double PercentLossFromBalanceToClose  = 100.00;
extern double DollarLossFromBalanceToClose   = 100.00;
extern string SettingOrders       = "_________________________________________";
extern bool   Hide_TP_SL          = true;
extern double TakeProfit          = 0.0; 
extern double StopLoss            = 0.0; 
extern bool   Use_TrailingStop    = False; 
extern double TrailingStop        = 17.0; 
extern double LockProfit          = 3.0; 
extern bool   Use_Step            = false;
extern double STEP                = 15.0;
extern bool   UseBreakEvenPoint   = false;
extern double BEP                 = 25.0;
//---------------------------------------
int init()
{
   return(0);
}
//--------------------------------------
int deinit()
{
   return(0);
}
//-------------------------------------
int start ()
 {
   double MyPoint;
   double SL, TP;
   if (Digits==4 || Digits==5){MyPoint=0.0001;}
   else if (Digits==2 || Digits==3){MyPoint=0.01;}
   double total, cnt;
   total=OrdersTotal();
int i;
int slippage=2;
double diff=AccountEquity()-AccountBalance();
//+------------------------------------------------------------------+
//| Manage Order                                                     |
//+------------------------------------------------------------------+

if (CloseALLBasedOnEquityBasket)
   {
   if (!GlobalVariableCheck("$Basket"))
      {
      GlobalVariableSet("$Basket",AccountBalance());
      }
   if ((PercentProfit>0.00 && PercentProfit<=((AccountEquity()-GlobalVariableGet("$Basket"))*100.00)/GlobalVariableGet("$Basket")) ||
       (DollarProfit>0.00 && DollarProfit<=(AccountEquity()-GlobalVariableGet("$Basket"))) )
      {CloseAllPairs=true;GlobalVariableDel("$Basket");}
    }

if (CloseALLBasedOnProfit)
   {
   if ((PercentProfitFromBalanceToClose>0.00 && PercentProfitFromBalanceToClose<=(diff*100.00)/AccountBalance()) ||
       (DollarProfitFromBalanceToClose>0.00 && DollarProfitFromBalanceToClose<=diff) )
      {CloseAllPairs=true;}
    }
if (CloseALLBasedOnLosses)
   {
   if ((PercentLossFromBalanceToClose>0.00 && -PercentLossFromBalanceToClose >= (diff*100.00)/AccountBalance()) ||
       (DollarLossFromBalanceToClose>0.00 && -DollarLossFromBalanceToClose >= diff) )
      {CloseAllPairs=true;}
    }
if (total==0)
   {
    CloseAllPairs=false;
    }

   for(cnt=0;cnt<total;cnt++)
     {
      if(OrderSelect(cnt, SELECT_BY_POS)==false)break;
      if(OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_BUY)  
           {
           if(Use_TrailingStop==true && TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*(TrailingStop+LockProfit))
                 {
                  if(OrderStopLoss()<Bid-MyPoint*(TrailingStop+LockProfit))
                    {
                     RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-MyPoint*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                    }
                 }
              }
           if(Use_Step==true && STEP>0)
              {
               if(Bid-OrderOpenPrice()>MyPoint*(STEP+STEP))
                 {
                  if(OrderStopLoss()<Bid-MyPoint*(STEP+STEP))
                    {
                     RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-MyPoint*STEP,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                    }
                 }
              }
           if(UseBreakEvenPoint==true)
             {                 
              if(OrderStopLoss()>=OrderOpenPrice())break;
              if(Bid-OrderOpenPrice()>MyPoint*BEP && OrderStopLoss()<OrderOpenPrice())
                 {
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
                  return(0);
                 }
             }
           }
         else if(OrderType()==OP_SELL) 
           {
          if(Use_TrailingStop==true && TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*(TrailingStop+LockProfit)))
                 {
                  if(OrderStopLoss()==0.0 || 
                     OrderStopLoss()>(Ask+MyPoint*(TrailingStop+LockProfit)))
                    {
                     RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                    }
                 }
              }
          if(Use_Step==true && STEP>0)
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*(STEP)))
                 {
                  if(OrderStopLoss()==0.0 || 
                     OrderStopLoss()>(Ask+MyPoint*(STEP+STEP)))
                    {
                     RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*STEP,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                    }
                 }
              }
           if(UseBreakEvenPoint==true)
              {                 
               if(OrderStopLoss()<=OrderOpenPrice() && OrderStopLoss()>0.0)break;
               if((OrderOpenPrice()-Ask)>(MyPoint*BEP) && OrderStopLoss()>OrderOpenPrice())
                 {
                  RefreshRates();
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
                  return(0);
                 }
              }
           }
        }
     }
 //----------------------------------------------+
 // TP && SL Close Function                      +
 //----------------------------------------------+   
if (Hide_TP_SL==false)
    { 
     if (StopLoss<MarketInfo(Symbol(),MODE_STOPLEVEL) || TakeProfit<MarketInfo(Symbol(),MODE_SPREAD))
     Hide_TP_SL=true;
    }
if (Hide_TP_SL)
   {
 	for(cnt=0;cnt<total;cnt++)
	   {
	    if(OrderSelect(cnt, SELECT_BY_POS)==false)break;
		 if (OrderSymbol()==Symbol())
		    {
		    if(OrderType()==OP_BUY)
		       {
		       if(Bid-OrderOpenPrice()>=TakeProfit*MyPoint && TakeProfit>0.0)
		         {
			      RefreshRates();
			      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		         return(0);
		         }
		       if(OrderOpenPrice()-Bid>=StopLoss*MyPoint && StopLoss>0.0)
		         {
			      RefreshRates();
			      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		         return(0);
		         }
	          }
		    if(OrderType()==OP_SELL)
		       {
		       if(OrderOpenPrice()-Ask>=TakeProfit*MyPoint && TakeProfit>0.0)
		         {
			      RefreshRates();
			      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		         return(0);
		         }
		       if(Ask-OrderOpenPrice()>=StopLoss*MyPoint && StopLoss>0.0)
		         {
			      RefreshRates();
			      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		         return(0);
		         }
	          }
	       }
       }
    }
  
if (Hide_TP_SL==false)
   { 
    if (StopLoss<MarketInfo(Symbol(),MODE_STOPLEVEL))SL=0; else SL=StopLoss;
    if (TakeProfit<MarketInfo(Symbol(),MODE_SPREAD))TP=0; else TP=StopLoss;
    for(cnt=0;cnt<total;cnt++)
       {
        if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
        if (SL==0.0 && TP==0.0)break;
        if (OrderSymbol()==Symbol())   
           {
            if (OrderType()==OP_BUY && OrderStopLoss()==0.0 && OrderTakeProfit()==0.0)  
               {
                RefreshRates();
                OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-MyPoint*SL,OrderOpenPrice()+MyPoint*TP,0,CLR_NONE);
                return(0);
               }
            if (OrderType()==OP_SELL && OrderStopLoss()==0.0 && OrderTakeProfit()==0.0)  
               {
                RefreshRates();
                OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+MyPoint*SL,OrderOpenPrice()-MyPoint*TP,0,CLR_NONE);
                return(0);
               }
            }
        }
    }  
//----------------------------------------------+
// Close Function                               +
//----------------------------------------------+   

if (CloseOrders_At_Price==true)
{
 if (CloseBuy_At_Price_Higher_Than>0.0)
    {
 	  for (cnt=0;cnt<total;cnt++)
	      {
	       if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
		    if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
		       {
		        if (OrderType()==OP_BUY && Bid>=CloseBuy_At_Price_Higher_Than)
		           {
			         RefreshRates();
			         OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		            return(0);
	              }
	          }
         } 
     }
 if (CloseSell_At_Price_Lower_Than>0.0)
    {
 	  for (cnt=0;cnt<total;cnt++)
	      {
	       if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
		    if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
		       {
		        if (OrderType()==OP_SELL && Ask<=CloseSell_At_Price_Lower_Than)
		           {
			         RefreshRates();
			         OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		            return(0);
	              }
	           }
          }
      }
}

if (CloseBuyOrders)
   {
 	 for (cnt=0;cnt<total;cnt++)
	     {
	      if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
		   if (OrderSymbol()==Symbol())
		      {
		       if (OrderType()==OP_BUY)
		          {
			        RefreshRates();
			        OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		           return(0);
	             }
	          }
         }
    }
if (CloseSellOrders)
   {
 	 for (cnt=0;cnt<total;cnt++)
	     {
	      if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
		   if (OrderSymbol()==Symbol())
		      {
		       if (OrderType()==OP_SELL)
		          {
			        RefreshRates();
			        OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
		           return(0);
	             }
	          }
         }
    }
   
//-----------------------------------------------------------------------------------------------

 if (CloseBuyAllPairs)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_BUY)
             {
              RefreshRates();
              OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
              return(0);
             }
         }
    }
//-----------------------------------------------------------------------------------------------
 if (CloseSellAllPairs)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_SELL)
             {
              RefreshRates();
              OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); 
              return(0);
             }
         }
    }
//-----------------------------------------------------------------------------------------------
 if (CloseAllCurrentPairs)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderSymbol()==Symbol())
             {
              if (OrderType()==OP_SELL || OrderType()==OP_BUY)
                 {
                  RefreshRates();
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
                  return(0);
                 }
             }
         }
    }
//-----------------------------------------------------------------------------------------------
 if (CloseAllProfitOrders)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_SELL || OrderType()==OP_BUY)
             {
              if (OrderProfit() + OrderSwap() >= MinProfit$)
                 {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
                 return(0);
                 }
             }
         }
    }
 //---------------------------------------------------------------------------------      
 if (CloseAllLossOrders)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_SELL || OrderType()==OP_BUY)
             {
              if (OrderProfit() + OrderSwap() <= MinLoss$)
                 {
                 RefreshRates();
                 OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
                 return(0);
                 }
             }
         }
    }
//-------------------------------------------------------------------------      
 if (CloseAllPairs)CloseAllOrders();
 
 /*
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_SELL || OrderType()==OP_BUY)
             {
              RefreshRates();
              OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
              return(0);
          if (OrderType()==OP_BUYSTOP||OrderType()==OP_BUYLIMIT||
              OrderType()==OP_SELLSTOP||OrderType()==OP_SELLLIMIT)
             {
              RefreshRates();
              OrderDelete(OrderTicket());
              return(0);
             }
             }
         }
    }*/
 //------
 if (DeleteBuyPendingOrders)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderSymbol()==Symbol()&&(OrderType()==OP_BUYSTOP||OrderType()==OP_BUYLIMIT))
             {
              RefreshRates();
              OrderDelete(OrderTicket());
              return(0);
             }
         }
    }
 //------
 if (DeleteSellPendingOrders)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderSymbol()==Symbol()&&(OrderType()==OP_SELLSTOP||OrderType()==OP_SELLLIMIT))
             {
              RefreshRates();
              OrderDelete(OrderTicket());
              return(0);
             }
         }
    }

 //------
 if (DeleteAllPendingOrders)
    {
     for (cnt=0;cnt<total;cnt++)
         {
          if (OrderSelect(cnt, SELECT_BY_POS)==false)break;
          if (OrderType()==OP_BUYSTOP||OrderType()==OP_BUYLIMIT||
              OrderType()==OP_SELLSTOP||OrderType()==OP_SELLLIMIT)
             {
              RefreshRates();
              OrderDelete(OrderTicket());
              return(0);
             }
         }
    }


 return(0);
}
//----------- Close All Orders, by Azmel
int CloseAllOrders()
{

int i;
int slippage=5;

   for (i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   //   if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
      {
         switch(OrderType())
         {
            case OP_BUY      : OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); break;
            case OP_SELL     : OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE); break;
            case OP_BUYLIMIT :
            case OP_SELLLIMIT:
            case OP_BUYSTOP  :
            case OP_SELLSTOP : OrderDelete(OrderTicket());                                                break;
         }
      }
   }
   return(0);
}
//----------- end of Close All Orders subroutine.

//--------------------------

