//+------------------------------------------------------------------+
//|                                                EBCloseProfit.mq4 |
//|                                      Copyright © 2011, Eric BOHN |
//|                                                                  |
//| Dans outil/options/expert consultant activer                     |
//| "autoriser le trading direct"                                    | 
//|                                                                  |
//| Recherche tous les ordres ouverts et ferme ceux dont le profit   |
//| est supérieur à zéro (ou montant spécifiée)                      |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2011, Eric BOHN"
#property link      ""
#property show_inputs

//#include <stderror.mqh>
#include <stdlib.mqh> //  Nécessaire pour ErrorDescription

#include <WinUser32.mqh>

#import "user32.dll"
  int     MessageBoxA(int hWnd ,string szText,string szCaption,int nType);
  
extern double Fermer_si_profit_supérieur_à_=0.00;

string type_ordre(int ordertype)
{
  switch(ordertype)
  {
    case 0  : return("OP_BUY");
              break;
    case 1  : return("OP_SELL");
              break;
    case 2  : return("OP_BUYLIMIT");
              break;
    case 3  : return("OP_SELLLIMIT");
              break;
    case 4  : return("OP_BUYSTOP");
              break;
    case 5  : return("OP_SELLSTOP");
              break;
    default : return("Erreur");
              break;
  } 
}
  
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
     string msg="Positions ouvertes :\n";
     int nbre_ordres = OrdersTotal();
     int ticket[500];
     double lot[500];
     int ordertype[500];

     int i=0;
     double prix=0.0;
     int slippage=0;
     double profit=0.0;
     int erreur=0;
     
     Comment("Script EBCloseProfit en action\n");
     
     if (nbre_ordres < 500)
     {               
        // Mise en tableau des ordres ouverts     
        for (i=0; i<nbre_ordres;i++)
        {
          if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
          {
            ticket[i]=OrderTicket();
            lot[i]=OrderLots();
            ordertype[i]=OrderType();
         
            msg=msg+" "+i+"  "+OrderTicket()+" "+type_ordre(OrderType())+" lot="+DoubleToStr(OrderLots(),1)+" expiration "+OrderExpiration()+" profit "+DoubleToStr(OrderProfit(),2)+"€\n";    
          }
          else
          {
            ticket[i]=0;
            lot[i]=0.0;
            ordertype[i]=0;
         
            msg=msg+" "+i+" OrderSelect failed error code is "+GetLastError()+" profit "+DoubleToStr(OrderProfit(),2)+"€\n";   
          }
        }
        msg=msg+"\nPositions fermées avec profit :\n";


        // Fermeture des positions si profit
        for (i=0; i<nbre_ordres;i++)
        {
          if (ticket[i]>0)
          {
             if (OrderSelect(ticket[i],SELECT_BY_TICKET)==true)
             {
               profit=OrderProfit();
            
               if (profit>Fermer_si_profit_supérieur_à_)
               {
                 if (ordertype[i]==OP_BUY || ordertype[i]==OP_BUYLIMIT || ordertype[i]==OP_BUYSTOP)
                 {
                   prix=Bid;
                   if (OrderClose(ticket[i],lot[i],prix,slippage,CLR_NONE)==true)
                   {
                     msg=msg+"Fermeture ticket["+i+"]="+ticket[i]+" : "+lot[i]+" au prix de "+prix+"\n";
                   }
                   else
                   {
                     erreur=GetLastError();
                     msg=msg+i+" : Erreur fermeture="+erreur+" : "+ErrorDescription(erreur)+"\n";
                   } 
                 }
                 else // ordertype[i]== sell ! 
                 {
                   prix=Ask;
                   if (OrderClose(ticket[i],lot[i],prix,slippage,CLR_NONE)==true)
                   {
                     msg=msg+"Fermeture ticket["+i+"]="+ticket[i]+" : "+lot[i]+" au prix de "+prix+"\n";
                   }
                   else
                   {
                     erreur=GetLastError();
                     msg=msg+i+" : Erreur fermeture="+erreur+" : "+ErrorDescription(erreur)+"\n";
                   }               
                 }
               }         
             }
             else
             {
               msg=msg+"orderselect ticket["+i+"] retourne false !\n";
             }
          }
          else
          {
             msg=msg+"ticket["+i+"] non > 0 :"+ticket[i]+"\n";
          }
        }
     }
     else
     {
        msg="Le nombre de positions dépasse l''initialisation des tableaux : 500 maxi !\n"; 
     }
        
     MessageBoxA(0, msg,"EBclose",0);
     
     Comment("Fin du script EBCloseProfit\n");
     
     return(0);
  }
//+------------------------------------------------------------------+