You are on page 1of 3

//+------------------------------------------------------------------+

//| VolumeX.mq5 |
//| Copyright 2019, Phillipe S. Scofield |
//| https://mlatstudent.blogspot.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Phillipe S. Scofield"
#property link "https://mlatstudent.blogspot.com/"
#property version "1.00"
#property description "Modificação do indicador padrão de volume com uma média
móvel incorporada"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_minimum 0.0
//+--------------------------------------------+
//| Volume Indicator drawing parameters |
//+--------------------------------------------+
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 Blue
//#property indicator_style1 STYLE_SOLID
#property indicator_width1 4
#property indicator_label1 "Volume"
//+--------------------------------------+
//| MA Indicator drawing parameters |
//+--------------------------------------+
#property indicator_type2 DRAW_LINE
#property indicator_color2 Red
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_label2 "Média"
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input ENUM_APPLIED_VOLUME volumeType=VOLUME_REAL; // Volume type
input int maPeriod=5; // Ma indicator period

double volumeBuffer[],ma[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator digits
IndicatorSetInteger(INDICATOR_DIGITS,0);

//---- set ExtBuffer[] dynamic array as an indicator buffer


SetIndexBuffer(0,volumeBuffer,INDICATOR_DATA);
//---- restriction to draw empty values for the indicator
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- set index draw begin
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);
//---- indexing the elements in buffers as timeseries
//ArraySetAsSeries(volumeBuffer,true);

//---- set ExtBuffer[] dynamic array as an indicator buffer


SetIndexBuffer(1,ma,INDICATOR_DATA);
//---- restriction to draw empty values for the indicator
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//--- set index draw begin
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,maPeriod-1);
//---- indexing the elements in buffers as timeseries
//ArraySetAsSeries(ma,true);

//----
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // number of bars in history at the
current tick
const int prev_calculated,// number of bars calculated at previous
call
const datetime &time[],
const double &open[],
const double& high[], // price array of maximums of price for
the indicator calculation
const double& low[], // price array of minimums of price for
the indicator calculation
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{

int i,limit;
//--- check for bars count
if(rates_total<maPeriod)
return(0);// not enough bars for calculation

if(IsStopped()) return(0); //Checking for stop flag


//--- first calculation or number of bars was changed
if(prev_calculated<maPeriod)
limit=maPeriod;
else limit=prev_calculated-1;
//--- the main loop of calculations
for(i=limit;i<rates_total && !IsStopped();i++)
{
if(volumeType==VOLUME_REAL)
{
volumeBuffer[i] = (double)volume[i];
} else {
volumeBuffer[i] = (double)tick_volume[i];
}
ma[i] = 0;
for(int k=0; k<maPeriod; k++)
{
ma[i] = ma[i] + volumeBuffer[i-k];
}
ma[i] = ma[i]/maPeriod;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+

You might also like