Array in an indicator

 
Hello mql fellows,
I would like to kindly ask you for help with the following issue:

What I want to do, is to draw an SMA (which is drawn OK) and to create a channel around it based on the volatility of the previous days. I create an array with daily ranges (OK as well) and try to use iMAOnArray to smoothe it. The problem I keep having is that the indicator seems to be using oldest ranges for the latest bars, because the values for the current bar changes when I load new bars into history.

Here is the part that causes trouble:

   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
 
   for(i=0; i<limit; i++)
   {
      MovingBuffer[i]=iMA(NULL,0,MA_Perioda,BandsShift,MODE_SMA,PRICE_CLOSE,i);
      //HighLow[i] = High[i] - Low[i];
   }
 
   for(i=limit; i>=0; i--)
   {
      HighLow[i] = High[i] - Low[i];
      Comment(HighLow[i]);
   }
 
   i=Bars-MA_Perioda+1;
   if(counted_bars>MA_Perioda-1) i=Bars-counted_bars-1;
   
   while(i>=0)
     {
      Deviation[i]= iMAOnArray(HighLow, Bars, 20, 0, MODE_EMA, i); 
      
      //Alert(HighLow[i]);
 
      UpperBuffer1[i]=MovingBuffer[i] + Factor1*Deviation[i];
      LowerBuffer1[i]=MovingBuffer[i] - Factor1*Deviation[i];
      
      UpperBuffer2[i]=MovingBuffer[i] + Factor2*Deviation[i];
      LowerBuffer2[i]=MovingBuffer[i] - Factor2*Deviation[i];
      
      UpperBuffer3[i]=MovingBuffer[i] + Factor3*Deviation[i];
      LowerBuffer3[i]=MovingBuffer[i] - Factor3*Deviation[i];
      i--;
     }

Attached is the whole indicator. Will be glad to reward the brave individual who fixes this.

Take care,
Jan
Files:
 

See ArraySetAsSeries() for your personal arrays when trying to use the built-in functions like iMAOnArray() with them.

 
phy:

See ArraySetAsSeries() for your personal arrays when trying to use the built-in functions like iMAOnArray() with them.

I did already but with no success. How would you implement it in the above code?
 

Like this example.

Change the indicator option from false to true to set the array as a series array. You can do that while the indicator is running.


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 DodgerBlue
 
extern bool UseSeriesArray = false;
 
double ma[];
double maOnArray[];
double array[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ma);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,maOnArray); 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
 return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   ArrayResize(array, Bars);   
   ArraySetAsSeries(array, UseSeriesArray); // Option sets true or false
   
   int period = 13;
   // Fill private array with prices
for(int i = Bars-1; i >= 0; i--){ array[i] = Close[i]; } // display indicators. if the private array is good, the two lines will be the same double offset = 10*Point; // so the two indicators don't overlap exactly for( i = Bars-1-period; i >= 0; i--){ ma[i] = iMA(Symbol(), 0, period, 0, 0, 0, i); maOnArray[i] = iMAOnArray(array, 0, period, 0, 0, i) - offset; } return(0); }
 
Great phy, it works. Thanks a lot.
Any idea why I keep getting "cannot resize the array" error though? It seems the array is resized (the number of elements in the array increases by 1 with each new bar) but I still get the error with each new tick.
 

Maybe cannot resize means you request the same size...

if(ArraySize(array) != Bars) ArrayResize(array, Bars);

 
Hey phy,
as promissed, you deserve a part of the money I got for the indicator. Drop me a line at jan[dot]klimo[@t]gmail[dot]com if you have paypal and care to accept a few bucks ;).

Take care,
Jan
 
This forum needs private messaging...
Reason: