//+------------------------------------------------------------------+
//|                                             history expanded.mq4 |
//|                                                          ......h |
//|                                                    hayseedfx.com |
//+------------------------------------------------------------------+
#property copyright "......h"
#property link      "hayseedfx.com"


#property indicator_chart_window

extern string   date         = "2011.3.1 00:00";
       int      d;
       datetime strdate;
extern int      horizontal   =    10;
extern int      vertical     =    10;
extern int      corner       =     1;
extern bool     History      =  true;
extern bool     Profit       =  true;
extern bool     Swap         =  true;
extern bool     Buys         =  true;
extern bool     BuyWins      =  true;
extern bool     BuyLoss      =  true;
extern bool     BuyPips      =  true;

extern bool     Sells        =  true;
extern bool     SellWins     =  true;
extern bool     SellLoss     =  true;
extern bool     SellPips     =  true;
       int      totalhistory;
       
       bool     TradeLength  = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits == 3 || Digits == 5)
   {
   d=10;
   }
   else
   {
   d=1;
   }
   
   
   strdate = StrToTime(date);
   //----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
 {
 if(totalhistory != OrdersHistoryTotal())
 { 
 if(History) {history();}


 if(Buys)
 {   
 Display("Buys",  horizontal,vertical, DoubleToStr(Data("buys"),0)+ "  Buys");
 }

 if(BuyWins)
 {   
 Display("BuyWins",  horizontal,vertical+50, DoubleToStr(Data("buywins"),0)+ "  Wins");
 }

 if(BuyLoss)
 {   
 Display("BuyLoss",  horizontal,vertical+100, DoubleToStr(Data("buyloss"),0)+ "  Loss");
 }

 if(BuyPips)
 {   
 Display("BuyPips",  horizontal,vertical+150, DoubleToStr(Data("buypips"),0)+ "  Pips");
 }

 if(Sells)
 {      
 Display("Sells",     horizontal,vertical+250, DoubleToStr(Data("sells"),0) + "  Sells");
 }
 
 if(SellWins)
 {      
 Display("SellWins",  horizontal,vertical+300, DoubleToStr(Data("sellwins"),0) + "  Wins");
 }
 
 if(SellPips)
 {      
 Display("SellLoss",  horizontal,vertical+350, DoubleToStr(Data("sellloss"),0) + "  Loss");
 }


 if(SellPips)
 {      
 Display("SellPips",  horizontal,vertical+400, DoubleToStr(Data("sellpips"),0) + "  Pips");
 }
 
 if(Profit)
 {
 Display("Profit",    horizontal,vertical+500, DoubleToStr(Data("profit"),2)+"   Profit");
 }
 
 if(Swap)
 {      
 Display("Swap",      horizontal,vertical+550, DoubleToStr(Data("swap"),2)+"  Swap");
 } 
 //----
 }
 
 if(TradeLength) {Length("All");}    // included but not used..... if need be, ask about it on platform tech section of forex factory
 //----
   return(0);
 }

 void history()
   {
   int total = OrdersHistoryTotal();
   int ticket;
   double pips;
   string name;
   for(int i=0;i<total;i++)
    {
     //---- 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")");
        break;
       }
       
       if(OrderSymbol() != Symbol())  continue;
       if(OrderCloseTime() < strdate) continue;

       ticket = OrderTicket();

      if(OrderType() == OP_BUY) {pips   = (OrderClosePrice()-OrderOpenPrice())/(Point*d);  name = DoubleToStr(pips,0)+"  "+ ticket;} 
      
      if(OrderType() == OP_BUY)
      {
                                                                                                            //string name = DoubleToStr(pips,0)+"  "+ ticket;
      ObjectCreate(name, OBJ_TREND, 0, 0, 0);
      if(OrderProfit() >= 0)ObjectSet(DoubleToStr(pips,0)+"  "+ ticket, OBJPROP_COLOR, Lime);
      if(OrderProfit() <  0)ObjectSet(name, OBJPROP_COLOR, Red);
      ObjectSet(name, OBJPROP_PRICE1, OrderOpenPrice());
      ObjectSet(name, OBJPROP_PRICE2, OrderClosePrice());
      ObjectSet(name, OBJPROP_TIME1, OrderOpenTime());
      ObjectSet(name, OBJPROP_TIME2, OrderCloseTime());   
      ObjectSet(name, OBJPROP_RAY, 0); 
      ObjectSet(name, OBJPROP_WIDTH, 2); 
      }

     
  
      if(OrderType() == OP_SELL) {pips   = (OrderOpenPrice()-OrderClosePrice())/(Point*d);  name = DoubleToStr(pips,0)+"  "+ ticket;} 

      if(OrderType() == OP_SELL)
      {
      pips   = (OrderClosePrice()-OrderOpenPrice())/(Point*d);

      ObjectCreate(name, OBJ_TREND, 0, 0, 0);
      if(OrderProfit() >= 0)ObjectSet(name, OBJPROP_COLOR, Lime);
      if(OrderProfit() <  0)ObjectSet(name, OBJPROP_COLOR, Red);
      ObjectSet(name, OBJPROP_PRICE1, OrderOpenPrice());
      ObjectSet(name, OBJPROP_PRICE2, OrderClosePrice());
      ObjectSet(name, OBJPROP_TIME1,  OrderOpenTime());
      ObjectSet(name, OBJPROP_TIME2,  OrderCloseTime());   
      ObjectSet(name, OBJPROP_RAY, 0); 
      ObjectSet(name, OBJPROP_WIDTH, 2); 
      }
 
   }
   }
   
   
   
   
   
//----
 void Display(string name, int x, int y,string text)
   {
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
   ObjectSet(name, OBJPROP_CORNER, corner);
   ObjectSet(name, OBJPROP_XDISTANCE, x);
   ObjectSet(name, OBJPROP_YDISTANCE, y);
   ObjectSet(name, OBJPROP_BACK, FALSE);
   ObjectSetText(name, text, 12, "Verdana", Aqua); 
   }   
 

 

//----

double Data(string type)    
  {
   
   double swap;
   double profit;
   int    buys,buypips,buywins,buyloss;
   int    sells,sellpips,sellwins,sellloss;
   int    buylength,selllength;
   
   int    total = HistoryTotal();
   for (int i = total - 1; i >= 0; i--)
   {
   OrderSelect (i, SELECT_BY_POS, MODE_HISTORY); 
      
      if(OrderSymbol() != Symbol())  continue;
      if(OrderType() > OP_SELL)      continue;      
      if(OrderCloseTime() < strdate) continue;
      
      profit   = OrderProfit()+ profit; 
      swap     = OrderSwap() + swap;
      if(OrderType() == OP_BUY)  {buypips  = (OrderClosePrice()-OrderOpenPrice())/(Point*d) +buypips;} 
      if(OrderType() == OP_BUY)  {buys++;} 
      if(OrderType() == OP_BUY && OrderProfit() > 0.0)  {buywins++;} 
      if(OrderType() == OP_BUY && OrderProfit() < 0.0)  {buyloss++;} 
       
      if(OrderType() == OP_SELL)  {sellpips = (OrderOpenPrice()-OrderClosePrice())/(Point*d) +sellpips;}         
      if(OrderType() == OP_SELL)  {sells++;} 
      if(OrderType() == OP_SELL && OrderProfit() > 0.0)  {sellwins++;} 
      if(OrderType() == OP_SELL && OrderProfit() < 0.0)  {sellloss++;} 
	}
	if(type == "profit")  {return(profit);}
	if(type == "swap")    {return(swap);}
	if(type == "buys")    {return(buys);}
	if(type == "buywins") {return(buywins);}
	if(type == "buyloss") {return(buyloss);}
	if(type == "buypips") {return(buypips);}

	if(type == "sells")    {return(sells);}
	if(type == "sellwins") {return(sellwins);}
	if(type == "sellloss") {return(sellloss);}	
	if(type == "sellpips") {return(sellpips);}
		
   }



//-----
//-----


int Length(string type)
{
//----
 int total=OrdersTotal();
   int seconds = 0;
   int count;
   int average;
   datetime opentrade;
   datetime closetrade;

   total = HistoryTotal();
   for (int cnt = total - 1; cnt >= 0; cnt--)
   {
   OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);
   if(OrderCloseTime() < strdate) continue;
   if((OrderType() > OP_SELL)                   ||
     (type == "buy"  && OrderType() != OP_BUY)  ||
     (type == "sell" && OrderType() != OP_SELL) ||
     (type == "win"  && OrderProfit() < 0.0)    ||
     (type == "loss" && OrderProfit() > 0.0))   continue;     // 0.0 gets counted both ways
   
   opentrade  = OrderOpenTime();
   closetrade = OrderCloseTime();
   count++;
   seconds += ((closetrade-opentrade)/3600);//24;
   average = seconds/count;
   }
 return(average);
}








