Indicator to label significant highs and lows with number of bars to previous high

 

Hi,

I'm trying to create an indicator to label significant highs and lows .

I'm stuck on two bits:

1) I think the code below should calculate how many bars to the previous high, but it doesn't appear to be working. This is just a test indicator in a separate window to check my code is working.

2) When this code works I would like to use the bars to previous high data in ExtMapBuffer1[]  to label certain bars eg only those with a 5 bar high with an arrow and a label indicating how many bars ot the previous high. How would I use an indicator to  label each bar with that number.

Thanks in advance for any help. 

 

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red


//--- buffers
int ExtMapBuffer1[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

//---- check for possible errors
   if (counted_bars<0) return (-1);
   
//---- last counted bar will be recounted
   
   if (counted_bars>0) counted_bars--;
   
   
   int pos = Bars - counted_bars;   
   
   Print ("Initial position (Bars) = ",pos);
   
   while(pos>=0)
      {
            pos --;
            
            int barstobechecked = Bars - pos;
            
            Print("Loop position = ",pos,"; Bars to be checked = ",barstobechecked);

            for (int y = 1; y <= barstobechecked; y++)

               {

                  double testhigh = High[pos+y];
                  
                  Print("pos = ",pos,"; y = ",y,"; testhigh = ",testhigh,"; Bar High =",High[pos]);
  
                  if (testhigh >= High[pos]) 
                     {
                        
                        ExtMapBuffer1[pos] = y;
                        Print(" Last High found ",y," bars ago");
                        Print ("barstohigherhigh of ",pos," = ",ExtMapBuffer1[pos]);
                        break;
                     }                  

                }
      
      }

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

Something like this is doing ??

highest lowest 24 & 72 periods 

 

 The red line is zigzag 24 periods

the other lines give values of the highest yellow short   orange longer period

and lowest the two different blue lines....      comment message with info what bar  

 
deVries:

Something like this is doing ??

 

 

 The red line is zigzag 24 periods

the other lines give values of the highest yellow short   orange longer period

and lowest the two different blue lines....      comment message with info what bar  


devries,

 no it it even simpler than that, I want it to output the pic below:

 

 

where the 10, 20, 25, 32 ,54 are highest high in the last 10, 20, 25, 32 bars.

I want to combine this indicator code with the lowest lows - not shown in picture.

At each bar I want the indicator to count how many bars back it has to go to find a higher high (and lower low). I want to store this integer in an array.

I want to then check if it is peak (or trough) by checking if it is the highest bar in the 1,2 or 3 bars each side of it.

Then I want to check if it's value is greater than an inputted variable, i.e. only the highest in the last 20 bars etc. 

Finally, if it meets all those criteria, the bar is marked with an arrow and the int value in the array e.g. 25 bars to highe rhigh is written over the bar in question as a text label.

Any help greatly appreciated. 

 

not tested

    for(int k=0 ; k < counted_bars; k++) 
       {
        int j = k+1; 
        while (High[j]<High[k] && j < counted_bars)j++;
        Highbuffer[k]= (j-1)-k;
       } 
 
deVries:

not tested

 


Thanks deVries,

 

Very elegant.

 

Am currently testing but looks good so far. 

 
deVries:

not tested

 

This appears to be working!! Had to fiddle a bit with offsets, but looks good on eurusd daily chart.

I failed in getting  the line

          int     testhigh = (j-1)-k;

to assign the value to an array testhigh[] or any other array declared either before start() or after, the value constantly assigned was 0. I did expand the number of buffer arrays to 4, but that didn't work either.

I would have preferred this solution as it would have given me an array I could reference from an EA. But it was beyond me.

Any suggestions for improvements gratefully accepted. 

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Purple
#property indicator_color2 Orange

extern int x = 1;
extern int sig = 20;
extern int arrowoffset = 20;
extern int labeloffset = 60;
extern int fontsize = 6;
extern string font = "Verdana";


//--- buffers
double ExtMapBuffer1[]; // Swing High
double ExtMapBuffer2[]; // Swing Low 



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,SYMBOL_ARROWDOWN);
   SetIndexBuffer(0,ExtMapBuffer1);


   
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,SYMBOL_ARROWUP);
   SetIndexBuffer(1,ExtMapBuffer2);


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
   double arrowoffsethigh = NormalizeDouble(((arrowoffset*10)*Point),Digits);
   double arrowoffsetlow  = NormalizeDouble(((arrowoffset*15)*Point),Digits);
   double labeloffsethigh = NormalizeDouble(((labeloffset*10)*Point),Digits) + arrowoffsethigh;
   double labeloffsetlow  = NormalizeDouble(((labeloffset*10)*Point),Digits) - arrowoffsetlow;
   
   Print(arrowoffsethigh," ",arrowoffsetlow," ",labeloffsethigh," ",labeloffsetlow);
   

   
   
   int    counted_bars=IndicatorCounted();
   
//---- check for possible errors
   if (counted_bars<0) return (-1);
   
//---- last counted bar will be recounted
   
   if (counted_bars>0) counted_bars--;
   
//---- Main Calculation Loop  
   
   int    highestin[]; // High of x bars
   
      for(int k=0 ; k < counted_bars; k++) 
       {
       // Calculate bars to last High
       
          int j = k+1; 
          while (High[j]<High[k] && j < counted_bars) j++;
          int     testhigh = (j-1)-k;
          string  highlabel = testhigh;

       
       
       // Calculate bars to last low
       
          int l = k+1; 
          while (Low[l]>Low[k] && l < counted_bars) l++;
          int testlow = (l-1)-k;
          string  lowlabel = testlow;


        
       double barhigh, barlow;
       
       barhigh = High[k];
         
         if (k-x <= 0) {ExtMapBuffer1[k] = EMPTY_VALUE;}
            else
            {
               
               if ((barhigh == High[iHighest(Symbol(),Period(),MODE_HIGH,((x*2)+1),(k-x))]) && (testhigh >= sig))
                {
                ExtMapBuffer1[k] = barhigh + arrowoffsethigh;
                ObjectCreate(StringConcatenate("High ",k),OBJ_TEXT,0,Time[k],(High[k] + labeloffsethigh));
                ObjectSetText(StringConcatenate("High ",k),highlabel,fontsize,font,Purple);
                }
                  else {ExtMapBuffer1[k] = EMPTY_VALUE;}
            
            }
         
         barlow = Low[k];
         
         if (k-x <= 0) {ExtMapBuffer2[k] = EMPTY_VALUE;}
            else
            {
               
               if ((barlow == Low[iLowest(Symbol(),Period(),MODE_LOW,((x*2)+1),(k-x))]) && (testlow >sig))
                {
                ExtMapBuffer2[k] = barlow - arrowoffsetlow;
                ObjectCreate(StringConcatenate("Low ",k),OBJ_TEXT,0,Time[k],(Low[k] - labeloffsetlow));
                ObjectSetText(StringConcatenate("Low ",k),lowlabel,fontsize,font,Orange);
                }
                  else {ExtMapBuffer2[k] = EMPTY_VALUE;}
            

        
       } 

   
  
         
         
      
      }

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