//+------------------------------------------------------------------+
//|                                          TickPriceChannel_v1.mq4 |
//|                             Copyright © 2007-08, TrendLaboratory |
//|                          Many Thanks to Rosh for Ticks indicator |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007-08, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 DodgerBlue
#property indicator_color3 DodgerBlue
#property indicator_style3 2
#property indicator_color4 Lime
//---- buffers
extern int     Length      =    14; //Length(Period) of Channel
extern int     UseDelimiter=     1; //Delimiter Mode: 0-off,1-on
extern color   DelimColor  =  Gray; //Color of Bars Delimiter 
extern int     MaxTicks    =   200; //Max Number of ticks   


double Up[];
double Dn[];
double Mid[];
double Ticks[];

int      tickCounter=0;
int      delimeterCounter;
datetime pTime;
string   short_name, setup;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   //IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, Up);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, Dn);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2, Mid);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3, Ticks);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   short_name="TickPriceChannel_v1("+Length+","+UseDelimiter+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Upper");
   SetIndexLabel(1,"Lower");
   SetIndexLabel(2,"Middle");
   SetIndexLabel(3,"Bid");
   
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(3,0.0);
      
   setup = short_name+": ";
   pTime = Time[0];
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   ObjDel(setup);
//----
   return(0);
}

void SetDelimeter()
{
//----
   string delimeterName = setup + TimeToStr(Time[0]);
   
   int handle=WindowFind(short_name);
   if(!ObjectCreate(delimeterName,OBJ_VLINE,handle,Time[0],0))
   {
   Print("Error delimiter:",GetLastError());
   }
   else 
   {
   ObjectSet(delimeterName,OBJPROP_COLOR,DelimColor);
   ObjectSet(delimeterName,OBJPROP_STYLE,STYLE_DOT);
   }
}

void ShiftArray()
{
   
   for(int cnt=tickCounter-1; cnt > 0; cnt--)
   {
   Ticks[cnt] = Ticks[cnt-1];
   Up [cnt] = Up[cnt-1];
   Dn [cnt] = Dn[cnt-1];
   Mid[cnt] = Mid[cnt-1];
   }
   
   if (UseDelimiter!=0)
   {
      for (int j=0;j<ObjectsTotal();j++)
      {
      int NumStr = StringFind(ObjectName(j),setup,0);
      
         if (NumStr == 0)
         {
         datetime Time1=ObjectGet(ObjectName(j),OBJPROP_TIME1);
         int BarTime1=iBarShift(NULL,0,Time1);
         Time1 = Time[BarTime1+1];
         ObjectSet(ObjectName(j),OBJPROP_TIME1,Time1); 
            if( BarTime1+1 > MaxTicks )
            {
               if (!ObjectDelete(ObjectName(j)))
               {
               int _GetLastError = GetLastError();
               Print("ObjectDelete: ",ObjectName(j)," Error #", _GetLastError );
               }
            }
         }       
      }
   }      
}

bool isNewBar()
{
   bool res=false;
   if (Time[0]!=pTime)
   {
   res=true;
   pTime=Time[0];
   }   

   return(res);
}

bool ObjDel(string name)
{
   int _GetLastError = 0;
     
   while(ObjFind(name,0,0) > 0)
   {
      int obtotal = ObjectsTotal();
      for (int i = 0; i < obtotal;i++)
      {
         if (StringFind(ObjectName(i),name,0) >= 0)
         {
            if (!ObjectDelete(ObjectName(i)))
            {
            _GetLastError = GetLastError();
            Print( "ObjectDelete( \"",ObjectName(i),"\" ) - Error #", _GetLastError );
            }
         }
      }
   }      
   if(_GetLastError > 0) return(false);
   else
   return (true);
}
//-----
int ObjFind(string name,int start, int num)
{
   int cnt = 0;
   
   for (int i = 0; i < ObjectsTotal();i++)
      if (StringFind(ObjectName(i),name,start) == num) cnt+=1;
   
   return(cnt);
}         
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   if(tickCounter == 0)
   {
      for(int i=Bars-1;i>=0;i--)
      {
      Ticks[i]=0.0;
      Up[i]=0.0;
      Dn[i]=0.0;
      Mid[i]=0.0;
      }
   }
   
   tickCounter++;

   
   if(tickCounter >= MaxTicks) 
   {
   tickCounter = MaxTicks; 
   Ticks[tickCounter]=0.0;
   Up[tickCounter]=0.0;
   Dn[tickCounter]=0.0;
   Mid[tickCounter]=0.0;
   }
  
   if (isNewBar()) {if(UseDelimiter == 1) SetDelimeter();}
   else
   ShiftArray();
      
   Ticks[0]=Bid;
   
   if(tickCounter>=Length)
   {
   Up[0] = TickHighest(Length); 
   Dn[0] = TickLowest(Length); 
   Mid[0]= 0.5*(Up[0] + Dn[0]);
   }   
//----
   return(0);
}
//+------------------------------------------------------------------+
double TickLowest(int per)
{
   double lowest = 1000000;
   for(int i = 0;i < per;i++) lowest = MathMin(lowest,Ticks[i]);
   return(lowest);
}                

double TickHighest(int per)
{
   double highest = 0;
   for(int i = 0;i < per;i++) highest = MathMax(highest,Ticks[i]);
   return(highest);
}                

