Writing a simple array in MQL4 to store indicator values and determine the running maximun/minimum

Ben Diagi
3 min readSep 15, 2019

I had literally spent two nights trying to figure this out because of how little available information there is about arrays and buffers in the mql4 documentation.

I hope this can help someone who’s struggled with it as much as I have in the past two days.

THE CHALLENGE

I want to calculate the lowest and highest values of a SMA everyday (from the start of day until current time), on every new candle, and compare that value to the closing price of the previous candle(s) (amongst many other things).

Sadly, one cannot simply do this using the iHighest or iLowest functions; as those only work with OHLC values.

I earlier attempted a workaround; where I tried to first ascertain the lowest and highest candle closes for the day range, and then calculate a moving average based on that candle close. However, that flopped blatantly, for obvious reasons.

MY SOLUTION

I’m sure there are a ton of other ways to do this better, but here’s what I did.

First, I have to create an array. The documentation is here.

int m = TimeMinute(TimeCurrent());
int h = TimeHour(TimeCurrent());
int bars = ((h*60)+m)/15;
int malookback=bars; // num. of bars to calc the value ofma
int madailyPERIOD = 50; // moving average period

double dllv; // variable to store the min. value
double madaily[97]; // declare an array

I want to figure out how many candles have been formed on the chart from the beginning of the day up till now. We know that the day starts at 00:00, therefore, we can simply subtract 00:00 from the current time to determine how many hours and minutes have been spent in a day.

I’m working with a 15 min chart here; so I’m adding the current hour to the current minute and dividing that by 15 in order to determine how many candles have passed since 00:00.

I would be using this value (malookback) in a for-loop later in order to quickly fill in the array everytime the function is called.

I didn’t know this, but apparently, you have to initialise an array after declaring it.

Initialise the Array:

   ArrayInitialize(madaily,0);

For Loop:

   
for(int i = 0; i < malookback; i++)

{
madaily[i]= iMA(NULL,PERIOD_M15,madailyPERIOD,0,
MODE_SMA,PRICE_CLOSE,i);

}

With the for loop, I’m filling the array with the indicator values every time the function is called.

Calculate the minimum using the value gotten from ArrayMinimum function(which is an index value).

  dllv =  iMA(NULL,NULL,50,0,MODE_SMA,PRICE_CLOSE,
ArrayMinimum(madaily,WHOLE_ARRAY,0));

Remember to reset your for loop every time it runs. I can do that here by writing

  if(condition){ return(1);malookback=0; }

This is what the function should look like

int ArrayCalcMin()
{
int m = TimeMinute(TimeCurrent());
int h = TimeHour(TimeCurrent());
int bars = ((h*60)+m)/15;
int malookback=bars;
Print(bars);
int madailyPERIOD = 50;

double madaily[97];
ArrayInitialize(madaily,0);

double dllv;

for(int i = 0; i < malookback; i++)

{

madaily[i] = iMA(NULL,PERIOD_M15,madailyPERIOD,0,
MODE_SMA,PRICE_CLOSE,i);

}
dllv = iMA(NULL,NULL,50,0,MODE_SMA,PRICE_CLOSE,
ArrayMinimum(madaily,WHOLE_ARRAY,0));

if(condition){return(1);malookback=0; }
else {return(0);malookback=0;}

}

Happy Trading!

--

--

Ben Diagi

I’m a Product Manager & Designer. I write about Product, Design and Finance. In my spare time, I build trading algorithms and create UX prototypes.