How to apply an iMA to a handle?

 

Hi all!

I'm trying to build an indicator based in ADX and a MA attached to it.

In MT5, when I use iMA I can apply it to a handle. How can I do this in MT4?

This is how I'm doing it:

for(int i=rates_total-14-1;i>=0;i--)
     {
      adx[i] = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,i);
      ma [i] = iMA(NULL,0,14,0,MODE_SMA,adx[i],i);
     }

but I get a warning of "possible loss of data due to type conversion" and the indicator does not work nor displays anything.

How can this be done?

Regards,

PS

 

You can use iMAOnArray:

for(int i=rates_total-14-1;i>=0;i--) {
   static double buffer[14];
   adx[i] = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,i);
   ArrayCopy(buffer,adx,0,i,14);
   ma[i] = iMAOnArray(buffer,0,14,0,MODE_SMA,0);
}
 
lippmaje:

You can use iMAOnArray:

Wow Thank you for your fast reply! It worked!

Now, I'm trying to plot this, but the plot does not work very well.

I'm experiencing two different things.

1) I can't plot the value of interest without plotting the adx and the ma. How can I fix that? Also, if I want to set the value of interest between -1 and 1 (like go or no go) it gives problems because of this issue.

2) I get array out of range if I remove this two arrays (adx and ma) as buffers in the indicator_buffer property (if I set that to 1 instead of 3). Why this happens?

Also, I can't get the indicator to draw the line, as stated in the indicator type "DRAW_LINE" and it draws like little segments.

This is my code:

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   1
#property indicator_type1  DRAW_LINE
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_color1  clrRed
#property indicator_label1 "Vol Test"

double vol[],volAux[];
double adx[],ma[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,vol,INDICATOR_DATA);
   SetIndexBuffer(1,adx,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ma,INDICATOR_CALCULATIONS);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int s;
   if(prev_calculated<=0)
     {
      s=1;
      ArrayInitialize(adx,EMPTY_VALUE);
      ArrayInitialize(ma,EMPTY_VALUE);
     }
   else
     {
      s=prev_calculated-1;
     }

   for(int i=rates_total-14-1;i>=0;i--)
     {
     static double buffer[14];
      adx[i]=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,i);
      ArrayCopy(buffer,adx,0,i,14);
      ma[i] = iMAOnArray(buffer,0,14,0,MODE_SMA,0);
     }

   for(int j=rates_total-14-1;j>=0;j--)
     {
      vol[j]=(adx[j]>ma[j]) ? 1:
             (adx[j]<ma[j]) ? -1:0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 

1) try to set the line color to none like so:

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,CLR_NONE);

2) you get index out of range because the engine cannot take care of resizing them when you remove it, I think it's the SetIndexBuffer that tells it to resize the buffer on each new bar. If you really don't need them as index buffer you need to do the ArrayResize on your behalf.

 
lippmaje:

1) try to set the line color to none like so:

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,CLR_NONE);

2) you get index out of range because the engine cannot take care of resizing them when you remove it, I think it's the SetIndexBuffer that tells it to resize the buffer on each new bar. If you really don't need them as index buffer you need to do the ArrayResize on your behalf.

1) Got it and yes, it works!


2) Hmm I don't know if I understand how to do this. This is my first indicator, so I feel a little bit lost and don't understand why I get this error if I remove those arrays as buffers index.


Basically, the part I want to plot is the result of the vol array (-1, 0 or 1) and I'm struggling with that.


By the way, I'm doing this because I want a good volume indicator. I'm following the nononsenseforex youtube channel and I'm missing a good volume indicator for trend trading. If you know any good fit, I would appreciate any recommendation.


Regards,

PS

 

2) It's a dynamic array, initially it is empty: 'double adx[];' <-- zero length, but resizeable via ArrayResize since dynamic. This makes your ArrayInitialize in OnInit needless, by the way. But because it is known as index buffer, the engine will do a resize on it whenever it is necessary. So that all values are shifted one item back and you can use adx[0] for the current bar. On the next bar then adx[0] has become adx[1] and adx[0] is available for computation. In case you don't want to set adx and ma as index buffer you have to do this resizing on your own.

Regarding the other part I cannot help.

 
Pedro Severin:


By the way, I'm doing this because I want a good volume indicator. I'm following the nononsenseforex youtube channel and I'm missing a good volume indicator for trend trading. If you know any good fit, I would appreciate any recommendation.


You may try Volume Zone Oscillator or Normalized Volume Oscillator - you should be able to find free versions on the net.

Some interesting indicators are offered as paid service along with futures volume feed - try clusterdelta.com.

 
Marcin Madrzak:

You may try Volume Zone Oscillator or Normalized Volume Oscillator - you should be able to find free versions on the net.

Some interesting indicators are offered as paid service along with futures volume feed - try clusterdelta.com.

Thank you. This indicators look very interesting.

I'm looking fo the Gadi Normalized Volume Bar, do you know it? It feels like its the normal volume indicator with a moving average on it.

By the way, how can I use the volume indicator that comes in mt4 as default? I can't call it with iVolume.