iMA, iMACD, iMAOnArray functions do not work.

 

In my first indicator, I am attempting something very simple which I am having difficulties in getting it to work correctly.

I want to draw a Buy Arrow (UP Arrow ) on Chart just below the Candle whenever MACD's Signal Crosses above MACD.

Opposite end will  be done once I get past this initial hurdles. 

 I've attempted all functions that I could find and all of them show zero in Macd Buffer and Signal Buffer.

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

     MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

OR

   for(i=0; i<limit; i++)

      MacdBuffer[i] =iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,1);

 

Also attempted to compute MACD by steps of first computing difference of fast and slow MA 

   for(i=0; i<limit; i++)
      Macd[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

 and then MA on difference

 

   for(i=0; i<limit; i++)
      SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

None of the methods produce expected result. What am I doing wrong? 

Except for iMaOnArray, none of the function have even report error. iMaOnArray reported error in log file that it will not work on Series Array. At one point, I had tried to pass Close[] Array to  the function.

To determine a Cross, I wrote this function

int Cross(int i, double firstArray[], double secondArray[])
{
    if ((firstArray[i-1] <= secondArray[i-1]) && (firstArray[i] > secondArray[i]))
        return (Low[i]);

   return (EMPTY_VALUE);
}

 

Should I use i-1 or i+1 in this function? Tried both and Neither works!!!

 
cdjindia:

In my first indicator, I am attempting something very simple which I am having difficulties in getting it to work correctly.

I want to draw a Buy Arrow (UP Arrow ) on Chart just below the Candle whenever MACD's Signal Crosses above MACD.

Opposite end will  be done once I get past this initial hurdles. 

 I've attempted all functions that I could find and all of them show zero in Macd Buffer and Signal Buffer.

<SNIP> 

Please edit your post . . . 


Please use this to post code . . . it makes it easier to read.

 
 
Thank you RaptorUK
 
cdjindia: Should I use i-1 or i+1 in this function? Tried both and Neither works!!!

i-1 is the future, you can't trigger the future.

if you count down, you don't need 3 separate loops.

   for(i=limit-1; i>=0; i--){
      double fast = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i),
             slow = iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
      Macd[i]=fast-slow;
      SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i); // you mean Macd[]
      bool wasAbove = Macd[i+] > SignalBuffer[i+],
            isAbove = Macd[i]  > SignalBuffer[i],
           isCross  = WasAbove != isAbove;
      // alternately
      //bool isCross = (Macd[i+] - SignalBuffer[i+]) * (Macd[i] - SignalBuffer[i]) < 0;
   }

Only MT4 provides a MACD with one line + histogram and that uses a SMA for the signal.

The other chart providers use a MACD with two lines + histogram and those use a EMA for the signal.

 
Thank you WHRoeder. Will try your pasted .... code