Moving Average and Standard Deviation

 

I am trying to write an indicator that plot the volume, its moving average and its standard deviation. When I compile it, there is no error. However, when I try to plot it onto the chart, there isn't anything. What's went wrong?

Thanks in advance. 

Below is the code.


#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   4
//--- plot Volume
#property indicator_label1  "Volume"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot VMA
#property indicator_label2  "VMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot VSD_Top
#property indicator_label3  "VSD_Top"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot VSD_Bot
#property indicator_label4  "VSD_Bot"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- input parameters
input int      Volume_MA;
input int      SD;
//--- indicator buffers
double         VolumeBuffer[];
double         VMABuffer[];
double         VSD_TopBuffer[];
double         VSD_BotBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,VolumeBuffer);
   SetIndexLabel(0,"Volume");
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(1,VMABuffer);
   SetIndexLabel(1,"Volume Moving Average");
   SetIndexDrawBegin(1,Volume_MA);
   SetIndexBuffer(2,VSD_TopBuffer);
   SetIndexLabel(2,"Volume SD Top");
   SetIndexDrawBegin(2,Volume_MA);
   SetIndexBuffer(3,VSD_BotBuffer);
   SetIndexLabel(3,"Volume SD Bot");
   SetIndexDrawBegin(3,Volume_MA);
   
//---
   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 counted_bars=IndicatorCounted();
   if(counted_bars<0)return(-1); 
   if(counted_bars>0) counted_bars--; 
   int limit=Bars-counted_bars-1; 
   
   for(int i=0;i<limit;i++)
   {
   VolumeBuffer[i]=volume[i];
   VMABuffer[i]= iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],0,i);
   VSD_TopBuffer[i]= iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],1,i);
   VSD_BotBuffer[i]= iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],2,i);
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 
kentaicm:

I am trying to write an indicator that plot the volume, its moving average and its standard deviation. When I compile it, there is no error. However, when I try to plot it onto the chart, there isn't anything. What's went wrong?

Thanks in advance. 

Below is the code.


  1. replace #property indicator_separate_window with #property indicator_chart_window
  2. assign values to inputs, like
    input int      Volume_MA=10;
    input int      SD=10;
    or enter them manually on indicator start.
 
Marcin Madrzak:

  1. replace #property indicator_separate_window with #property indicator_chart_window
  2. assign values to inputs, likeor enter them manually on indicator start.
Still not workable. 
 
kentaicm:
Still not workable. 
In Mql4 you can't use iBands on a volume array like that, you need to use iBandsOnArray. See here for a code snippet: https://www.mql5.com/en/forum/309671#comment_11223077
How to apply an iMA to a handle?
How to apply an iMA to a handle?
  • 2019.04.04
  • www.mql5.com
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...
 
  1. kentaicm:
      VolumeBuffer[i]=volume[i];
       VMABuffer[i]    = iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],0,i);
       VSD_TopBuffer[i]= iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],1,i);
       VSD_BotBuffer[i]= iBands(NULL,0,Volume_MA,SD,0,VolumeBuffer[i],2,i);
    1. volume has no set direction, you are using it as As Series. Either set the direction or use the predefined Volume[].
      To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
                Event Handling Functions - Functions - Language Basics - MQL4 Reference
    2. The sixth argument of iBands is applied price. What price are you selecting when VolumeBuffer[i] is above six? You need to use iBandsOnArray

  2. lippmaje https://www.mql5.com/en/forum/309671#comment_11223077
    Please use the link button Use the link button See the difference? https://www.mql5.com/en/forum/309671#comment_11223077
              Messages Editor
 
whroeder1:
    1. volume has no set direction, you are using it as As Series. Either set the direction or use the predefined Volume[].
    2. The sixth argument of iBands is applied price. What price are you selecting when VolumeBuffer[i] is above six? You need to use iBandsOnArray

  1. Please use the link button See the difference? https://www.mql5.com/en/forum/309671#comment_11223077
              Messages Editor

You guys are great helper!! Solved. Managed to get it. Thanks to all here.

Reason: