Array problem

 

Hi


I am a developer who comes from a Java-background. Therefore I have a hard time understanding Array handling in MQL.


Lets say I want to make an Indicator, in the calculation of the "current value", I need to know lets say the squareroot of some "earlier values", therefore I want an array with squareroots of every value (every bar). Here is the problem, because I don't want to show the squareroot value in the chart. Therefore the array is not a "buffer" and it therefore wont automatically push backwards on every new period/timeframe (I mean that value[i] wont become value[i+1], so that index 0 is the most recent point). How do I achieve this? I hope you understood the question, feel free to comment...


Thanks in advance.

 
sigvardsen:

Hi


I am a developer who comes from a Java-background. Therefore I have a hard time understanding Array handling in MQL.


Lets say I want to make an Indicator, in the calculation of the "current value", I need to know lets say the squareroot of some "earlier values", therefore I want an array with squareroots of every value (every bar). Here is the problem, because I don't want to show the squareroot value in the chart. Therefore the array is not a "buffer" and it therefore wont automatically push backwards on every new period/timeframe (I mean that value[i] wont become value[i+1], so that index 0 is the most recent point). How do I achieve this? I hope you understood the question, feel free to comment...


Thanks in advance.

Hi sigvardsen

I suggest you look at the ZigZag indicator that comes with MT4 for an example of what you want to do.

This property sets the number of visible indicator buffers

#property indicator_buffers 1

but later, in init(), the function SetIndexBuffer() is used three times to assign arrays to inidcator buffers. The first one is drawn, extra ones are not.

   SetIndexBuffer(0,ZigzagBuffer);
   SetIndexBuffer(1,HighMapBuffer);
   SetIndexBuffer(2,LowMapBuffer);

You would use such additional indicator buffers for your intermediate calculations.

Hope this helps.

Cheers

Jellybean

 
Jellybean:

Hi sigvardsen

I suggest you look at the ZigZag indicator that comes with MT4 for an example of what you want to do.

This property sets the number of visible indicator buffers

but later, in init(), the function SetIndexBuffer() is used three times to assign arrays to inidcator buffers. The first one is drawn, extra ones are not.

You would use such additional indicator buffers for your intermediate calculations.

Hope this helps.

Cheers

Jellybean


Thank you for the reply, I have examined the ZigZag indicator. But I don't understand why the following indicator doesn't show any graph:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- buffers
double p[];
double q[];

int init()
{
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, p);
   SetIndexBuffer(1, q);
   return(0);
}
int deinit()
{
   return(0);
}
int start()
{
   int limit = Bars - IndicatorCounted();
   int i;
   for (i = limit; i >= 0; i--)
   {
      q[i] = Close[i] * 2;
      p[i] = q[i];
   }
   return(0);
}
 
sigvardsen:

Hi


I am a developer who comes from a Java-background. Therefore I have a hard time understanding Array handling in MQL.


Lets say I want to make an Indicator, in the calculation of the "current value", I need to know lets say the squareroot of some "earlier values", therefore I want an array with squareroots of every value (every bar). Here is the problem, because I don't want to show the squareroot value in the chart. Therefore the array is not a "buffer" and it therefore wont automatically push backwards on every new period/timeframe (I mean that value[i] wont become value[i+1], so that index 0 is the most recent point). How do I achieve this? I hope you understood the question, feel free to comment...


Thanks in advance.

From the Article "Transferring an Indicator Code into an Expert Advisor Code, Indicator Structure"

//---- INDICATOR BUFFERS EMULATION

int NewSize = iBars(symbol, timeframe); //---- Checking the change of the zero bar if(ArraySize(Ind_Buffer0) < NewSize) { //---- Set the direct indexing direction in the array ArraySetAsSeries(Ind_Buffer0, false);

ArraySetAsSeries(Ind_Buffer1, false);

ArraySetAsSeries(Ind_Buffer2, false); //---- Change the size of the emulated indicator buffers ArrayResize(Ind_Buffer0, NewSize);

ArrayResize(Ind_Buffer1, NewSize);

ArrayResize(Ind_Buffer2, NewSize); //---- Set the reverse indexing direction in the array ArraySetAsSeries(Ind_Buffer0, true);

ArraySetAsSeries(Ind_Buffer1, true);

ArraySetAsSeries(Ind_Buffer2, true); } //----

 
sigvardsen wrote >>

Thank you for the reply, I have examined the ZigZag indicator. But I don't understand why the following indicator doesn't show any graph:

Try added "IndicatorBuffers":

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- buffers
double p[];
double q[];

int init()
{
IndicatorBuffers(2);

SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0, p);
SetIndexBuffer(1, q);
return(0);
}
int deinit()
{
return(0);
}
int start()
{
int limit = Bars - IndicatorCounted();
int i;
for (i = limit; i >= 0; i--)
{
q[i] = Close[i] * 2;
p[i] = q[i];
}
return(0);
}