//+------------------------------------------------------------------+
//|                                       Fahrenheit 451 history.mq4 |
//|                                                          ......h |
//|                                                    hayseedfx.com |
//+------------------------------------------------------------------+
#property copyright "......h"
#property link      "hayseedfx.com"

#property indicator_chart_window

extern string   date               = "2012.9.8 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;
extern bool     DrawAverageLine    =  true;

       int      totalhistory;


//+------------------------------------------------------------------+
//| 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()
  {
//----
   DeleteS();
//----
   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+40, DoubleToStr(Data("buywins"),0)+ "  Wins");  //  DoubleToStr(Data("byprofit"),0)+ "  "+
 }

 if(BuyLoss)
 {   
 Display("!"+"BuyLoss",       horizontal,vertical+80, DoubleToStr(Data("buyloss"),0)+ "  Loss"); // DoubleToStr(Data("byloss"),0)+ "  "+
 }

 if(BuyPips)
 {   
 Display("!"+"BuyPips",       horizontal,vertical+120, DoubleToStr(Data("buypips"),0)+ "  Pips");
 }

 if(Sells)
 {      
 Display("!"+"Sells",         horizontal,vertical+180, DoubleToStr(Data("sells"),0) + "  Sells");
 }
 
 if(SellWins)
 {      
 Display("!"+"SellWins",      horizontal,vertical+220, DoubleToStr(Data("sellwins"),0) + "  Wins");
 }
 
 if(SellPips)
 {      
 Display("!"+"SellLoss",      horizontal,vertical+260, DoubleToStr(Data("sellloss"),0) + "  Loss");
 }


 if(SellPips)
 {      
 Display("!"+"SellPips",      horizontal,vertical+300, DoubleToStr(Data("sellpips"),0) + "  Pips");
 }
 
 if(Profit)
 {
 Display("!"+"Profithistory", horizontal,vertical+330, DoubleToStr(Data("profit"),2)+"   Profit");
 }
 
 if(Swap)
 {      
 Display("!"+"Swap",          horizontal,vertical+350, DoubleToStr(Data("swap"),2)+"   Swap");
 Display("!"+"SwapLong",      horizontal,vertical+370, DoubleToStr(Data("swaplong"),2)+"   Buy");
 Display("!"+"SwapShort",     horizontal,vertical+390, DoubleToStr(Data("swapshort"),2)+"   Sell");
 
 
 } 

  totalhistory = OrdersHistoryTotal();
   
 //----
 }

 
 //----
   return(0);
 }


//------
//------
  

//------  
//------



//----
//----

 void history()
   {
   int total = OrdersHistoryTotal();
   int ticket;
   double pips;
   double profit;
   double lots;
   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();
       lots       = OrderLots();
       profit     = OrderProfit();

      if(OrderType() == OP_BUY) {pips   = (OrderClosePrice()-OrderOpenPrice())/(Point*d);  name = "!"+DoubleToStr(pips,0)+"  "+ ticket+"  "+DoubleToStr(lots,2)+"  "+ DoubleToStr(profit,2);} 
      
      if(OrderType() == OP_BUY)
      {
                                                                                                            //string name = DoubleToStr(pips,0)+"  "+ ticket;
      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); 
      }

     
  
      if(OrderType() == OP_SELL) {pips   = (OrderOpenPrice()-OrderClosePrice())/(Point*d);  name = DoubleToStr(pips,0)+"  "+ ticket+"  "+DoubleToStr(lots,2)+"  "+ DoubleToStr(profit,2);} //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;
   
   double byprofit,byloss,slprofit,slloss;
  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(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);} 
      if(OrderType() == OP_BUY)  {buys++;} 
      if(OrderType() == OP_BUY && OrderProfit() > 0.0)  {buywins++; byprofit += OrderProfit();} 
      if(OrderType() == OP_BUY && OrderProfit() < 0.0)  {buyloss++; byloss   += OrderProfit();} 
      
       
      if(OrderType() == OP_SELL)  {sellpips += (OrderOpenPrice()-OrderClosePrice())/(Point*d);}         
      if(OrderType() == OP_SELL)  {sells++; slprofit += OrderProfit();} 
      if(OrderType() == OP_SELL && OrderProfit() > 0.0)  {sellwins++; slprofit += OrderProfit();} 
      if(OrderType() == OP_SELL && OrderProfit() < 0.0)  {sellloss++; slloss   += OrderProfit();} 
            
	}
	if(type == "profit")     {return(profit);}
	if(type == "swap")       {return(swap);}
	if(type == "swaplong")   {return(MarketInfo(Symbol(),18));}   //18
	if(type == "swapshort")  {return(MarketInfo(Symbol(),19));}  //19
	
	
	if(type == "buys")       {return(buys);}
	if(type == "buywins")    {return(buywins);}
	if(type == "buyloss")    {return(buyloss);}
	if(type == "buypips")    {return(buypips);}
   if(type == "byprofit")   {return(byprofit);}
   if(type == "byloss")     {return(byloss);}
   
	if(type == "sells")      {return(sells);}
	if(type == "sellwins")   {return(sellwins);}
	if(type == "sellloss")   {return(sellloss);}	
	if(type == "sellpips")   {return(sellpips);}
   if(type == "slprofit")   {return(slprofit);}
   if(type == "slloss")     {return(slloss);}
		
   }



//-----
//-----

 
void DeleteS()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringFind(name,"!",0)>-1) ObjectDelete(name);

   }
}



