MQL4 coding question, how to find nearest MA value

 

Hello guys, newbie coder here, just finished my first course and only been at this for a month,

anyways. I am currently trying to automate my strategy and in order to do so I need to find the nearest moving average that is greater than bid and then again to find the nearest MA that is below bid. and I need to save both of them so I can use the values as TPs and SLs...… to elaborate.

I use 4 moving averages on every timeframe except M1  so I have 4 on M5, 4 on M15. 4 on M30 and so on up to monthly. I have defined all my MA's and put them in an array. I managed to sort them from high to low, but I cant figure out how to get the one that's nearest to bid price... because its not a constant variable so it floats within the array between the MAs. So if MA 1= 1.0009 MA 2 = 1.0008 MA 3 = 1.0007 and Bid = 1.0006 and MA 4 = 1.0005 and MA 5 = 1.0004

I need to get the value of MA 3 and MA 4

as theyre the closest above and below MA periods..... But when I sort them as an array even if I use the bid  variable array as the starting point within the ArraySort function it starts with the highest above or below it not the nearest. I cant figure out how to get

the nearest above and below MA and its driving me crazy I've exhausted google and youtube and played around for hours...…… any suggestions ???? 

this is what I have so far.

"
  double v1 = M5P50;
  double v2 = iMA(Symbol(),5,100,0,0,0,0);
  double v3 = iMA(Symbol(),5,150,0,0,0,0);
  double v4 = iMA(Symbol(),5,200,0,0,0,0);
  double v5 = iMA(Symbol(),15,50,0,0,0,0);
  double v6 = iMA(Symbol(),15,100,0,0,0,0);
  double v7 = iMA(Symbol(),15,150,0,0,0,0);
  double v8 = iMA(Symbol(),15,200,0,0,0,0);
  double v9 = Bid;

  double arr[9];
  arr[0]=v1; arr[1]=v2; arr[2]=v3; arr[3]=v4; arr[4]=v5; arr[5]=v6; arr[6]=v7; arr[7]=v8; arr[8]=v9;
 ArraySort(arr,8,MODE_DESCEND);



Then I use comment function to list them on the chart from high to low but I don't know how to find the nearest above and below only the highest or the lowest......

 

Here are the steps I would approach this:

  • loop over all MAs that you want to consider
  • for each MA compute the distance to the bid
  • if this distance is positive and constitutes a new minimum, remember it together with the MAs properties as nearest above the bid
  • if this distance is negative and constitutes a new maximum, remember it as nearest below the bid

Some code would look like:

int periods[] = { PERIOD_M5, PERIOD_M15, PERIOD_M30, ... };
double mindist = 9999;
double maxdist = -9999;
int mindistp, mindistl, maxdistp, maxdistl;
double maabove, mabelow;

for(int i=0; i<ArraySize(periods); i++)
  for(int l=50; l<=200; l+=50) {
    double ma=iMA(NULL,periods[i],l,0,MODE_SMA,PRICE_CLOSE,0);
    if(ma-Bid>0 && mindist>ma-Bid) { mindist=ma-Bid; mindistp=i; mindistl=l; maabove=ma; }
    if(ma-Bid<0 && maxdist<ma-Bid) { maxdist=ma-Bid; maxdistp=i; maxdistl=l; mabelow=ma; }
  }
Print("ma above: ",maabove," P: ",periods[mindistp]," @",mindistl);
Print("ma below: ",mabelow," P: ",periods[maxdistp]," @",maxdistl);
 
lippmaje:

Here are the steps I would approach this:

  • loop over all MAs that you want to consider
  • for each MA compute the distance to the bid
  • if this distance is positive and constitutes a new minimum, remember it together with the MAs properties as nearest above the bid
  • if this distance is negative and constitutes a new maximum, remember it as nearest below the bid

Some code would look like:

Oh My ! that works perfect! Thank you so much! the only issue I had with it is it only prints on line? I love the last part you added where in displays on chart the nearest ma value and what MA it is. But it only shows the second line

this one shows up on the chart

Print("ma below: ",mabelow," P: ",periods[maxdistp]," @",maxdistl);

but this one does not

Print("ma above: ",maabove," P: ",periods[mindistp]," @",mindistl);

I tried fixing it but couldn't figure it out. any recommendations? 

if not no biggy I have what I need with that so thanks again!




 

No idea why the Print is missing. Could be a language specific quirk. Maybe if you add a semicolon like so:

for(int i=0; i<ArraySize(periods); i++)
  for(int l=50; l<=200; l+=50) {
    double ma=iMA(NULL,periods[i],l,0,MODE_SMA,PRICE_CLOSE,0);
    if(ma-Bid>0 && mindist>ma-Bid) { mindist=ma-Bid; mindistp=i; mindistl=l; maabove=ma; }
    if(ma-Bid<0 && maxdist<ma-Bid) { maxdist=ma-Bid; maxdistp=i; maxdistl=l; mabelow=ma; }
  };

Happy coding.

Reason: