//+------------------------------------------------------------------+
//|                                                   Profit_Monitor |
//|                               Copyright © 2009, Dennis Hamiilton |
//|                                              ramble_32@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Dennis Hamiilton"
#property link      "ramble_32@yahoo.com"

double PRT,ORP,RCR2=0;
bool Get_vars,EAC,RCR,CAO,DLA,CAN,SPD;
int OMN,MRB1,MRB2,MRB3,MRE1,MRE2,MRE3,MN1,MN2,MN3,MN4,MN5,MN6,MN7,MN8,MN9,MNA;

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- Reset Global to false
   GlobalVariableDel("PM_Script"); GlobalVariableDel("Profit_Monitor"); GlobalVariableDel("Suspend_Trades");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start(){
   MotherFunction();
  }
//----
void MotherFunction(){
   while(true){
      RefreshRates();
//----
   GlobalVariableSet("PM_Script_Loaded",true);
//---- Get Global Variables
   if(GlobalVariableCheck("Profit_Monitor")){ Get_vars=GlobalVariableGet("Profit_Monitor"); }
   if(Get_vars==true)
     {
      PRT=GlobalVariableGet("PM_P_Target");   ORP=GlobalVariableGet("PM_Overall_P");
      EAC=GlobalVariableGet("PM_Enable");     RCR=GlobalVariableGet("PM_Recurring");
      CAO=GlobalVariableGet("PM_Close_Open"); DLA=GlobalVariableGet("PM_Delete_All");
      CAN=GlobalVariableGet("PM_Close_Now");  SPD=GlobalVariableGet("PM_Suspend");
      //----
      MRB1=GlobalVariableGet("PM_MRB1"); MRB2=GlobalVariableGet("PM_MRB2");
      MRB3=GlobalVariableGet("PM_MRB3"); MRE1=GlobalVariableGet("PM_MRE1");
      //----
      MN1=GlobalVariableGet("PM_MN1"); MN2=GlobalVariableGet("PM_MN2"); MN3=GlobalVariableGet("PM_MN3");
      MN4=GlobalVariableGet("PM_MN4"); MN5=GlobalVariableGet("PM_MN5"); MN6=GlobalVariableGet("PM_MN6");
      MN7=GlobalVariableGet("PM_MN7"); MN8=GlobalVariableGet("PM_MN8"); MN9=GlobalVariableGet("PM_MN9");
      MNA=GlobalVariableGet("PM_MNA");
     }
//---- Count Open and Pending Orders
   double O_pr=0,T_pr,Profit_Target=PRT,Overall_Profit=ORP;
   int Total=OrdersTotal(),cnt=0,TBS_cnt=0,PBS_cnt=0,mag=0,MN0=567;
   for(cnt=0; cnt<=Total; cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
        {
         OMN=OrderMagicNumber();
         if((OMN>=MRB1&&OMN<=MRE1)||(OMN>=MRB2&&OMN<=MRE2)||OMN==MN1||OMN==MN2||OMN==MN3||OMN==MN4
         ||  OMN==MN5||OMN==MN6||OMN==MN7||OMN==MN8||OMN==MN9||OMN==MNA||OMN==MN0){
         //---- Count Orders and Total Profit
         if(OrderType()==OP_BUY||OrderType()==OP_SELL){ TBS_cnt+=1; O_pr+=OrderProfit(); }
         if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP){ PBS_cnt+=1; }
         if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT){ PBS_cnt+=1; } }
        }
     }
   GlobalVariableSet("PM_O_pr",O_pr); GlobalVariableSet("PM_T_pr",T_pr);
   if(EAC==false){ RCR2=0; } if(RCR==true||RCR2==0){ RCR2=1; }
   if(EAC==true&&RCR2==1){
   if(O_pr>=Profit_Target||T_pr>=Overall_Profit){ CAO=true; } }
   if(TBS_cnt==0||RCR2==2){ CAO=false; }
//---- Delete Open and Pending
   Total=OrdersTotal(); cnt=0;
   for(cnt=Total-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
        {
         OMN=OrderMagicNumber();
         if((OMN>=MRB1&&OMN<=MRE1)||(OMN>=MRB2&&OMN<=MRE2)||OMN==MN1||OMN==MN2||OMN==MN3||OMN==MN4
         ||  OMN==MN5||OMN==MN6||OMN==MN7||OMN==MN8||OMN==MN9||OMN==MNA||OMN==MN0){
         //---- Delete on Close All
         if(CAO==true||CAN==true){ T_pr+=O_pr;
         if(OrderType()==OP_BUY||OrderType()==OP_SELL){
         OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Magenta); } }
         //---- Delete on Close All or Suspend
         if(DLA==true||CAN==true||SPD==true){ if(OMN==MN1){ mag=MN1; } // Passing Test Variable
         if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP){ OrderDelete(OrderTicket()); }
         if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT){ OrderDelete(OrderTicket()); } }
        } }
     }
   if(T_pr>=Overall_Profit||RCR==false){ T_pr=0; RCR2=2; } // Overall Profit Reached
//---- Display Message Referring to Monitor EA
   string com1="SET with Profit Monitor EA on Separate Chart . . . Profit = ",com2=DoubleToStr(O_pr,2),
   com3="  Target = ",com4=DoubleToStr(PRT,2);
   Comment(com1,com2,com3,com4," . . . . . . ",com1,com2,com3,com4);
//----
   GlobalVariableSet("Suspend_Trades",SPD);
//----
   Sleep(100);
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+