//+------------------------------------------------------------------+
//|                                   KantorInformation v1.00               |
//|                                   Copyright © 2008, Denkhaus     |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Denkhaus Logic Research 2008"
#property indicator_chart_window

extern int     myCorner          = 0 ;
extern int     myStyle           = STYLE_DASH ;

extern bool    ShowMarketPrice   = false;
extern color   ColorMarketPrice  = White;
extern bool    ShowAvgEntryPrice  = true;
extern color   ColorAvgEntryPrice = DodgerBlue;

extern bool    ShowDailyWL       = true;
extern color   ColorDailyWL      = Yellow;
extern bool    ShowHourlyWL      = true;
extern color   ColorHourlyWL     = Olive;
extern bool    ShowAverageWL     = true;
extern color   ColorAverageWL    = Orange;

extern bool    ShowLineLabels    = true;
extern bool    ShowSwapInfo      = true;
extern int     WLShift           = 0;
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void init()
{   
   DeleteObjects();  
}
//+------------------------------------------------------------------+
//|  Custom indicator iteration function                             |
//+------------------------------------------------------------------+  
void deinit()
{
   DeleteObjects();   
   return(0);
}
//+------------------------------------------------------------------+
//|  Custom indicator iteration function                             |
//+------------------------------------------------------------------+
void start()
{
   deinit();
   
   double Avg1_H1    = iMA(NULL, PERIOD_H1, 5,  0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg2_H1    = iMA(NULL, PERIOD_H1, 7,  0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg3_H1    = iMA(NULL, PERIOD_H1, 22, 0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg4_H1    = iMA(NULL, PERIOD_H1, 34, 0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg1_D1    = iMA(NULL, PERIOD_D1, 5,  0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg2_D1    = iMA(NULL, PERIOD_D1, 7,  0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg3_D1    = iMA(NULL, PERIOD_D1, 22, 0, MODE_SMA, PRICE_CLOSE, WLShift);
   double Avg4_D1    = iMA(NULL, PERIOD_D1, 34, 0, MODE_SMA, PRICE_CLOSE, WLShift);
   
   double dWLDaily  = 0.0;
   double dWLHourly = 0.0;
   double diff ;

   color  diffcolor = Yellow ;
    
   if(Symbol() == "GBPUSD")
   {
     dWLDaily    = (Avg1_D1 + Avg2_D1 + Avg3_D1 + Avg4_D1)/4;
     dWLHourly   = (Avg1_H1 + Avg2_H1 + Avg3_H1 + Avg4_H1)/4;
   }
   else
   {
     dWLDaily    = (Avg1_D1 + Avg2_D1 + Avg3_D1)/3;
     dWLHourly   = (Avg1_H1 + Avg2_H1 + Avg3_H1)/3;
   }
   
   double dWLAverage = (dWLDaily + dWLHourly)/2;
   
   if(ShowSwapInfo)
   {      
      double dSpreadValue = MarketInfo(Symbol(), MODE_SPREAD);
      double dPipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
      double dTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
      double dSwapLong  = MarketInfo(Symbol(), MODE_SWAPLONG) * dTickValue;
      double dSwapShort = MarketInfo(Symbol(), MODE_SWAPSHORT)* dTickValue;

//      string SwapInfo   = StringConcatenate("1 Pip= ", dPipValue, "$ Spread:", dSpreadValue, "  SWAP buy:", dSwapLong,"$|sell:", dSwapShort, "$");  
 

      string SwapInfo   = "1 Pip = " + DoubleToStr(dPipValue,4) ;
         
/*         
         
      if(dSwapLong > dSwapShort)
      
         ShowLabelObject("SwapInfo1", SwapInfo, Lime,myCorner,5,12);
      }
      else if(dSwapLong < dSwapShort)
      {
         ShowLabelObject("SwapInfo1", SwapInfo, Red,myCorner,5,12);
      }
*/      
      
      
      if(dSwapLong > dSwapShort) { diffcolor = Lime ; } else { diffcolor = Red ; }
      ShowLabelObject("SwapInfo1", SwapInfo, diffcolor ,myCorner,5,12);      
      
      SwapInfo   =  "$ Spread: " +  DoubleToStr(dSpreadValue,0)  ;        
      ShowLabelObject("SwapInfo2", SwapInfo, DodgerBlue ,myCorner,5,24);      
      
      
      SwapInfo   =  "SWAP buy: "  +  DoubleToStr(dSwapLong,Digits)  ;        
      ShowLabelObject("SwapInfo3", SwapInfo, DodgerBlue ,myCorner,5,36);        
      
      SwapInfo   =  "SWAP sell: "  +  DoubleToStr(dSwapShort,Digits)  ;        
      ShowLabelObject("SwapInfo4", SwapInfo, DodgerBlue ,myCorner,5,48);     
      

         
//   double dWLAverage = (dWLDaily + dWLHourly)/2;    
      
      diff       = Close[0] - dWLHourly ;
      if( diff >= 0 ) { diffcolor = Lime ; } else { diffcolor = Red ; }
      
      SwapInfo   =  "Pips to H1 WL : " +  DoubleToStr( diff / Point ,0)  ;        
      ShowLabelObject("SwapInfo5", SwapInfo, diffcolor ,myCorner,5,60);         
      
      diff       = Close[0] - dWLDaily ;
      if( diff >= 0 ) { diffcolor = Lime ; } else { diffcolor = Red ; }
      
      SwapInfo   =  "Pips to D1 WL : " +  DoubleToStr( diff / Point ,0)  ;        
      ShowLabelObject("SwapInfo6", SwapInfo, diffcolor ,myCorner,5,72);       

      diff       = Close[0] - dWLAverage ;
      if( diff >= 0 ) { diffcolor = Lime ; } else { diffcolor = Red ; }
      
      SwapInfo   =  "Pips to Avg WL : " +  DoubleToStr( diff / Point ,0)  ;        
      ShowLabelObject("SwapInfo7", SwapInfo, diffcolor ,myCorner,5,84);         
           
   }
   else
   {
      ObjectDelete("SwapInfo");
   }
   
   if(ShowMarketPrice)
   {
      ShowHLineObject("MarketPrice", ColorMarketPrice, Close[0]);            
   }
   else  
   {
     DeleteHLineObject("MarketPrice");
   }
   
   if(ShowDailyWL)
   {
      ShowHLineObject("DailyWL", ColorDailyWL, dWLDaily);            
   }
   else  
   {
     DeleteHLineObject("DailyWL");
   }
   
   if(ShowHourlyWL)
   {
      ShowHLineObject("HourlyWL", ColorHourlyWL, dWLHourly);      
   }
   else  
   {
     DeleteHLineObject("HourlyWL");
   }
   
   if(ShowAverageWL)
   {
      ShowHLineObject("AverageWL", ColorAverageWL, dWLAverage);
   }
   else  
   {
      DeleteHLineObject("AverageWL");
   }  
   
   int    nOrders       = 0;
   double dOpenPrice    = 0.0;
   double dProfit       = 0.0;
   
   for (int i = 0; i <= OrdersTotal(); i++) 
   { 
      if(OrderSelect(i,SELECT_BY_POS))
      { 
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            { 
               dOpenPrice += OrderOpenPrice();
               dProfit+= OrderProfit() + OrderSwap();
               nOrders++;
            }
         } 
      }
   }   
   
   if(nOrders > 0)
   {
      double dAvgEntryPrice = dOpenPrice/nOrders;
      
      if(ShowAvgEntryPrice)
      {
         ShowHLineObject("AvgEntryPrice", ColorAvgEntryPrice, dAvgEntryPrice);            
      }
      else  
      {
         DeleteHLineObject("AvgEntryPrice");
      }
      
      if(ObjectFind("PipRectangle")!= 0)
      {
         ObjectCreate("PipRectangle", OBJ_RECTANGLE, 0, Time[0], dAvgEntryPrice, Time[0], Close[0]);      
      }
      
      datetime dtInfoText     = Time[0] + Period() * 1000;
      double   dPriceInfoText = ((Close[0] + dAvgEntryPrice)/2) + 2 * Point;
      
      if(ObjectFind("InfoString")!= 0)
      {         
         ObjectCreate("InfoString", OBJ_TEXT, 0,dtInfoText, dPriceInfoText);         
      }
      
      ObjectSetText("InfoString", DoubleToStr(dProfit,2), 8, "Verdana", Gold);
      ObjectSet("InfoString", OBJPROP_PRICE1, dPriceInfoText);
      ObjectSet("InfoString", OBJPROP_TIME1, dtInfoText);      
      
      ObjectSet("PipRectangle", OBJPROP_TIME1,  Time[0]);         
      ObjectSet("PipRectangle", OBJPROP_PRICE1, dAvgEntryPrice);  
      ObjectSet("PipRectangle", OBJPROP_TIME2,  Time[0] + Period() * 2000);         
      ObjectSet("PipRectangle", OBJPROP_PRICE2, Close[0]);  
      
      if(dProfit < 0 )
      {
         ObjectSet("PipRectangle", OBJPROP_COLOR, Maroon);           
      }
      else
      {
         ObjectSet("PipRectangle", OBJPROP_COLOR, DarkGreen);  
      }
   }
   else
   {     
      ObjectDelete("PipRectangle");
      ObjectDelete("InfoString");  
   }  
}


//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void ShowLabelObject(string name, string text, color clr, int corner, int xDist, int yDist)
{
   if(ObjectFind(name)!= 0)
   {
      CreateLabelObject(name,text, clr, corner, xDist, yDist);     
   }       
   
   ObjectSetText(name, text, 10, "Verdana", clr);   
}

//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void CreateLabelObject(string name, string text, color clr, int corner, int xDist, int yDist)
{
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(name, text, 10, "Verdana", clr);
   ObjectSet(name, OBJPROP_CORNER, corner);
   ObjectSet(name, OBJPROP_XDISTANCE, xDist);
   ObjectSet(name, OBJPROP_YDISTANCE, yDist);
}
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void DeleteHLineObject(string name)
{
   ObjectDelete(name);
   ObjectDelete(name + "_Label");
}
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void ShowHLineObject(string name, color clr, double dValue)
{
   if(ObjectFind(name)!= 0)
   {
      CreateHLineObject(name,clr, dValue);         
   }    
   
   if(ShowLineLabels)
   {
      ObjectSet(name + "_Label",OBJPROP_PRICE1,dValue);  
      ObjectSet(name + "_Label",OBJPROP_TIME1,Time[0] + Period() * 3000);  
   }
   
   ObjectSet(name, OBJPROP_PRICE1, dValue);
}
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void CreateHLineObject(string name, color clr, double dValue)
{
   if(ShowLineLabels)
   {
      ObjectCreate   (name + "_Label", OBJ_TEXT, 0,Time[0] + Period() * 3000, dValue); 
      ObjectSetText  (name + "_Label",  name, 7, "Verdana", clr);
   }
   
   ObjectCreate   (name,            OBJ_HLINE, 0, Time[0], dValue);
   ObjectSet      (name,            OBJPROP_STYLE, myStyle );
   ObjectSet      (name,            OBJPROP_COLOR, clr);  
//   ObjectSet      (name,            OBJPROP_WIDTH, 0);
   
}
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void DeleteObjects()
{   
   ObjectDelete("PipRectangle");
   ObjectDelete("InfoString");
   
   ObjectDelete("AvgEntryPrice");    
   ObjectDelete("AvgEntryPrice_Label"); 
   ObjectDelete("MarketPrice");
   ObjectDelete("MarketPrice_Label");   
   ObjectDelete("AverageWL");
   ObjectDelete("AverageWL_Label");
   ObjectDelete("HourlyWL");
   ObjectDelete("HourlyWL_Label");
   ObjectDelete("DailyWL");   
   ObjectDelete("DailyWL_Label");
   ObjectDelete("SwapInfo");
   ObjectDelete("SwapInfo1");   
   ObjectDelete("SwapInfo2");   
   ObjectDelete("SwapInfo3");   
   ObjectDelete("SwapInfo4");   
   ObjectDelete("SwapInfo5");
   ObjectDelete("SwapInfo6");
   ObjectDelete("SwapInfo7");         
}