an indicator need help

 

i tried to write a indicator. this indicator should looks like zigzag.

the from the left first bar use the first bar's openprice as the first zag point.

then from left to right, if some the price is max or min than the first point over 20points draw section, and then the next point is max or min than the previous point over 20 points

my code:

#property copyright "Copyright ?2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       pipstep=20;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
double last;
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
      int limit;

//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

limit = Bars - counted_bars;

//  ExtMapBuffer1[limit] = last;
   for(int i=limit-1; i>0; i--)
      {
   
      if(i== limit-1) last = Open[i];
      
      if( High[i] >last+pipstep*Point && Low[i]< last+pipstep*Point)
      {
      ExtMapBuffer1[i] = last+pipstep*Point;
      last= ExtMapBuffer1[i];
     
      }else if( High[i] >last-pipstep*Point && Low[i]< last-pipstep*Point)
      {
      ExtMapBuffer1[i] = last-pipstep*Point;
      last = last-pipstep*Point;
     
      }
  // Print(i+"|"+ExtMapBuffer1[i]);
    }
   return(0);
  }
//+------------------------------------------------------------------+

please help to make this right. thank you.

 
stonejiang wrote >>

i tried to write a indicator. this indicator should looks like zigzag.

the from the left first bar use the first bar's openprice as the first zag point.

then from left to right, if some the price is max or min than the first point over 20points draw section, and then the next point is max or min than the previous point over 20 points

my code:

please help to make this right. thank you.

Not sure what your ideas is, hope the below code is what you need.

#property copyright "Copyright ?2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       pipstep=20;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
      int counted_bars=IndicatorCounted();
//----
      int limit;
      static double last = 0;
//---- last counted bar will be recounted
      limit = Bars - counted_bars - 1;

      int i = limit;
      
      while (i>=0)
      {
         if(i == Bars-1) last = Open[i];
      
         if (High[i] > last+pipstep*Point) 
         {
            last = High[i];
            ExtMapBuffer1[i] = High[i];
         } else {
            if (Low[i] < last-pipstep*Point) 
            {
               last = Low[i];
               ExtMapBuffer1[i] = Low[i];
            } else {
               ExtMapBuffer1[i] = 0;
            }
         }
         i--;
      }
      return(0);
  }
//+------------------------------------------------------------------+
Reason: