//+------------------------------------------------------------------+
//|                                                 BarCountTest.mq4 |
//|                                       Copyright © 2011, One day! |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, One day!"
#property link      ""

#property indicator_chart_window
                                                                              // EXPLANATORY COMMENTS
                                                                              //=======================================================

extern color   clrLineColour     =  Gray;                                     // Default line colour.
extern int     intFontSize       =  11;                                       // Default font size.
extern int     intLineWeight     =  1;                                        // Default line width.

string         strOriginBar      =  "BarCount";                               // Constant value for the name of the origin bar. Do not change this!
string         strName           =  "BCL:";                                   // Constant value for first part of the name of all vertical line objects, except the origin
datetime       dteOldTime;                                                    // Represents the last time value
datetime       dteNewTime;                                                    // Used to find the current time value of the origin bar
int            intTimeFrame;                                                  // Store the value for the current chart timeframe
int            i,j,k;                                                         // Used as loop controls
int            A_intTest[32]     =  {11,22,33,45,56,67,79,90,101,112,123,135,       
                                    146,157,168,180,191,202,213,225,236,247,
                                    258,270,281,292,303,315,326,337,348,360}; // Array of integers to use in testing
                                    
int init(){
   
   if(ObjectFind(strOriginBar) == -1){                                        // If the bar is not on the chart, objectfind will return -1
      //Alert("Origin bar cannot be located!"); 
      Alert("Origin bar cannot be located! Creating Now");
      ObjectCreate   (strOriginBar,OBJ_VLINE,0,Time[3],0);// If the returned value was -1 then the alert user
      
      
                                      
   }
   if(ObjectFind(strOriginBar) != -1){
      //Alert("Origin bar has been located!");                                // If the returned value wasn't -1 then the alert user
      intTimeFrame   =  Period();                                             // Initialise the period function and store the result
      dteOldTime     =  ObjectGet(strOriginBar,OBJPROP_TIME1);                // Find the time value of the bar; 
      UDF_DeleteCounts();
      UDF_DrawCounts(dteOldTime,intTimeFrame);                                // Call the user defined function to draw the vertical lines                                          
    }                                                                         // End of if statement
   return(0);
}

int deinit(){

   UDF_DeleteCounts(); 
                                                   // Delete all of the lines created by the indicator
   return(0);
}

int start(){

    

   if(ObjectFind(strOriginBar) != -1){                                        // If the bar is not on the chart, objectfind will return -1
      dteNewTime  =  ObjectGet(strOriginBar,OBJPROP_TIME1);
                         // If the returned value wasn't -1 then the bar exists and the time value is stored
      if(dteOldTime != dteNewTime){                                           // If different then...
         dteOldTime  =  dteNewTime;                                           // Store the new value then...
         UDF_DeleteCounts();                                                  // Delete the current lines then...
         UDF_DrawCounts(dteOldTime,intTimeFrame);                             // Draw them in the new locations  
      }
   }

   return(0);
}

//================================================================================================================================================
// User defined functions
//================================================================================================================================================
   /* UDF to draw the vertical bars
   
      Arguements:
      intOriginTime  =  the datetime value of the opening of the origin bar
      intTF          =  the integer value of the current timeframe
   */
//================================================================================================================================================

void  UDF_DrawCounts(int intOriginTime, int intTF){
   int            intIndex;                                                            // Store the index number of the bar
   int            intCurrArrayValue;                                                   // Used for storing the array value during the for loop
   int            intBarCount;                                                         // Used to count up from zero and compare with array values
   int            intElementCount;                                                     // Used for counting through the array elements
   datetime       dteNextTime;                                                         // Store the time value of a bar in the loop
   string         strCaption;                                                          // Label description for each bar   
   
   intBarCount       =  0;                                                             // Starts at zero. The index of the first element in the array is also 0
   intElementCount   =  0;                                                             // Starts at zero - the first element in the array
   intIndex          =  iBarShift(Symbol(),0,intOriginTime,true);                      // The bars index position can then be determined and stored
   
   for(i = intIndex; i >= 0; i--){                                                     // Start of main loop to decrement from the origin location
      intCurrArrayValue =  A_intTest[intElementCount];                                 // Store array value
      if(intBarCount == intCurrArrayValue){                                            // Check if the bar count matches the current array value
         dteNextTime =  iTime(Symbol(),intTF,i);                                       // Find the time value for the current bar in the sequence
         strCaption  = StringConcatenate("Count: ",intBarCount);                       // Form the caption to appear as the description label
         
         ObjectCreate   (strName + i,OBJ_VLINE,0,dteNextTime,0);                       // Create a line...
         ObjectSetText  (strName + i,strCaption,intFontSize,"Tahoma",clrLineColour);   // Add a caption to it...
         ObjectSet      (strName + i,OBJPROP_COLOR,clrLineColour);                     // Set the colour of the line...
         ObjectSet      (strName + i,OBJPROP_WIDTH,intLineWeight);                     // Set the thickness of the line
         
         intElementCount ++;                                                           // Add 1 to the element count to read the next value
      }
         
      intBarCount ++;                                                                  // Add one to the bar count  
   }                                                                                   // End of for loop  
   WindowRedraw();
  
} 

//================================================================================================================================================
// UDF to delete the vertical bars
//================================================================================================================================================

void  UDF_DeleteCounts(){
   
   int      j;                                           // Loop counter
   int      intObjTotal =  ObjectsTotal(OBJ_VLINE) + 1;  // Find all vertical line objects
   string   strObjRef;                                   // Store object name

   for(j = intObjTotal; j >=0; j--){                     // Start for loop
      strObjRef   =  ObjectName(j);                      // Define object
      if(StringSubstr(strObjRef,0,4) == "BCL:"){         // If it contains: "BDC:" characters then...
         ObjectDelete(strObjRef);                        // Delete it         
      }                                                  // end if
   }                                                     // end for
   WindowRedraw();
}