//+------------------------------------------------------------------+
//|                                                       Tick.mq4 |
//|                                                      Denis Orlov |
//|                                    http://denis-or-love.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Denis Orlov"
#property link      "http://denis-or-love.narod.ru"

//#include <Trend.mqh>

#property indicator_separate_window

double Line1[],Line2[],Line3[],Line4[],Line5[];
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Green
//#property indicator_color4 Green
//#property indicator_color5 Red

double oldPrice;
double UpSumm, DnSumm, MaxDiff;
datetime per;

extern int Diff_Line=6;
extern bool Diff_Alert=True;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
    IndicatorShortName("Volume");
      int Win=WindowFind("Volume");
   DrawLabels("Diff", 1, 30, 10, "...", Blue, Win, 12);
   DrawLabels("MaxDiff", 1, 30, 30, "...", Blue, Win, 14);
   //ObjectSet("MaxDiff", OBJPROP_FONTSIZE, 14); 
   ObjectSetText("MaxDiff","...",14,"Arial",Blue);
   
//---- indicators
   SetIndexBuffer(0,Line1);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);//DRAW_LINE
   SetIndexShift(0,2);
   
   SetIndexBuffer(1,Line2);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexShift(1,2);
   
   SetIndexBuffer(2,Line3);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexShift(2,2);
   
     /* SetIndexBuffer(4,Line4);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);//
   SetIndexBuffer(5,Line5);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2);*/
   
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
      int    i, Counted_bars=IndicatorCounted();
      
      
      
     i=Bars-Counted_bars-1;           // Индекс первого непосчитанного  
     //if (History>0 && i>History-1)                 // Если много баров то ..      
      // i=History-1; 
     /* while(i>=2)                      // Цикл по непосчитанным барам     
      { 
        if(Bull_Bear(i)>0) {Line1[i+1]=Volume[i]; Line2[i+1]=Volume[i]-Volume[i]/24;}
        if(Bull_Bear(i)<0) {Line2[i+1]=Volume[i]; Line3[i+1]=Volume[i]-Volume[i]/24;}
      i--;
      }*/
      
      if (per == Time[0])//тот же бар
      {
         double Razn=(Close[0]-oldPrice)/Point;
         //Alert("Bid="+Bid+";oldPrice="+oldPrice+";Razn="+Razn);
         if(Razn>0)
            {
               UpSumm=UpSumm+Razn;
            }
         if(Razn<0)
            {
                DnSumm=DnSumm+Razn;
            } 
         oldPrice=Close[0]; 
 
       Line1[1]=UpSumm; 
       Line2[0]=-DnSumm; 
       //if(Line1[0]<Line2[0])Line3[0]=UpSumm; else Line3[0]=0;
       
       int Win=WindowFind("Volume");
       double Diff=Line1[1]-Line2[0];
       string DiffS=DoubleToStr(Diff, 1);
       
        if(Line1[1]>Line2[0])
         {color clr=Green; DrawLabels("Diff", 1, 30, 10, DiffS ,clr , Win, 12); 
          }//Line4[0]=Diff*2;
         else
       if(Line1[1]<Line2[0])
         {clr=Red; DrawLabels("Diff", 1, 30, 10, DiffS, clr, Win, 12); 
         }//Line5[0]=-Diff*2;
        // else
       //if(Line1[1]==Line2[0])
         //{clr=Blue; DrawLabels("Diff", 1, 30, 10, "..." ,clr , Win, 12); 
        //}// Line5[0]=0; Line4[0]=0;
        
        if(MathAbs(Diff)>=MathAbs(MaxDiff))
         {
          MaxDiff=Diff;
          DrawLabels("MaxDiff", 1, 30, 30, DiffS ,clr , Win, 14); 
          
          if(Diff_Line!=0 && MathAbs(MaxDiff)>=Diff_Line)
            {
            string name="Diff "+TimeToStr(Time[0],TIME_MINUTES);
            
            if(Diff_Alert && ObjectFind(name)==-1)
               Alert(Symbol()+" : Разница между тиками достигла "+DiffS);
               
            if(ObjectFind(name)==-1)
               ObjectCreate(name, OBJ_VLINE,Win, Time[0], 0);
         ObjectSet(name, OBJPROP_COLOR, clr ) ;
         ObjectSetText( name, DiffS); 
            }
         }
         
         
         
      }
      else// новый бар
      {
       per = Time[0];

       Line1[3]=UpSumm; 
       Line2[3]=-DnSumm;
       if(Line1[3]<Line2[3])Line3[3]=UpSumm; else Line3[3]=0;
       
       Line1[2]=0; 
       Line2[2]=0;
        Line1[1]=0; 
         Line2[1]=0;
       
       UpSumm=0; DnSumm=0; MaxDiff=0;
      // Line5[0]=Line5[1]; Line4[0]=Line4[1];
       oldPrice=Open[0];

      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
int DrawLabels(string name, int corn, int X, int Y, string Text, color Clr=Green, int W=0, int FSize=10)
   {
     int Error=ObjectFind(name);// Запрос 
   if (Error!=W)// Если объекта в ук. окне нет :(
    {  
      ObjectCreate(name,OBJ_LABEL,W, 0,0); // Создание объекта
    }
     
     ObjectSet(name, OBJPROP_CORNER, corn);     // Привязка к углу   
     ObjectSet(name, OBJPROP_XDISTANCE, X);  // Координата Х   
     ObjectSet(name,OBJPROP_YDISTANCE,Y);// Координата Y   
     ObjectSetText(name,Text,FSize,"Arial",Clr);
   }