Problem with iMAOnArray; It doesn't function.

 
Hello

I had a problem with one of my custom indicators. after many hours of debuging and summarizing I reached to this piece of code. But this code plots nothing.
extern int SlowPeriod=100;
extern int FastPeriod=15;
extern int MaMethod=MODE_SMA;
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- buffers
double L1[];
 
double Price[];
 
 
int indexbegin = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0,L1);
    SetIndexLabel(0, "L1");   
    SetIndexStyle( 0, DRAW_HISTOGRAM);
 
    indexbegin = Bars - 20;
    if (indexbegin < 0)
        indexbegin = 0;
    
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i,j,k;
   
    int counted_bars = IndicatorCounted();    
    //---- check for possible errors
    if (counted_bars < 0) counted_bars = 0;
    //---- last counted bar will be recounted
    if (counted_bars > 0) counted_bars--;
    if (counted_bars > indexbegin) counted_bars = indexbegin;
 
    for (i = indexbegin-counted_bars; i >= 0; i--)
    {    
      ArrayCopySeries(Price,MODE_CLOSE,Symbol(),Period());
      
      L1[i]=-iMAOnArray(Price,0,SlowPeriod,0,MaMethod,i)+iMAOnArray(Price,0,FastPeriod,0,MaMethod,i);
 
      //if instead of the above line I use this:
      //L1[i]=Price[i];
      //a histogram is ploted. so there is not any problem with other parts of code. Only iMAOnArray has a problem.
   }
   return(0);
}
can you please help?

 
extern int SlowPeriod=100;
extern int FastPeriod=15;
extern int MaMethod=MODE_SMA;
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- buffers
double L1[];
 
double Price[];
 
 
int indexbegin = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0,L1);
   SetIndexLabel(0, "L1");   
   SetIndexStyle( 0, DRAW_HISTOGRAM);
   SetIndexBuffer(1,Price);
 
   indexbegin = Bars - 20;
   if (indexbegin < 0)
       indexbegin = 0;
    
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i,j,k;
   
    int counted_bars = IndicatorCounted();    
    //---- check for possible errors
    if (counted_bars < 0) counted_bars = 0;
    //---- last counted bar will be recounted
    if (counted_bars > 0) counted_bars--;
    if (counted_bars > indexbegin) counted_bars = indexbegin;
 
    for (i = indexbegin-counted_bars; i >= 0; i--)
    {    
      ArrayCopySeries(Price,MODE_CLOSE,Symbol(),Period());
      
      L1[i]=-iMAOnArray(Price,0,SlowPeriod,0,MaMethod,i)+iMAOnArray(Price,0,FastPeriod,0,MaMethod,i);
 
      //if instead of the above line I use this:
      //L1[i]=Price[i];
      //a histogram is ploted. so there is not any problem with other parts of code. Only iMAOnArray has a problem.
   }
   return(0);
}
//+------------------------------------------------------------------+
Two additional lines bolded in the init function
 

Thanks stringo

Do you mean iMAOnArray only works on indicator buffers and not on ordinary arrays? even without those two lines array Price were containing values and

L1[i]=Price[i];

were functioning.

In fact, I prefere to use iMAOnArray on arrays in my code.

 
AR78 wrote:

Thanks stringo

Do you mean iMAOnArray only works on indicator buffers and not on ordinary arrays? even without those two lines array Price were containing values and

L1[i]=Price[i];

were functioning.

In fact, I prefere to use iMAOnArray on arrays in my code.


bool ArraySetAsSeries( double&array[], bool set)
Sets indexing direction of the array. If the set parameter has the TRUE value, the array will be indexed in a reversed order, i. e., the last element has a zero index. The FALSE value sets a standard indexing order. The function returns the previous status.
Parameters:
array[] - The numeric array to set.
set - Array indexing order.
Sample:
double macd_buffer[300];
double signal_buffer[300];
int    i,limit=ArraySize(macd_buffer);
ArraySetAsSeries(macd_buffer,true);
 
for(i=0; i<limit; i++)
   macd_buffer[i]=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i);
   
for(i=0; i<limit; i++)
   signal_buffer[i]=iMAOnArray(macd_buffer,limit,9,0,MODE_SMA,i);
 

Thanks CockeyedCowboy

but this doesn't solve the problem. Please pay attention to the bold lines:

extern int SlowPeriod=100;
extern int FastPeriod=15;
extern int MaMethod=MODE_SMA;
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- buffers
double L1[];
 
double Price[];
 
 
int indexbegin = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0,L1);
   SetIndexLabel(0, "L1");   
   SetIndexStyle( 0, DRAW_HISTOGRAM);


   

        ArraySetAsSeries(Price,true);   
   //Adding or removing the following line also has no effect:
       //ArrayResize(Price,Bars);


 

   indexbegin = Bars - 20;
   if (indexbegin < 0)
       indexbegin = 0;
    
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i,j,k;
   
    int counted_bars = IndicatorCounted();    
    //---- check for possible errors
    if (counted_bars < 0) counted_bars = 0;
    //---- last counted bar will be recounted
    if (counted_bars > 0) counted_bars--;
    if (counted_bars > indexbegin) counted_bars = indexbegin;
 
    for (i = indexbegin-counted_bars; i >= 0; i--)
    {    
      ArrayCopySeries(Price,MODE_CLOSE,Symbol(),Period());
      
      L1[i]=-iMAOnArray(Price,0,SlowPeriod,0,MaMethod,i)+iMAOnArray(Price,0,FastPeriod,0,MaMethod,i);
 
      //if instead of the above line I use this:
      //L1[i]=Price[i];
      //a histogram is ploted. so there is not any problem with other parts of code. Only iMAOnArray has a problem.
   }
   return(0);
}
//+------------------------------------------------------------------+

Result is the same

 

Excuse me stringo

no new Idea?

 
tumbleweed rolls by...
 
Craig:
tumbleweed rolls by...

This time I solved my problem by redefining Ma functions inside of EA. but only after spending a lot of time to find the problem, without any debug facilities.
 
AR78 wrote:

Excuse me stringo

no new Idea?


Your Price array is not allocated. 3 ways:
1. Assign it to index buffer (my example)
2. Define it with non-zero size (double Price[10000];)
3. Resize it on the every start (very slow solution).
 

Thanks very much stringo.

 

and one more thing:

why

3. Resize it on the every start (very slow solution)?

And why not resizing it only once at init() ?

Reason: