//+------------------------------------------------------------------+
//|                                             Profit_Loss_Info.mq4 |
//|                                                    Goldexcalibur |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Goldexcalibur"
#property link      ""

#property indicator_chart_window
extern int Corner=0;

extern color Warna_Text       = Silver;
extern color Warna_Order_Sell = Red;
extern color Warna_Order_Buy  = Blue;
extern color Warna_Rugi       = Red;
extern color Warna_Laba       = Blue;
extern string NamaFont        = "Verdana Bold";
extern int    UkuranFont      = 10;
extern double posX            =  5;
extern double posY            = 15;
int x = NULL;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   for(int i = ObjectsTotal() - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringFind(label, "", 0) < 0)       
           continue;
       ObjectDelete(label);   
     }
   ObjectsDeleteAll(0,OBJ_HLINE);
   ObjectsDeleteAll(0,OBJ_TEXT);
   ObjectsDeleteAll(0,OBJ_LABEL); 
//----
   return(0);
  }
  
void DisplayText(string objname, string text, int fontsize, color clr, double C, double X, double Y, string font)
{
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(objname, text, fontsize, font, clr);
   ObjectSet(objname, OBJPROP_CORNER, Corner);
   ObjectSet(objname, OBJPROP_XDISTANCE, X);
   ObjectSet(objname, OBJPROP_YDISTANCE, Y);
}


  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int    j;
   int    idx;
   string text;
   double PL;
   double total;
//----
         if(x != NULL) {
            for(idx = 0; idx<x; idx++) {
               ObjectDelete("O"+idx);
               ObjectDelete("PL"+idx);
            }
            
            ObjectDelete("Total1");
            ObjectDelete("Total2");
         }
            
         text = "";
         j=0;
         total = 0;
         
         
                  
   
         DisplayText("h", "Profit/Loss Info ", UkuranFont, Warna_Text, 0, posX, posY , NamaFont); 
   
         for (int i = OrdersTotal()-1 ; i >= 0; i--) 
         { 
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
            { 
               
               if(OrderType() == OP_BUY)  {
                  //DisplayText("O"+j, OrderSymbol() + " (" + OrderTicket() + ")" + "   ", Font, Warna_Order_Buy, 0, 5, 25 + j * 18, "Arial Bold"); 
                  DisplayText("O"+j, OrderSymbol() + "   ", UkuranFont, Warna_Order_Buy, 0, posX, posY+20 + j * 18, NamaFont); 
                  
                  PL = (OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);
                  total = total + PL;
                  if(PL<0) DisplayText("PL"+j, "= "+DoubleToStr(PL, 0), UkuranFont, Warna_Rugi, 0, posX+129, posY+20 + j * 18, NamaFont); 
                  else if(PL>0) DisplayText("PL"+j, "= +"+DoubleToStr(PL, 0), UkuranFont, Warna_Laba, 0, posX+129, posY+20 + j * 18, NamaFont); 
                  else DisplayText("PL"+j, "= "+DoubleToStr(PL, 0), UkuranFont, Warna_Text, 0, posX+129, posY+20 + j * 18, NamaFont); 
               }
               else if(OrderType() == OP_SELL)  {
                  //DisplayText("O"+j, OrderSymbol() + " (" + OrderTicket() + ")" + "   ", Font, Warna_Order_Sell, 0, posX, 25 + j * 18, NamaFont); 
                  DisplayText("O"+j, OrderSymbol() + "   ", UkuranFont, Warna_Order_Sell, 0, posX, posY+20 + j * 18, NamaFont); 
                  
                  PL = (OrderOpenPrice()-OrderClosePrice())/MarketInfo(OrderSymbol(),MODE_POINT);
                  total = total + PL;
                  if(PL<0) DisplayText("PL"+j, "= "+DoubleToStr(PL, 0) , UkuranFont, Warna_Rugi, 0, posX+129, posY+20 + j * 18, NamaFont); 
                  else if(PL>0) DisplayText("PL"+j,"= +"+DoubleToStr(PL, 0) , UkuranFont, Warna_Laba, 0, posX+129, posY+20 + j * 18, NamaFont); 
                  else DisplayText("PL"+j,"= "+DoubleToStr(PL, 0) , UkuranFont, Warna_Text, 0, posX+129, posY+20 + j * 18, NamaFont); 
               }
               j=j+1;
            } 
         } 
         // menghitung total LABA / RUGI
         double LR = AccountEquity() - AccountBalance();
         string sLR = DoubleToStr(LR,2);
         DisplayText("Total1","Total Profit/Loss  = ", UkuranFont, Warna_Text, 0, posX, posY+20 + j * 18, NamaFont);
   
         if(total<0) DisplayText("Total2", DoubleToStr(total, 0) + " point = $ "+sLR, UkuranFont, Warna_Rugi, 0, posX+143, posY+20 + j * 18, NamaFont);
         else if (total>0) DisplayText("Total2","+" + DoubleToStr(total, 0) + " point = $ "+sLR, UkuranFont, Warna_Laba, 0, posX+143, posY+20 + j * 18, NamaFont);
         else DisplayText("Total2", DoubleToStr(total, 0) + " point = $ "+sLR, UkuranFont, Warna_Text, 0, posX+143, posY+20 + j * 18, NamaFont);
         
         x=j;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+