//+------------------------------------------------------------------+
//|                                                    Todays PL.mq4 |
//|                                                           GumRai |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "none"

#property indicator_chart_window
//--- input parameters
extern color     TextColour = Red;
/*
Initial Total Balance:   $
Today Total Bance : %______ $_____
Today current profit: % _____ $ ___ done
Today highest profit: % _____ $ ___ done
Today lowest loss:     % _____ $ ____ done
Total drawdown account:% ______ $ ____ (This drawdown is the maximum real open floating, not when closing trades),
 this value never reduces or decreases.
*/
int WeekDay;
string idgam="MessageTodaysPL";
double init_depo;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   WeekDay=8;

   label(1,"",DoubleToStr(init_depo,2),clrLime,"Deposit = ");
   label(2,"",DoubleToStr(0,2),TextColour,"Today Total Equity = ");
   label(3,"",DoubleToStr(0,2),TextColour,"Today's Current Profit = ");
   label(4,"",DoubleToStr(0,2),TextColour,"Today's Highest Profit = ");
   label(5,"",DoubleToStr(0,2),TextColour,"Today's Lowest Loss = ");
   label(6,"",DoubleToStr(0,2),TextColour,"Yesterday's Lowest Loss = ");
   label(7,"",DoubleToStr(0,2),TextColour,"Total drawdown account = ");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   hapusff(idgam);
   return 0;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void hapusff(string name="")
  {
   for(int a=ObjectsTotal()-1; a>=0; a--)
     {
      string ns=ObjectName(a);
      if(StringFind(ns,name,0)>-1)
         ObjectDelete(ns);
     }
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
color ccol(double a)
  {
   return a>0?clrLime:a<0?clrRed:TextColour;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int tmin;
int start()
  {
   if(init_depo==0)
     {
      for(int b=0; b<OrdersHistoryTotal(); b++)
        {
         if(OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
           {
            if(OrderType()==6)
               init_depo+= OrderProfit();
           }
        }
      if(init_depo==0)
        {
         Alert("deposit not detected");
         init_depo=AccountBalance();
         if(init_depo==0)
           {
            Alert("AccountBalance 0");
            return -1;
           }
        }
      label(1,"",DoubleToStr(init_depo,2),clrLime,"Deposit = ");

     }
//----


   double currentprofit=0,currentprofit_his=0;
   static double highestprofit;
   static double lowestprofit;
   static double yesterdaylowestprofit;
   static double balance;
   static double perballoss;
   static double yesterdayperballoss;
   static double dd;
   static double ddp;
   if(WeekDay!= DayOfWeek())
     {
      yesterdaylowestprofit=lowestprofit ;
      yesterdayperballoss=perballoss;
      highestprofit = 0;
      lowestprofit = 0;
      WeekDay = DayOfWeek();
      balance=init_depo;
      label(4,"0",DoubleToStr(currentprofit,2),ccol(currentprofit),"Today's Current Profit = ");
      label(5,"0",DoubleToStr(currentprofit,2),TextColour,"Today's Highest Profit = ");
      label(6,DoubleToStr(yesterdayperballoss,2),DoubleToStr(yesterdaylowestprofit,2),clrYellow,"Yesterday Lowest Loss = ");
     }
   double percentbal=balance>0?(AccountEquity()-balance)/balance*100:0;
   label(2,DoubleToStr(percentbal,2),DoubleToStr(AccountEquity(),2),ccol(AccountEquity()-balance),"Today Total Equity = ");

   datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_D1 * 60));


   for(int a = OrdersHistoryTotal()-1; a>=0; a--)
     {
      if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderOpenTime() >= Midnight)
           {
            currentprofit_his +=OrderProfit() +OrderSwap() +OrderCommission()  ;
           }

        }

      else
         continue;
     }

   for(a = OrdersTotal()-1; a>=0; a--)
     {
      if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
        {
         //  if(OrderOpenTime() >= Midnight)
           {
            currentprofit += OrderProfit() +OrderSwap() +OrderCommission()  ;
           }

        }

      else
         continue;
     }
   double ttl_pro=   currentprofit+currentprofit_his;
   double percurprofit=balance==0?0:currentprofit/balance*100;

   label(3,DoubleToStr(percurprofit,2),DoubleToStr(currentprofit,2),ccol(currentprofit),"Today's Current Profit = ");
   if(currentprofit > highestprofit)
     {
      highestprofit=currentprofit;
      double perbalprofit=balance==0?0:highestprofit/balance*100;
      label(4,DoubleToStr(perbalprofit,2),DoubleToStr(highestprofit,2),clrLime,"Today's Highest Profit = ");
     }
   if(currentprofit < GlobalVariableGet((string)AccountNumber()+"LowestDl"+TimeToStr(iTime(_Symbol,PERIOD_D1,0))))
     {
      lowestprofit=currentprofit;
      perballoss=balance==0?0:lowestprofit/balance*100;
      GlobalVariableSet((string)AccountNumber()+"LowestDl"+TimeToStr(iTime(_Symbol,PERIOD_D1,0)),lowestprofit);
      GlobalVariableSet((string)AccountNumber()+"LowestPr"+TimeToStr(iTime(_Symbol,PERIOD_D1,0)),perballoss);

      label(5,
            DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestPr"+TimeToStr(iTime(_Symbol,PERIOD_D1,0))),2),
            DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestDl"+TimeToStr(iTime(_Symbol,PERIOD_D1,0))),2),
            clrYellow,"Today's Lowest Loss = ");
     }
   if(tmin!=TimeMinute(TimeCurrent()))
     {
     tmin=TimeMinute(TimeCurrent());
      label(5,DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestPr"+TimeToStr(iTime(_Symbol,PERIOD_D1,0))),2),
            DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestDl"+TimeToStr(iTime(_Symbol,PERIOD_D1,0))),2),clrYellow,"Today's Lowest Loss = ");

      label(6,DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestPr"+TimeToStr(iTime(_Symbol,PERIOD_D1,1))),2),
            DoubleToStr(GlobalVariableGet((string)AccountNumber()+"LowestDl"+TimeToStr(iTime(_Symbol,PERIOD_D1,1))),2),clrYellow,"Yesterday Lowest Loss = ");
     }
   double now_dd=AccountBalance()==0?0:(1-(AccountEquity()/AccountBalance()))*100;
   double now_dd_d=AccountBalance()==0?0:AccountBalance()-AccountEquity();
   if(dd>lowestprofit)
     {
      GlobalVariableSet("DD"+(string)AccountNumber(),lowestprofit);
      dd=lowestprofit;
     }
   if(ddp>perballoss)
     {
      ddp=perballoss;
      GlobalVariableSet("DD_P"+(string)AccountNumber(),perballoss);
     }
   label(7,DoubleToStr(GlobalVariableGet("DD_P"+(string)AccountNumber()),2),DoubleToStr(GlobalVariableGet("DD"+(string)AccountNumber()),2),clrYellow,"Total drawdown account = ");
  // label(8,"","",clrYellow,"ver. 20 mei ");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void label(int m,string valp,string vald,color warna_val,string cap="")
  {
   int obj=OBJ_LABEL;
   string nama=idgam+"lbl"+(string)m;
   int y=20+(15*m);
   int XDISTANCE_Label=120;
   int XDISTANCE_Valp=70;
   int XDISTANCE_Vald=10;
   if(ObjectFind(0,nama)<0)
     {
      ObjectCreate(0,nama,obj,0,0,0);
      ObjectSetInteger(0,nama,OBJPROP_XDISTANCE,XDISTANCE_Label);
      ObjectSetInteger(0,nama,OBJPROP_YDISTANCE,y);
      ObjectSet(nama, OBJPROP_CORNER, 1);    // Reference corner
      ObjectSetText(nama,cap,8,"Arial",TextColour);  // Changing object properties
     }
   nama=idgam+"valp"+(string)m;
   if(valp=="")
      ObjectDelete(0,nama);
   if(valp!="")
     {
      if(ObjectFind(0,nama)<0)
        {
         ObjectCreate(0,nama,obj,0,0,0);
         ObjectSetInteger(0,nama,OBJPROP_XDISTANCE,XDISTANCE_Valp);
         ObjectSetInteger(0,nama,OBJPROP_YDISTANCE,y);
         ObjectSet(nama, OBJPROP_CORNER, 1);    // Reference corner
        }
      ObjectSetText(nama,valp+" %",8,"Arial",warna_val);  // Changing object properties
     }
   nama=idgam+"vald"+(string)m;
   if(ObjectFind(0,nama)<0)
     {
      ObjectCreate(0,nama,obj,0,0,0);
      ObjectSetInteger(0,nama,OBJPROP_XDISTANCE,XDISTANCE_Vald);
      ObjectSetInteger(0,nama,OBJPROP_YDISTANCE,y);
      ObjectSet(nama, OBJPROP_CORNER, 1);    // Reference corner
     }
   if(vald=="")
         ObjectDelete(0,nama);

   if(vald!="")
      ObjectSetText(nama,vald+" $",8,"Arial",warna_val);  // Changing object properties

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
