Custom MovingAverage for MT5

fxwalb

Master Trader
Dec 18, 2009
31
0
62
Hi to everybody!

This attached Custom MovingAverage has, as far as I can see, no option yet for price parameter.

I would like to use also high and low e.g.

Can anybody give me a hint how to get this option?

Is this a hidden feature in the code or do I have to change the code?

Thanks in advance.
 

Attachments

  • Custom Moving Average.mq5
    7.1 KB · Views: 30

Enivid

Administrator
Staff member
Nov 30, 2008
19,240
1,507
144
Odesa
www.earnforex.com
1. The OnCalculate() function should redeclared for more input parameters:

MQL5:
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[])

2. Add the input parameter for price type.

3. Inside the OnCalculate() function check the input type and send he appropriate price array (open/close/high/low) instead of the "price" array to MA calculate functions.
 
Last edited:

fxwalb

Master Trader
Dec 18, 2009
31
0
62
Thanks for the advice.

did the following: changed on Calculate in this way
-------------------------------------------------------
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[]
const int begin,
const double &price[])
{
//--- check for bars count
if(rates_total<InpMAPeriod-1+begin)
return(0);// not enough bars for calculation
//--- first calculation or number of bars was changed
if(prev_calculated==0)
ArrayInitialize(ExtLineBuffer,0);
//--- sets first bar from what index will be draw
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1+begin);

//--- calculation
ArraySetAsSeries(close, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);

ENUM_APPLIED_PRICE myMA_Price;

switch(InpMAPrice) {
case PRICE_CLOSE : myMA_Price=close; break;
case PRICE_OPEN : myMA_Price=open; break;
case PRICE_HIGH : myMA_Price=high; break;
case PRICE_LOW : myMA_Price=low; break;
case PRICE_MEDIAN : myMA_Price=(high+low)/2; break;
case PRICE_TYPICAL : myMA_Price=(high+low+close)/3; break;
case PRICE_WEIGHTED : myMA_Price=(high+low+close+close)/4;
}
switch(InpMAMethod)
{
case MODE_EMA: CalculateEMA(rates_total,prev_calculated,begin,myMA_Price); break;
case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,begin,myMA_Price); break;
case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,begin,myMA_Price); break;
case MODE_SMA: CalculateSimpleMA(rates_total,prev_calculated,begin,myMA_Price); break;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
-------------------------------------

is this correct or are there any mistakes in the code?
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
19,240
1,507
144
Odesa
www.earnforex.com
First, I am not sure if you need to set the price arrays as timeseries.

Second, you can't add or divide arrays. So, in your switch/case construction you have to run cycles through all price array and reassign each element of the array accordingly.