//+------------------------------------------------------------------+
//|                                                ProfitTracker.mq4 |
//|        Copyright © 2009, Jason Muchow (airforcemook@hotmail.com) |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Jason Muchow (airforcemook@hotmail.com)"

#property indicator_chart_window

extern bool only_this_symbol = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   run();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   deleteIcons();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   run();
//----
   return(0);
  }
//+------------------------------------------------------------------+

void deleteIcons() {
   for (int i=0;i<=ObjectsTotal();i++) {
      string name=ObjectName(i);
      if (StringFind(name,"pt_",0)!=-1) {
         ObjectDelete(name);
      }
   }
}

void run() {
   
   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 = time_hour*24;
   int time_week = time_day*7;
   int time_month = time_day*31;//todo fix it so that 30 day months, etc work properly
   
   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()+OrderSwap();
         
         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)) {
         order_profit = OrderProfit()+OrderSwap();
         
         if (OrderSymbol()!=Symbol() && only_this_symbol) {
            // do nothing
         } else {
         
            profit_current += order_profit;
         
         }
      
      }
   }
   
   // create labels
   string font = "arial";
   string name;
   
   name="pt_name";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Profit$",16,font,GreenYellow);
   prepare_label(name,3,20,100);
         
   name="pt_month";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Month",8,font,Gray);
   prepare_label(name,3,20,90);
         
   name="pt_week";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Week",8,font,Gray);
   prepare_label(name,3,25,80);
   
   name="pt_day";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Day",8,font,Gray);
   prepare_label(name,3,31,70);
   
   name="pt_hour";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Hr",8,font,Gray);
   prepare_label(name,3,39,60);
   
   name="pt_l1";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"__________",10,font,Gold);
   prepare_label(name,3,20,60);
   
   name="pt_now";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,"Now",12,font,GreenYellow);
   prepare_label(name,3,18,42);
   
   /////////////////
   // Now set prices
   /////////////////
   name="pt_m$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_month),9,font,Lime);
   if (profit_month<=0) {ObjectSet(name,OBJPROP_COLOR,OrangeRed);}
   prepare_label(name,3,55,90);
   
   name="pt_w$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_week),9,font,Lime);
   if (profit_week<=0) {ObjectSet(name,OBJPROP_COLOR,OrangeRed);}
   prepare_label(name,3,55,80);
   
   name="pt_d$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_day),9,font,Lime);
   if (profit_day<=0) {ObjectSet(name,OBJPROP_COLOR,OrangeRed);}
   prepare_label(name,3,55,70);
   
   name="pt_h$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_hour),9,font,Lime);
   if (profit_hour<=0) {ObjectSet(name,OBJPROP_COLOR,OrangeRed);}
   prepare_label(name,3,55,60);
   
   name="pt_n$";
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,money(profit_current),12,font,Lime);
   if (profit_current<=0) {ObjectSet(name,OBJPROP_COLOR,Red);}
   prepare_label(name,3,55,42);
         
}
          
void prepare_label (string name,int corner,int x,int y) {
   ObjectSet(name,OBJPROP_CORNER,corner);
   ObjectSet(name,OBJPROP_XDISTANCE,x);
   ObjectSet(name,OBJPROP_YDISTANCE,y);
}

string money(double value) {
   string msg;
   msg = DoubleToStr(value,2);
   return(msg);
}
   
            