MAY BE THAT A MINUTE OF YOUR TIME IS ONE MONTH TO TEAR MY HAIR

 

I try to Save the ZigZag indicator in a table, then creates a new array with non-zero points. It is very simple but I don 't do. I am a beginner in programming.


Here is the code that I wrote to accomplish this task:

If you have any suggestions, advice or links about similar problems ... thank you in advance, wishing you success in trading


extern int ExtDepth     = 15 ;
extern int ExtDeviation = 5 ;
extern int ExtBack      = 3 ;
//---- buffers

double Zig_Zag[] ;
double turn_level[] ;
int  turn_index[] ;
double sig[][] ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);

   SetIndexStyle(0,DRAW_HISTOGRAM);
   
   SetIndexBuffer(0,sig);
   SetIndexBuffer(1,Zig_Zag);
   SetIndexBuffer(2,turn_level);
   SetIndexBuffer(3,turn_index);

   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
 { 
   
   
   int  i , k , limit ,  counted_bars=IndicatorCounted();
   
   if(Bars < 0 ) return(0) ;
   
   limit = Bars - counted_bars ;
   
   if( limit > 0 )counted_bars-- ;
   
   // I would like to use a loop in order to save the ZIGZAG indicator in a array! But this poses problems!
  
   //the solution may be simple, but I have very weak notions of programming ...
   
   for( i = 0 ; i < limit ; i++ )
      { Zig_Zag[i] = iCustom(NULL , 0 , "ZigZag" , ExtDepth , ExtDeviation , ExtBack ,0 , i ); }
    /* 
    **I have tried to collect the output array in a file but it does not
    
    handle=FileOpen(FileName, FILE_CSV|FILE_WRITE, '\t');
    
    for( i = 0 ; i <limit ; i++ )
      { 
        Zig_Zag[i] = iCustom(NULL , 0 , "ZigZag" , 15 , 5 , 3 ,0 , i );
        FileWrite(handle,i , DoubleToStr(Zig_Zag[i],Digits) );    
       }
  
     FileClose(handle) ;
     
     **with this code; the file gets the data but I have no array
     
     handle=FileOpen(FileName, FILE_CSV|FILE_WRITE, '\t');
    
    for( i = 0 ; i <limit ; i++ )
      { 
        Zig_Zag = iCustom(NULL , 0 , "zigzagtest" , 15 , 5 , 3 ,0 , i );
        FileWrite(handle , i , Zig_Zag );    
       }
  
     FileClose(handle) ;
     */
   
   
   
   //the goal is to obtain two array corresponding levels and indices of price reversal ...
    
   k=0;
   for( i = 0 ; i< limit ; i++ )
      { if(Zig_Zag[i] != 0 )  { 
                                 turn_level[k] = Zig_Zag[i] ;
                                 turn_index[k] = i ;
                                 k ++ ;
                               }
       }
      
   int Nb_turn_pt = ArraySize(turn_level) ; // numbers of points turns 
   
    
      //  I have tested different way to obtain an indicator which gives the slope of the indicator ZigZag in a new array
       for( k = 0 ; k < Nb_turn_pt ; k++ )
       { for( i=0 ;  i< (turn_index[k+1] - turn_index[k]) ; i++ )
           { 
             if( turn_level[k+1] < turn_level[k] ) { sig[k][i] = 1 ;}
             if( turn_level[k+1] > turn_level[k] ) { sig[k][i] = -1 ; }
            
             
            }
       }  
  /*
        for(k = 0 ; k < Nb_turn_pt ; k++ )
          { for( i= turn_index[k] ; i < turn_index[k+1] ; i++)
             {
             if( turn_level[k+1] < turn_level[k] ) { sig[i] = 1 ;}
             if( turn_level[k+1] > turn_level[k] ) { sig[i] = -1 ; }
             }
          }  
        
         
        
        k=0 ;
        for(i = 0 ;  i < turn_index[Nb_turn_pt] ; i++ )
           { if( i < turn_index[k] && level_turn[k] < level[k+1]) sig[i]= 1 ;
             if( i < turn_index[k] && level_turn[k] > level[k+1]) sig[i]= -1 ;
             if(i == turn_index[k]) sig[i] = -sig[i-1] ;
             
             }
  */
         
   
//----
   return(0);
  }

 
when I print the last error after iCustom Loop i have error :4002 Attempt to access an array item number of which is out of the array range
 

Why don't you use this it might make the errors easier to understand. It sound like an index is not being set correctly.

int FileWriteArray( int handle, object array[], int start, int count) 
 

 
I haven't tested your code but the variable counted bars might be the cause your array error. try limit=Bars;
 
you right "circlesquares" I have tested you suggestion and I have no problem for built ZigZag[i] Array now ; but the indicator still return extrem values !! I'll place error test every where ; i'll give you news
 

i found a method more elegant to solve this problem by using ZigZagPoints from https://www.mql5.com/zh/code/7705 we dodge the problems of dynamic arrays and nested loops



Files:
zigsig.mq4  4 kb
Reason: