[Help] How to create a MT4/MT5 Custom Volume Indicator?

 

[Help] How to create a MT4/MT5 Custom Volume Indicator? 

I want to create my custom Volume Indicator with moving average, and some added custom indicator in that main chart and sub-window but there's no setting in SetIndexStyle() function to draw the bar volume indicator? Is there any way to make that custom volume indicator with added setting like: Main Chart, Sub-window Mode?

Here's my code and screenshot: My custom volume indicator is very different from MT4 Volume Indicator. I want to make it looks like MT4 Volume.

Custom Vol

//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 clrDodgerBlue

double vol[];

int init()
{
   string short_name;

   IndicatorBuffers(1);
   
   SetIndexBuffer(0, vol);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   
   //--- name for DataWindow and indicator subwindow label
   short_name = "My Volume";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
  
   return(INIT_SUCCEEDED);
}

int start()
{ 
   int limit;
   int counted_bars=IndicatorCounted();
   
  //---- check for possible errors
   if(counted_bars<0) return(-1);
   
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   limit=Bars-counted_bars;
   
   for(int i=limit; i>=0; i--)
   {    
      vol[i] = Volume[i];
   }
   
   return 0;   
 }
 
Musngi:

[Help] How to create a MT4/MT5 Custom Volume Indicator? 

I want to create my custom Volume Indicator with moving average, and some added custom indicator in that main chart and sub-window but there's no setting in SetIndexStyle() function to draw the bar volume indicator? Is there any way to make that custom volume indicator with added setting like: Main Chart, Sub-window Mode?

Here's my code and screenshot: My custom volume indicator is very different from MT4 Volume Indicator. I want to make it looks like MT4 Volume.


You have to use as many buffers as you want to have colors in MQL4. BTW stop using obsolete functions init() and start(). You should use OnInit() and OnCalculate() instead.
 
Petr Nosek:
You have to use as many buffers as you want to have colors in MQL4. BTW stop using obsolete functions init() and start(). You should use OnInit() and OnCalculate() instead.
I cant find good resources to learn new style of mql4/mql5. 
 
Musngi:
I cant find good resources to learn new style of mql4/mql5. 
You can start reading documentation from this site. There are lots of examples of code. If you use MT4 you should read the documentation from https://docs.mql4.com/
 
Petr Nosek:
You can start reading documentation from this site. There are lots of examples of code. If you use MT4 you should read the documentation from https://docs.mql4.com/
I searched in forums and read documentation then I didn't find a way to make volume indicator like in mt4 volume indicator.
 
Musngi:
I searched in forums and read documentation then I didn't find a way to make volume indicator like in mt4 volume indicator.
You could make a copy of the custom indicator included with Metatrader and start editing it. Then it would look the same unless you break something. Creating an indicator in both the main window and the sub window at the same time is tricky, maybe you need something else. Here is the forum post where someone asked that question already https://www.mql5.com/en/forum/7314
 
Matthew Colter:
You could make a copy of the custom indicator included with Metatrader and start editing it. Then it would look the same unless you break something. Creating an indicator in both the main window and the sub window at the same time is tricky, maybe you need something else. Here is the forum post where someone asked that question already https://www.mql5.com/en/forum/7314
How do I make a copy if I can't find it in MT4 Indicator Folder?
 
Help me pls
 
Help me pls
 
Musngi:
Help me pls
Next time it will be a ban.
 
Alain Verleyen:
Next time it will be a ban.

I did it! haha.. Here's the source code/screenshot of my custom volume indicator. What do you say?

Custom Volume

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 3
#property indicator_color1 clrBlack
#property indicator_color2 clrGreen
#property indicator_color3 clrRed
#property indicator_width2 3
#property indicator_width3 3

double volumes_buffer[];
double volumes_up[];
double volumes_down[];

int init()
{
   //---- indicator buffers mapping
   SetIndexBuffer(0, volumes_buffer);       
   SetIndexBuffer(1, volumes_up);
   SetIndexBuffer(2, volumes_down);
   
   //---- drawing settings
   SetIndexStyle(0, DRAW_NONE);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   
   //---- sets default precision format for indicators visualization
   IndicatorDigits(0);   
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("My Volumes");
   SetIndexLabel(0, "Volumes");      
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);
   
   //---- sets drawing line empty value
   SetIndexEmptyValue(1, 0.0);
   SetIndexEmptyValue(2, 0.0);       
   //---- initialization done
  
   return(INIT_SUCCEEDED);
}

int start()
{ 
   int limit;
   int counted_bars=IndicatorCounted();
   
  //---- check for possible errors
   if(counted_bars<0) return(-1);
   
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   limit=Bars-counted_bars;
   
   for(int i=0; i<limit; i++)
   {
      double my_volume = Volume[i];
      if(my_volume > Volume[i+1])
      {
         volumes_buffer[i] = my_volume;
         volumes_up[i] = my_volume;
         volumes_down[i] = 0.0;        
      }
      else
      {
         volumes_buffer[i] = my_volume;
         volumes_up[i] = 0.0;
         volumes_down[i] = my_volume;        
      } 
   }        
   
   return 0;   
 }