//+------------------------------------------------------------------+
//|                                           451 profit or loss.mq4 |
//|                                                           .....h |
//|                                                    hayseedfx.com |
//+------------------------------------------------------------------+
#property copyright ".....h"
#property link      "hayseedfx.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

#define   aday 86400

//--- input parameters
extern string note         = "place on any daily chart......h";

datetime bar, bbar, abar;
extern string   date         = "2013.3.2 03:00";

       datetime strdate;
       int      total;

//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,3);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,3);
   SetIndexBuffer(1,ExtMapBuffer2);
   
   if(Period() != 1440) { Alert("profit or loss indicator is only accurate on a daily chart......h");}
   strdate = StrToTime(date);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

     int limit;
     int counted_bars=IndicatorCounted();

     if(counted_bars>0) counted_bars--;

       limit    = Bars-counted_bars;
       
       if(total != OrdersHistoryTotal())
       {
       double pl;      
       bool profitable = true;
//---- 
   for(int i=0; i<limit; i++)
       {
       bar  = iTime(NULL,PERIOD_D1,i);
       bbar = bar - aday;
       abar = bar + aday;
       pl = Profit(bar,abar); 
       
       if(pl >= 0.0) profitable =  true;
       if(pl <  0.0) profitable = false;
       
       if(profitable)
       {
       ExtMapBuffer1[i] =  pl;  
       ExtMapBuffer2[i] = 0.0;   
       }
       else
       {
       ExtMapBuffer2[i] =  pl;   
       ExtMapBuffer1[i] = 0.0;   
       }       
       }
       total = OrdersHistoryTotal();
       }
       Comment(ClosedOrders());
//----
   return(0);
  }
//+------------------------------------------------------------------+DayOfYear()

 
 double ClosedOrders()    
  {
  int count;
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     //---- check selection result
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")");
        break;
       }
       if(OrderType() > OP_SELL)     continue;
      if(OrderCloseTime() < strdate) continue;

      count++;
                  
	}
   return (count);	
   }
    // some work with order
   
    
   /* 
double ClosedOrder()    
  {
   int totals;
   int count = 0;
   totals = OrdersHistoryTotal();
   for (int i = totals - 1; i >= 0; i--)
   {
   OrderSelect (i, SELECT_BY_POS, MODE_HISTORY); 
      if(OrderType() > OP_SELL)      continue;
      if(OrderCloseTime() < strdate) continue;

      count++;
                  
	}
   return (count);	
   }
*/
//------  
//------

double Profit(datetime b, datetime a)
{
   
   double profit;
  
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     //---- check selection result
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")");
        break;
       }             
                                                               
    if(OrderCloseTime() < b) continue;  
    if(OrderCloseTime() > a) continue;
     
     profit = OrderProfit()+profit;
     }
 
  return (profit);
}

//------
//------

