//+------------------------------------------------------------------+
//|                                                ProfitTracker.mq4 |
//|        Copyright © 2009, Jason Muchow (airforcemook@hotmail.com) |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Jason Muchow (airforcemook@hotmail.com)"

#property indicator_chart_window

bool only_this_symbol = true;
extern int Quotient = 10;
extern bool Show_Month = true;
extern bool Show_Week = true;
extern bool Show_Day = true;
extern bool Show_Hour = true;
extern bool Show_Float = false;
extern int corner = 3;
extern int FontSize = 9;
extern int label_x=18;
extern int label_y=171;
extern color Profit_color = SeaGreen;
extern color Loss_color = Red;
extern color label_color = DarkGray;
extern color period_color = Gray;
// extern color line_color = Gray;
extern color BgColor = White;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   run();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("pt_name");
   ObjectDelete("pt_month");
   ObjectDelete("pt_week");
   ObjectDelete("pt_day");
   ObjectDelete("pt_hour");
   ObjectDelete("pt_l1");
   ObjectDelete("pt_now");   
   
   ObjectDelete("pt_m$");
   ObjectDelete("pt_w$");
   ObjectDelete("pt_w$");
   ObjectDelete("pt_d$");
   ObjectDelete("pt_h$");
   ObjectDelete("pt_n$"); 
      
   ObjectDelete("Bcgrnd1");     
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   run();
//----
   return(0);
  }
//+------------------------------------------------------------------+

void run() {
ObjectCreate("Bcgrnd1", OBJ_LABEL, 0, 0, 0, 0, 0);
ObjectSetText("Bcgrnd1", "g", 56, "Webdings");      
ObjectSet("Bcgrnd1", OBJPROP_CORNER, corner);
ObjectSet("Bcgrnd1", OBJPROP_BACK, false);
ObjectSet("Bcgrnd1", OBJPROP_XDISTANCE, 2);
ObjectSet("Bcgrnd1", OBJPROP_YDISTANCE, 149);    
ObjectSet("Bcgrnd1", OBJPROP_COLOR, BgColor);
  
   double profit_current,profit_hour,profit_day,profit_week,profit_month;
   
   profit_current=0;
   profit_hour=0;
   profit_day=0;
   profit_week=0;
   profit_month=0;
   
   int start_hour = iTime(Symbol(),PERIOD_H1,0);
   int start_day = iTime(Symbol(),PERIOD_D1,0);
   int start_week = iTime(Symbol(),PERIOD_W1,0);
   int start_month = iTime(Symbol(),PERIOD_MN1,0);
   
   int time_hour = 60*60;
   int time_day = 1440*60;
   int time_week = 10080*60;
   int time_month = 43200*60;
   
   int now = TimeCurrent();
   
   int iter_max = OrdersHistoryTotal();
   
   for (int i=0;i<=iter_max;i++) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
         int order_start = OrderOpenTime();
//       double order_profit = (OrderProfit()+OrderCommission()+OrderSwap())/OrderLots()/MarketInfo(Symbol(),MODE_TICKVALUE);
       if(OrderType() == OP_BUY)
        {
       double order_profit = (OrderClosePrice()-OrderOpenPrice())/Point/Quotient;}
       if(OrderType() == OP_SELL)
        {
              order_profit = (OrderOpenPrice()-OrderClosePrice())/Point/Quotient;}
         
         if (OrderSymbol()!=Symbol() && only_this_symbol) {
            // do nothing
         } else {
            
            if (order_start >= start_hour) {profit_hour+=order_profit;}
            if (order_start >= start_day) {profit_day+=order_profit;}
            if (order_start >= start_week) {profit_week+=order_profit;}
            if (order_start >= start_month) {profit_month+=order_profit;}
            
         }
      }
   }
   
   for (i=0;i<=OrdersTotal();i++) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
       if(OrderType() == OP_BUY)
        {
             order_profit = (OrderClosePrice()-OrderOpenPrice())/Point/Quotient;}
       if(OrderType() == OP_SELL)
        {
              order_profit = (OrderOpenPrice()-OrderClosePrice())/Point/Quotient;}

         if (OrderSymbol()!=Symbol() && only_this_symbol) {
            // do nothing
         } else {
         
            profit_current += order_profit;
         
         }
      
      }
   }
   
   // create labels
   string font = "Berlin Sans FB";
   string name;
   
   name="pt_name";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"PipTracker",FontSize+2,font,label_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+59);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+5,label_y+59);}    
      
   name="pt_month";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Month",FontSize,font,period_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+48);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+41,label_y+48);}  
   
   name="pt_week";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Week",FontSize,font,period_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+38);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+42,label_y+38);}     
   
   name="pt_day";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Day",FontSize,font,period_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+28);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+51,label_y+28);}      
   
   name="pt_hour";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Hr",FontSize,font,period_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+18);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+59,label_y+18);}   
   
   if(Show_Float == true)
    {  
   name="pt_now";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Float",FontSize,font,period_color);
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+2,label_y+9);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+47,label_y+9);}     
   } 
   /////////////////
   // Now set prices    
   /////////////////
    if(Show_Month == true)
    {
   name="pt_m$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_month),FontSize,font,Profit_color);
   if (profit_month<=0) {ObjectSet(name,OBJPROP_COLOR,Loss_color);}
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+37,label_y+48);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+2,label_y+48);}    
    }
    if(Show_Week == true)
    {   
   name="pt_w$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_week),FontSize,font,Profit_color);
   if (profit_week<=0) {ObjectSet(name,OBJPROP_COLOR,Loss_color);}
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+37,label_y+38);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+2,label_y+38);}     
   }
       if(Show_Day == true)
    {
   name="pt_d$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_day),FontSize,font,Profit_color);
   if (profit_day<=0) {ObjectSet(name,OBJPROP_COLOR,Loss_color);}
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+37,label_y+28);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+2,label_y+28);}    
   }
       if(Show_Hour == true)
    {
   name="pt_h$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_hour),FontSize,font,Profit_color);
   if (profit_hour<=0) {ObjectSet(name,OBJPROP_COLOR,Loss_color);}
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+37,label_y+18);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+2,label_y+18);}       
   }
      if(Show_Float == true)
    { 
   name="pt_n$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_current),FontSize,font,Profit_color);
   if (profit_current<=0) {ObjectSet(name,OBJPROP_COLOR,Loss_color);}
   if(corner == 0 || corner==2)
   {prepare_label(name,corner,label_x+37,label_y+9);}
   if(corner == 1 || corner==3) 
   {prepare_label(name,corner,label_x+2,label_y+9);}    
   }        
}
          
void prepare_label (string name,int corner,int x,int y) {
// corner = 2; 
y=y-25;
x=x-15;
   ObjectSet(name,OBJPROP_CORNER,corner);

   ObjectSet(name,OBJPROP_XDISTANCE,x);
   ObjectSet(name,OBJPROP_YDISTANCE,y);
}

string money(double value) {
   string msg;
   msg = DoubleToStr(value,1);
   return(msg);
}
   
            